Skip to content

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

Description

@Clive321A

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions