Cookies

We use cookies to improve your browsing experience on our site, show personalized content and analyze site traffic. By choosing "I Accept", you consent to our use of cookies and other tracking technologies.

To the top
Close
Zengo - Is Dompdf or pdf.js better for PDF generation? We will help you make the decision!
2022. 03. 02.

Is Dompdf or pdf.js better for PDF generation? We will help you make the decision!

Zengo - reading time3 minutes reading time

PDF files play a central role in the transmission of documents, but these have become standardized over time. This ensures that their format is maintained on other platforms and in print. Generating PDF files is an essential and important part of a web application or any system. Nowadays, all systems have the right interfaces and features for this, all for the convenience of the user.

What can PDF generation be used for?

On the user side, this is a very useful and important feature, as it allows us to export the received orders, data about a user or information about an entry, which we can even send in an aesthetic form to a customer. The possibilities are almost endless, making our lives easier.

When to choose which one?

The problem always determines exactly which method and lib we should use to generate a PDF. We need to know how much design elements need to be used in the PDF, whether it is our job to store the file on the server, what kind of server environment is available, and what the constraints are for that particular file.

There are many solutions available to us, but each has different advantages and disadvantages. Let's look at these!

What alternatives are available?

From the Backend page (Laravel/PHP) there are also several alternatives for generating PDFs. In Laravel, DOMPDF is the most used because it has a separate Laravel package that is quick and easy to install. In addition, we can choose from other alternatives:

There are also many different libs from the Javascript page, of which we prefer print.js, but other solutions are ready to use:

100%

DOMPDF

You can use dompdf lib to generate PDF files on the server side, using an html template.

Advantages
  • The completed PDF file is generated on the server side so we can store it there. This is one of the biggest advantages over client (frontend) side generation.
  • Handles page breaks well, except for tables (see at Disadvantages).
Disadvantages
  • It has server-side dependencies, so it is not available in all environments (php-font-lib, php mbstring extension, php dom extension).
  • It cannot process and display all html and css rules properly, there are some config settings (html5 parser) that you can enable to render multiple html rules.
  • You can't break a spreadsheet into multiple pages, so rows must fit on one page.
pdf.js

The pdf.js a Javascript based lib where we can generate the PDF document on the client side.

Advantages
  • We can use almost any design element (html, css) and they will all display properly. This is one of the biggest advantages over backend PDF generation on any server.
  • It can receive a json data structure where it can generate a table from key-value pairs, which can be formatted to some extent.
Disadvantages
  • In this case, due to the client-side generation, we cannot store the completed file on the server side.
  • The speed of generation is determined by the performance of the client-side machine.
  • We do not get a specific PDF, but a print image that we can save or print to PDF.
  • Page breaks can't be handled properly for content.
  • Generation does not work under Internet Explorer.
How to use them

Like the other packages available for PHP and Laravel, Laravel dompdf can be installed as a composer dependency with only the service provider must be registered in the Laravel config. This will make the functions provided by the package available in the project.

The print.js is easy to set up like any other frontend package: it can be used as an npm dependency where the functions provided by print.js become available on the Javascript page after installation. We can provide easy interoperability between server and client side generation by returning a rendered html from the backend and receiving it as a parameter.

Whichever one you choose, first make sure which aspects are important to you when generating a PDF!