NodeJS – How to convert an HTML to PDF using PhantomJS

Spread the post
  • 46

Problem Statement:-

How to convert an HTML to PDF using PhantomJS

PDF is Portable Document Format. This format is used to save files that cannot be modified but can be printed easily. PDF is explicitly used for exchanging electronic documents. 

PDF conversion is the most commonly used feature that allows documents from other formats to be converted to PDF easily. With the availability of many PDF converters, it is feasible to convert any document into a PDF. However, it is a challenge when you want to convert HTML into PDF while using NodeJS. Hence this blog will guide you to convert HTML to PDF seamlessly.

Description of the Issue:-

I was working on the back-end of a client application when I encountered this PDF conversion issue. I used HTML-PDF NPM to create a PDF from HTML. However, phantomJS module loading occurred first, and then I got stuck with the phantomPath issue, which terminated the PDF conversion process.

The Solution Offered:-

After extensive research, I figured out two different ways to convert an HTML into a PDF

         1. If the installation is done right and the linking is not done rightly. You can use the below query to add the link.

sudo npm link html-pdf

sudo npm link phantomjs-prebuilt

        2. If the first method did not solve your issue, follow the below-mentioned steps.

Remove the existing HTML-pdf and phantomJS module. Now reinstall them with a specific version you need and use the following query.

Sudo npm i html-pdf (command)

Sudo npm install  phantomjs-prebuilt@2.1.13 –unsafe-perm=true –allow-root

Technologies or frameworks or tools – NodeJS

NodeJS is an open-source runtime environment that uses JavaScript to code in front-end and back-end. Moreover, NodeJS uses a V8 engine that compiles JavaScript into native machine code directly, which leads to an increase in speed and efficient execution of the code. NodeJS also has an active community that is huge and includes a network of developers that contribute to several projects and help budding developers that are stuck while working in NodeJS.

Platforms (Web, Mobile, Etc.) – Web

Server or OS – Ubuntu

Soft Suave is the top web and mobile app development company in India that houses NodeJS developers from the top 1% of the talent pool in the world. The developers are highly-experienced, and their expertise extends to all front-end and back-end technologies. When you hire dedicated NodeJS developers from Soft Suave, you get developers who excel in communication, productivity and ensure client projects are delivered on time. Our developers never shy away to face complex challenges and strive to offer impeccable solutions at an affordable cost.

  • 46