Welcome Guest Search | Active Topics | Sign In | Register

Anchor links not working between merged documents Options
Megan
Posted: Tuesday, March 30, 2021 8:00:54 PM
Rank: Newbie
Groups: Member

Joined: 9/11/2019
Posts: 4
Hello,

I have a scenario requiring multiple calls to HtmlToPdf.ConvertHtml(), each with different HtmlToPdfOptions for each call, in order to accomplish different margins on different pages of the PDF. These individual documents then get merged together into a single document.

One thing I'm having issues with is anchor links. If the link and target reside in the same original document (were part of the same ConvertHtml() call), the links work correctly. However, if the link and target resided in two different documents before the merge, the links do not work. They wind up looking like this: file:///HtmlToPdf.Options.BaseUrl/NotSet#TestId123

I have tried merging the documents together two different ways:
1. Making multiple ConvertHtml() calls passing the same document
2. Making multiple ConvertHtml() calls each with a new document, and then calling PdfDocument.Merge() and passing the HtmlToPdfResults.

I've tried setting the HtmlToPdfOptions.BaseUrl to something explicitly but that doesn't seem to help. For example, I've tried setting it to the base URL of our website, but it then just prepends that URL before the anchor link like this: http://www.google.com/#TestId123 -- which is never going to be a legit URL as I am just trying to link to another element in the same PDF, not elsewhere on our website.

Below is a very simple, stripped down code sample I have tried that doesn't work. For reference, this is running EO.Pdf version 21.0.69.

Code: C#
var htmlToPdfResults = new List<HtmlToPdfResult>();
var pdfOptions = new HtmlToPdfOptions();
pdfOptions.NoLink = false;

var html1 = "&lt;div&gt;&lt;a href='#TestId123'&gt;Click Me&lt;/a&gt;&lt;/div&gt;";
var htmlToPdfResult1 = HtmlToPdf.ConvertHtml(html1, new PdfDocument(), pdfOptions);
htmlToPdfResults.Add(htmlToPdfResult1);

var html2 = "&lt;div id='TestId123' name='TestId123'&gt;Hello World&lt;/div&gt;";
var htmlToPdfResult2 = HtmlToPdf.ConvertHtml(html2, new PdfDocument(), pdfOptions);
htmlToPdfResults.Add(htmlToPdfResult2);

var doc = PdfDocument.Merge(true, htmlToPdfResults.ToArray());


What am I missing? Thanks in advance for your assistance.
eo_support
Posted: Wednesday, March 31, 2021 9:29:41 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,067
Hi,

The target element must be an A element. We do not recognize name attributes as targets on a DIV element.

Thanks!
Megan
Posted: Tuesday, April 13, 2021 5:12:59 PM
Rank: Newbie
Groups: Member

Joined: 9/11/2019
Posts: 4
Thank you for the reply. I was just using <div> as an example for simplicity, but in actuality our scenario (a table of contents concept) had the anchor link destination targets as <td> elements with id attributes for the fragment identifier. (I didn't know the element type mattered or I would have been more specific.) Because this exact scenario worked fine in EO.PDF when we had just a single document (before we had the need to use merge), I assumed the usual HTML rules applied and that you could use the id attribute on any element type to act as the fragment identifier.

Knowing the target can only be an <a> element with the name attribute, I was able to modify things to add hidden [display: none;] <a> elements before the <td>'s to use as the jump target. This solves our need - thank you!
eo_support
Posted: Wednesday, April 14, 2021 10:27:27 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,067
Great! Glad to hear that you it works for you now.


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.