Introduction
In this article, we’ll talk about a series of Javascript libraries for
generating PDFs.
We'll look into real-world use cases and we'll mainly focus on 5
things:
- the running environment
- the supported modules
- typings
- custom fonts
- easy to use
After this read, you'll be able to find the right PDF library for your
Javascript application.
In the end, we'll also introduce pdfme, a very handy and
powerful PDF library!
Let's GO pdfme Official Site
if you like it, give me a start⭐ https://github.com/pdfme/pdfme
Comparison
Compare 6 libraries
1. PDFKit
PDFKit is one of the first pdf libraries released in the huge
Javascript ecosystem. Available since 2012 has gained strong
popularity and it is still receiving updates as of 2021.
A little bit more difficult to use if compared to other libraries offers
support to both Node and the browser through Webpack.
And as we will see later in this comparison some PDF libraries are
wrappers around PDFKit.
It supports custom fonts and image embedding, but lacks of a high-
level API;
In addition, the documentation tends to be complex.
As you can expect, it requires a certain amount of time to get used
to it, and at the very beginning,
you'll find that designing PDFs will not be the easiest thing to do.
Point of evaluation | Evaluation |
---|---|
Works in Node and browser | △(a bit complicated) |
Typings | ○(DefinitelyTyped) |
Custom fonts | ○(Be careful when using this in a browser) |
Easy to use | △(a bit complicated) |
2. pdfmake
pdfmake is a wrapper library built around PDFKit. The main
the difference is in the programming paradigm:
while PDFKit adopts the classic imperative style, pdfmake has a
declarative approach.
That's why it's easier to focus on what you want to do, instead of
spending time telling the library how to achieve a determined result.
But not all that glitters is gold, you might encounter issues when
you try to embed custom fonts while using Webpack. Unfortunately,
there is no much documentation available on the web about this
issue. However, if you are not using Webpack you can easily clone
the git repository and run the font embedding script.
Point of evaluation | Evaluation |
---|---|
Works in Node and browser | △(Be careful using Webpack) |
Typings | ○(DefinitelyTyped) |
Custom fonts | △(Need to build your own) |
Easy to use | ○ |
3. jsPDF
jsPDF has the highest number of start among the pdf libraries in
GitHub, and not by chance it’s very stable and well maintained.
The modules are exported according to the AMD module standard,
which makes easy to use it with node and browsers.
As for PDFKit the APIs provided have an imperative pattern, with
the result that creating a complex layout tend to be very hard.
Embedding fonts it’s not difficult but needs an extra step: converting
the fonts to TTF files.
jsPDF is not the easiest library to master, but the documentation is
very rich so you’ll not encounter any particular obstacle while
working with it. https://rawgit.com/MrRio/jsPDF/master/docs/index.html)
Point of evaluation | Evaluation |
---|---|
Works in Node and browser | ○ |
Typings | ○ |
Custom fonts | ○(ttf files need to be converted) |
Easy to use | △(a bit complicated) |
4. Puppeteer
As you may know, Puppeteer is a Node library that provides a high-
level API to control Chrome, but it can also be used to create PDFs as well.
The templates have to be written in HTML, which makes jsPDF
very easy to use for web developers.
The following article is a good reference to use while you are
developing: Generate a PDF from HTML with puppeteer
Puppeteer has mainly two disadvantages:
- You need to implement a backend solution.
- You need to start Puppeteer every-time you need to create a PDF, which creates some overhead. It is slow.
If the disadvantages listed above are not a big problem for you,
then it may be a good option especially if you need to design
HTML-tables etc.
Point of evaluation | Evaluation |
---|---|
Works in Node and browser | x |
Typings | - |
Custom fonts | ○(web fonts) |
Easy to use | ? |
5. pdf-lib
pdf-lib is a library for creating and editing PDFs implemented
entirely in Typescript, and as for pdfmake is built around PDFKit.
Although it was released after all the other libraries, it’s very popular
with thousands of stars on GitHub.
The design of the APIs are awesome and of course, works with
both: node and browsers.
It supports PDF merging, splitting, embedding, and has a lot of
features that other libraries just don’t have;
pdf-lib is very powerful, but also very simple to use.
One of the hottest features is the support to Unit8Array and
ArrayBuffer to embed font files, which allows using fs
in case you
are working with node and xhr
in case you are working in the
browser. You’ll be able to feel its superiors in terms of performance
when you compare it with other libraries, and of course it can be
used with Webpack.
Also, this library has an imperative approach, and as can be
deduced working with complex layouts it’s not so easy.
Point of evaluation | Evaluation |
---|---|
Works in Node and browser | ○ |
Typings | ○ |
Custom fonts | ○ |
Easy to use | △(a bit complicated, the layout needs to be calculated) |
6. pdfme
Finally, we are at the end, so let me introduce pdfme. I personally developed this library, with the aim of
making Pdf-lib as declarative as possible.
In contraposition with pdf-lib, pdfme doesn’t require the
developer to calculate the layout by himself:
no need to define every time alignments, line-height, etc.
With the advantages of pdf-lib, such as the ability to use Uint8Array and ArrayBuffer for font data, and the ability to embed PDF files,
pdfme allows developers to create complex layouts efficiently.
You can also try Template Design & Code Generator to design your
favorite PDF layout and generate some executable code!
Point of evaluation | Evaluation |
---|---|
Works in Node and browser | ○ |
Typings | ○ |
Custom fonts | ○ |
Easy to use | ○ |
Let's GO pdfme Official Site
if you like it, give me a start⭐ https://github.com/pdfme/pdfme
Top comments (30)
Puppeteer is by far the easiest for me. You just create a page in your frontend framework with html5 /css and use puppeteer with headless chrome to create en exact pdf of that page. Very easy to do you could create a firebase app to use as backend
I just had a question about this approach. Rendering a page on a Headless browser is a resource heavy task.
Were you able to get the desired performance? If yes, how many users was this "firebase app" serving and how many requests for generating a PDF was your backend handling?
Thank You
SNikhill
I was able to get the desired performance but I'm using firebase so performance is never an issue. I don't have many users. Maybe firebase costs will rise fast if you have lot's of users/requests.
Indeed, Puppeteer is one of the easiest ways to create PDFs.
However, it can be difficult to use in some use cases.
If there is a scrollable div container with content, can it the whole scrollable area rather than visible part of div?
@ruben, It may look like easy.
But you have to open a browser, wait for page to load and take the screenshot. It is a CPU intensive work.
You have to manage memory efficiently.
⭐PDFMe⭐ looks promising. Let me try in my upcoming project.
pdfme looks really cool, will definitely try it.
An interesting library is this one too:
react-pdf.org/
It has pretty good ease of use if you are a front-end developer.
What is missing from so many PDF libs is the ability to load and modify existing PDFs. Like, I want to be able to load a PDF and stamp over it "PROCESSED". Well, maybe not that exact example. But at least take exiting PDFs, fill out their forms, add a layer of content, etc...
The signing of PDFs is also another tool. I get that really isn't PDF creation. But still, the e-signing libs can be a chore.
Can I use it in Angular rather than react?
For me the best PDF generator is headless chrome... A litte bit tricky to use but very efficient. the problem is that this chrome functionality has not been maintained for a while... I will try labelmake to see if it well manage page breaks. Thanks.
Good article. Check out the client-side pdf generator github.com/ovvn/dom-to-pdf which makes a printable paginated pdf from DOM.
Cool ! I will need that.
You can also try my own wrapper over puppeteer
github.com/AmirTugi/tea-school
I aimed to make the usage of puppeteer easy.
Any of the above deals with CMYK PDF output? Thanks
Your missing hummus library.