Rotativa.AspNetCore icon indicating copy to clipboard operation
Rotativa.AspNetCore copied to clipboard

Creating internal links in PDF, and BaseUrl that gets inserted into the HEAD

Open Clive321A opened this issue 2 years ago • 0 comments

I created a PDF that contains links to other locations in the PDF, using this in the razor view

<a class="menuItem" href="#ruleskey">Rules</a>

which links to

<a name="ruleskey">&nbsp;</a>

However in the ViewAsPdf.cs there is this code

string baseUrl = string.Format("{0}://{1}", context.HttpContext.Request.Scheme, context.HttpContext.Request.Host);
var htmlForWkhtml = Regex.Replace(html.ToString(), "<head>", string.Format("<head><base href=\"{0}\" />", baseUrl), RegexOptions.IgnoreCase);

byte[] fileContent = WkhtmltopdfDriver.ConvertHtml(this.WkhtmlPath, this.GetConvertOptions(), htmlForWkhtml);

Which sets a base href, this causes the links to not work, the only way I can get links to work is to

//string baseUrl = string.Format("{0}://{1}", context.HttpContext.Request.Scheme, context.HttpContext.Request.Host);
var htmlForWkhtml = html.ToString();

byte[] fileContent = WkhtmltopdfDriver.ConvertHtml(this.WkhtmlPath, this.GetConvertOptions(), htmlForWkhtml);

I guess the Base Url is there for image resources and the like, I have inlined my images as base64, which is why I probably dont have any other issues once I remove it.

Question is, is there a correct way to do clickable links to other locations in a PDF? OR can the addition of the BaseUrl be made configurable if its not needed?

Clive321A avatar Nov 14 '23 03:11 Clive321A