We started DocRaptor in 2010 because we couldn't find any high-quality HTML to PDF conversion libraries. While the quality of open-source PDF generators has improved over time, we still believe DocRaptor is the easiest and most powerful Node.js HTML to PDF option. That doesn't mean we're the best fit for every Node.js PDF conversion project though.
We'll use our extensive experience in PDF generation to compare the most popular JavaScript-based libraries. Creating PDFs from HTML can be surprisingly different than creating standard web pages. Our goal is to speed up your research and save you pain.
We haven't tried all these libraries in a production environment, so please do further testing and evaluation on your document—but we believe our shared PDF knowledge provides useful insight.
We'll review the most popular Node.js and JavaScript HTML to PDF libraries, as well as a few non-HTML-based and even non-JavaScript-based options. The large majority of libraries support both server-side Node.js and client-side JavaScript, so we've created just one guide to cover both. We'll specifically note if a library is only server-side or client-side.
Puppeteer is a Node.js (no client-side support) library for accessing headless Chromium. It's the most popular open-source HTML-to-PDF converter and has the best support for modern HTML, CSS, and JavaScript. The only downside is Chrome's lack of PDF-specific functionality such as headers and footers, accessible PDFs, page float, to name just a few. It's best for less complex documents, but still superior to any other HTML-to-PDF libraries.
jsPDF is one of the most popular JavaScript PDF generators. It is primarily focused on creating PDFs in an element-by-element, object-by-object style, but it does support HTML to PDF rendering through the html2canvas library. html2canvas supports a wide range of CSS properties, but is still considered experimental and not recommended for production usage. html2canvas also doesn't work in Node.js, though standard jsPDF does.
The jsPDF documentation is entirely focused on the method definitions and lacks tutorials for using these methods. While there are helpful guides available elsewhere and many StackOverflow answers, we found the learning curve to be steep.
Our recommendation would be to avoid using jsPDF for HTML to PDF conversion, and instead only use jsPDF as a PDF generator. Even then, something as simple and common as adding page numbers involves looping through all the pages and inserting a page number element on each page. Most HTML to PDF generators solve this with a single CSS property like content: "Page " counter(page, upper-alpha) " of " counter(pages, decimal);
.
The node-html-pdf library is obviously Node.js-only. It relies upon the PhantomJS headless browser, which is based on an incredibly old version of WebKit. We strongly recommend against using PhantomJS, as it's buggy and lacks modern HTML and JavaScript support. Use Puppeteer instead.
In addition to having excellent documentation and overall PDF creation capabilities, PDF-LIB also supports PDF editing—which is a relatively rare functionality. It supports forms, but misses other common features such as SVG images (you can draw SVG paths) or headers and footers.
Overall, PDF-LIB seems like the most modern JavaScript PDF convertor. It was created more recently than most of the libraries from this list and has the advantage of reusing some of their code without the limitations of an existing API
PDFKit is one of the oldest JavaScript-based libraries and is still under active development. Unlike the HTML-to-PDF libraries, PDFKit creates documents element-by-element, box-by-box. PDFKit supports hardly any complex PDF functionality, with the exception of forms. Tables must be drawn cell-by-cell, for example. Bookmarks and accessibility tagging must be created explicitly, rather than automatically defined as the more advanced HTML-based libraries will do. In general, PDFKit seems best for very simple documents.
PDFMake is actually based on PDFKit. However, unlike PDfKit's imperative approach, PDFMake uses a JSON-based declarative style. This removes a lot of flexibility, but PDFMake offers theoretically greater functionality. For example, PDFMake supports tables, headers/footers, watermarks, and columns—none of which are directly supported by PDFKit.
Unfortunately, the declarative approach severely limits PDFMake's capabilities for creating complex or graphically oriented PDFs. Each element is placed directly after the previous element, with no ability for overlapping elements or positioned elements. This is demonstrated by the PDFMake example documents. We would only recommend it for the most simple, text-based documents.
Unlike the pure-JavaScript libraries reviewed above, DocRaptor is a JavaScript wrapper around an online HTML to PDF API. We use the industry-leading, commercial Prince engine to generate the PDFs.
In our opinion, DocRaptor provides the best combination of advanced HTML and JavaScript support along with PDF-specific functionality that dramatically eases development. With simple HTML and CSS, DocRaptor lets you create:
You can be making advanced PDFs in minutes with DocRaptor's Node.js HTML to PDF code samples and JavaScript HTML to PDF library. No signup is required with our public testing API key and a free plan is also available.
In addition to DocRaptor, there are many other online HTML to PDF APIs and they frequently have Node.js or JavaScrap wrappers. All the other APIs use open-source PDF generators, typically Puppeteer or occasionally the dated wkhtmltopdf or PhantomJS libraries.
Like DocRaptor, these APIs will relieve your infrastructure and management burdens, but they'll suffer from the same downsides as the open-source libraries themselves and struggle with more complex PDF documents. They're best for simpler documents. We'd recommend comparing the customers, uptime guarantees, and samples documents of the various APIs.
Frankly, the JavaScript-based HTML to PDF library selection is relatively poor. Our Top Ruby HTML-to-PDF Libraries and Top Python HTML to PDF Libraries review some better alternatives.
In our experience, the complexity of your desired document is the primary factor in your library choice. For HTML to PDF converters, complexity increases when the number of non-web-page elements increases. For example:
As browsers are not designed to render these document-level and page-level elements, many HTML to PDF generators struggle with implementing these concepts, and many ignore them altogether. If your document contains such elements, the more testing you'll need to conduct (and the more likely you'll need a commercial solution, such as DocRaptor).
We hope this list of JavaScript HTML to PDF libraries and their respective PDF generation capabilities has saved you time and effort. If you have any questions about your conversion options or need help perfecting your DocRaptor document, please contact us at support@docraptor.com.