

Notice: the code provided is very simple and does not contemplate any error handling, so please beaware of that if you plan to use it in a production set up.
NODEJS PDFKIT HTML TO PDF PDF
Now start your server, and visit, for example: There you go, an API that takes a target URL and renders a PDF version of it. The next step is to write code to open up a browser and load a website:Īwait page. That installation with download among other things a Chromium browser into your node modules folder, so be patient, it may take a minute or two more than a regular package.
NODEJS PDFKIT HTML TO PDF INSTALL
NODEJS PDFKIT HTML TO PDF HOW TO
In that sense, a headless browser is simply just another browser that understands how to render HTML web pages and process JavaScript.

If you are unfamiliar with the term headless browsers, it’s simply a browser without a GUI. Is, “A Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol”. What Is Puppeteer, and Why Is It Awesome? ) that deal with PDF generation, but those libraries are often hard to use and involve a ton of steps, and APIs we are not familiar with.įortunately, there is an easier way, which is to convert web pages into PDF format, and that can be done with the help of a tool called Puppeteer.

When we are faced with such a task our intuition is to google “JavaScript create pdf” and we get awesome SDKs (like PDFKit A common request I have seen in my career, especially when building web applications, is to generate PDF documents that users of the app can download. Writing a Puppeteer PDF API with ExpressĪs web developers, we love working with HTML, CSS, and JavaScript, but when we are working on projects, sometimes we are asked to solve problems that involve other technologies.Using Puppeteer to convert a web page into a PDF file.What is Puppeteer, and why is it awesome?.Just call the API and pass in the PDF URLs to join them into a single file and receive the generated result via webhook or via polling. You can use the API to join your PDFs that are generated using Puppeteer too if they are hosted on a server. Bannerbear has an API to join PDF files when you generate PDFs from templates using Bannerbear’s PDF generation API. If you have multiple PDFs and want to join them into a single PDF, here’s an easy method to do so. to generate PDFs, so feel free to play around with the code. You can also modify the code to pass in different arguments to the function, eg. Margin: ,ġ-Run node index.js or node index.js url to convert the HTML content of a web page using its URL into a PDF.Ģ-Run node index.js file to convert the content of an HTML file into a PDF.
