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.

Existing jsPDF plugins

Hi, I’ve been working on a new html2pdf package that uses html2canvas + jsPDF to convert HTML content to PDF. I know there are already three existing jsPDF plugins for HTML: addHTML, fromHTML, and html2pdf (same name). I don’t want to step on any toes - from what I can tell:

  • fromHTML is the oldest plugin and renders the HTML directly to PDF (which is great), but its support for complex HTML/CSS is lacking.
  • addHTML is newer (but now deprecated) and uses html2canvas/rasterizeHTML to create a canvas, then puts the image onto the PDF page. Current state is described at #944.
  • html2pdf: I haven’t found much info (apart from the demo), but it looks like it renders directly to PDF like fromHTML, which again is great but runs the risk of poor HTML/CSS support.

New html2pdf package

My html2pdf package takes the same approach as addHTML - I convert to a canvas with html2canvas, split that image up into pages, and attach each image onto its own PDF page. I believe it has some advantages over the jsPDF plugins:

  • Easier to use - creating a PDF from an HTML element/page/string is one line: html2pdf(element);
  • Fits content to the page - it copies the HTML first into an (invisible) container div with the PDF page width, so that content can resize appropriately (also working on a ‘shrink-to-page’ option)
  • Supports page-breaks - a feature that comes up a lot, i.e. #190
  • Is external - it keeps the logic separate and can respond more readily to updates in both repos (including html2canvas internally in jsPDF causes some problems)
  • Is external (disadvantage) - it can’t be used inline with other jsPDF commands the way the current plugins are - it’s a standalone package that creates and closes the PDF.

Open issues

That said, I’ve found 48 open issues on jsPDF that I think html2pdf could resolve, but I don’t want to start pushing html2pdf if it conflicts with jsPDF’s internal implementations. @MrRio @Flamenco I’d appreciate your feedback!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:13
  • Comments:68 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
marc-y-marccommented, Jul 27, 2017

@eKoopmans

Dear sir, It is again proven that not all hero’s wear a cape these days (or do you? 😉.

My god, you have done an amazing job in simplifying this whole struggle to generate PDF’s from HTML via javascript. I have had so many issues untill i came across this post.

Thank you so much mister!

2reactions
amitmerincommented, Jan 29, 2019

@sandroden Try this: It should fix the error: “html2canvas not loaded”

saveAsPdf() {
      window.html2canvas = html2canvas;
      var doc = new jsPDF(
        'p', 'pt', 'a4'
      );
      doc.html(document.querySelector("body"), {
        callback: function(pdf) {
          pdf.save("cv-a4.pdf");
        }
      });
    }

You can find a forked running example of your code here

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML form method Attribute - W3Schools
The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can...
Read more >
HTML | method Attribute - GeeksforGeeks
The HTML | method Attribute is used to specify the HTTP method used to send data while submitting the form. There are two...
Read more >
.html() | jQuery API Documentation
This method uses the browser's innerHTML property. Some browsers may not return HTML that exactly replicates the HTML source in an original document....
Read more >
HTTP request methods - MDN Web Docs
Chrome Edge CONNECT Full support. ChromeYes. Toggle history Full support. Edge12. Toggle history DELETE Full support. ChromeYes. Toggle history Full support. Edge12. Toggle history GET Full...
Read more >
How can I create a function/method that makes a new html file?
How could I make a method/function that would generate a new html file? I have tried certain methods that create a new window,...
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