An image

Corporate Blogs

How To Convert HTML To PDF Using Node.js?

E-Commerce Businesses Need a Mobile App

Hello folks, today we’re going to check how to convert HTML to PDF content in Node.js and also few deployment tips to get it done right.

I planned to describe the conversion through Node API service using html-pdf. This npm package depending on phantomjs. I am using it to allow more options like paper size, header & footer, border type of styles into the pdf phantom JS helping internally.

Installing NPM Dependencies And Packages

To install the npm packages, use the below command. I’m giving an example with dynamic content which will help us in real-time. To give an example of PDF conversion, I’m just going with one sample certificate template and this template allows dynamic content.

Filename: certificate.ejs

NOTE: To avoid font size issues while generating PDF, go with pt(point) size format instead of px(pixel).

Read A File In Node.js Using Node FS

Using fs npm package, you can read the content from the ejs file. Here it is.

Display Dynamic Content On HTML Page Using Node ejs

In real time, mostly we cannot maintain the static templates. Using ejs npm package we can update the part of HTML content or we can bind the dynamic values into the existing content.

Also Read

Build A Node.js API In 30 Minutes

Convert HTML To PDF Using Node.js

Now we have updated HTML content with dynamic values, with this content we can able to generate the pdf using the html-pdf npm package.

Using certificate.ejs file, here we are generating the PDF with a complete example. Use the below source to do that.

From this example, the generated PDF will respond in fly mode.

To test this endpoint, we have to pass the request body content like,

I used a postman to test this endpoint.

Here is the result.

html to pdf using Node.js

Install Custom Fonts In Ubuntu

By chance, If you have used custom fonts to generate PDF, then those fonts won’t render properly in server-side. To avoid this problem, you must install the custom fonts locally on production machine(server) or copy the font files into the /usr/share/fonts/ and /usr/local/share/fonts/ directories.

When the pdf is generated on the server then you may notice that the spacing between letter will get vary, to overcome this problem, you have to override this file /etc/fonts/conf.d/51-local.conf in your production machine with below content.

Similarly, you can follow this below link if you want to install the custom font in Windows.

Hope this will help you to work as you expected from production with any font relevant issues.

Turn your vision to magnificent reality With
Our Web and Mobile Solutions
Professional Life of an IT Consultant Interview with Valentin Crettaz
Written by
Young Senior Software Developer having 4+years of experience in Web development, skilled in Golang, NodeJS, Ruby, AngularJS, Ruby on Rails & AWS services. With a handful of skills, he keeps thriving through all the Edge of Web development to become a perfect back end developer. Besides, this young techie will always buzz up & surprise us with his beautiful Movie collections.

Leave a Comment

Rate this Blog!