Skip to content

write_html: make href="#" a link to the top of the document - #2189

Merged
andersonhc merged 7 commits into
py-pdf:masterfrom
RavSinghChandan:html-href-top-of-document
Nov 14, 2026
Merged

andersonhc merged 7 commits into
py-pdf:masterfrom
RavSinghChandan:html-href-top-of-document

Conversation

@RavSinghChandan

Copy link
Copy Markdown

<a href="#">, as in the "Back to top" links of many web pages, makes output() fail after write_html():

FPDFException: Named destination '' was referenced but never set with set_link(name=...)

An empty fragment is not a missing target: the HTML spec defines it as the top of the document, and browsers go there. Running write_html() over about 400 real HTML pages, 4 failed this way.

write_html() now makes href="#" an internal link to the top of the first page, with add_link(page=1), as it already does for a page number. A link to a fragment that no id or <a name> defines, such as #nowhere, still raises as before (#2048).

test_html_link_to_the_top_of_the_document puts the link on the second page and checks that it goes to the top of the first; it fails without the change. Changelog entry under 2.9.1 / Fixed.

@andersonhc andersonhc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found one edge case: with a portrait first page and a landscape second page, href="#" lands partway down page 1. add_link(page=1) uses the current page’s height to calculate the destination.

Could we use the first page’s height here and add a regression test with different page orientations?

add_link() measures the destination from the top of the current page,
so from a landscape page the link landed partway down a portrait first
page.
@RavSinghChandan

Copy link
Copy Markdown
Author

Thanks, fixed. add_link() measures the destination from the top of the current page, so for href="#" the destination now takes its top from the height of page 1. test_html_link_to_the_top_of_the_document_from_another_orientation covers a portrait first page with the link on a landscape page, and the reverse; both fail without the change.

@andersonhc
andersonhc merged commit 8296b4f into py-pdf:master Nov 14, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants