question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support hyperlinks in html()

See original GitHub issue

I’m running jsPDF 2.3.1 to render PDFs from HTML. I am able to generate PDF files, but hyperlinks are not always rendering correctly.

Here are two examples:

<a href="https://github.com">https://github.com</a>

<a href="https://github.com">GitHub</a>

The first example generates a clickable link in the PDF (good), while the second example just colors the text blue with no hyperlinking (bad).

Here’s the code I’m using to generate this:

const doc = new jsPDF('portrait', 'pt', 'letter');
const fullDocBody = '<a href="https://github.com">GitHub</a>'; 
doc.html(fullDocBody, {
  // Callback function is called when html conversion to PDF completes
  callback: function (doc) {
    doc.save('test.pdf');   // Saves to a file
  },
  x: 60,
  y: 60
 });

Changing fullDocBody to one of the two link strings shown above will demonstrate the behavior I am describing.

My application needs to accept either link type, as it is taking HTML from a rich text editor component to render the PDF.

Thanks for your time.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

1reaction
rd0rnotcommented, May 18, 2021

@HackbrettXXX - thanks for looking at those files and confirming your earlier comments. I appreciate the quick response.

@joseDaKing - No, I haven’t found a convenient way to overcome this. I think one possible solution would be to write logic to parse the HTML and directly write content using the jsPDF API instead of relying on the .html() method. As I understand it (and I could be mistaken), .html() uses the html2canvas library to internally render the content and takes a “screenshot” of that rendered page to produce a PDF.

For my team, we will likely see if we can purchase an off-the-shelf PDF component. I think jsPDF is great, but generating a document from HTML doesn’t work the way we would need it to.

0reactions
Uzlopakcommented, Nov 3, 2021

Maybe if you take the onClone method as in #3178 and we do a .link() in that rectangular instead of an .addImage()?

Potentially an issue would be, that link is expecting the coordinates on the current page, so if you want to print on multiple pages, we would need to modify .link() it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML Links Hyperlinks
Links are found in nearly all web pages. Links allow users to click their way from page to page. HTML Links - Hyperlinks....
Read more >
Creating hyperlinks - Learn web development | MDN
Hyperlinks allow us to link documents to other documents or resources, link to specific parts of documents, or make apps available at a...
Read more >
Links in HTML documents
Since links may point to documents encoded with different character encodings, the A and LINK elements support the charset attribute.
Read more >
HTML Anchors: Here's How To Create Links For Fast ...
When writing external links try to avoid linking to direct competitors. You don't want to help their SERP ranking for search terms that...
Read more >
How to Create Links to Other Pages in HTML
Links are specified in HTML using the <a> tag. A link or hyperlink could be a word, group of words, or image. <a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found