

The official documentation states that “getting headless Chrome up and running in Docker can be tricky”. I think this is the trickiest part of the implementation – so let me save you a couple of hours of Googling.
#Pdfkit async await pdf
That was it! If you click on the save button, the PDF will be saved by the browser.
#Pdfkit async await install
Npm install html2canvas jspdf import html2canvas from 'html2canvas'Ĭonst domElement = document.getElementById('your-id') Html2canvas, to make a screenshot from the DOM This method is plain and simple: create a screenshot from the page, and put it in a PDF file. If you don’t have special needs, like selectable or searchable text in the PDF, it is a good and simple way to generate one. Option 1: Make a Screenshot from the DOMĪt first sight, this solution seemed to be the simplest, and it turned out to be true, but it has its own limitations. However, it probably makes more sense to let the backend handle it, as you don’t want to use up all the resources the user’s browser can offer.Įven so, I’ll still show solutions for both methods. It is possible to generate a PDF file both on the client-side and on the server-side. Final option 3: Puppeteer, headless Chrome with Node.jsĬlient side or Server side PDF generation?.Option 1: Making a Screenshot from the DOM.Client side or Backend side PDF generation?.This blogpost will walk you through on these possibilities and the final implementations.Ī personal comment before we get started: it’s quite a hassle, so buckle up! Table of Contents: So the PDF should have different styling and additions compared to the original React page.Īs the assignment was a bit more complex than what could have been solved with simple CSS rules, we first explored possible implementations.


Furthermore, there were some special requests to manipulate the layout and make some rearrangements of the HTML elements. That page is basically a report/result for patients with data visualization, containing a lot of SVGs. Node.js is free of locks, so there's no chance to dead-lock any process., headless Chrome & Docker.īackground: A few months ago one of the clients of RisingStack asked us to develop a feature where the user would be able to request a React page in PDF format. In this article I’m going to show how you can generate a Puppeteer PDF document from a heavily styled React web page using Node.js Node.js is an asynchronous event-driven JavaScript runtime and is the most effective when building scalable network applications.
