googleads
How To Convert HTML To PDF Using Node.js | HTML To PDF API
preloder
Technical Tips & Tricks Web Development

How To Convert HTML To PDF Using Node.js

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. Am using it to allow more options like paper size, header & footer, border type of styles into the pdf phantomjs 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.

Best To 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 postman to test this endpoint.

Here is the result.

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.
https://support.microsoft.com/en-in/help/314960/how-to-install-or-remove-a-font-in-windows

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

Like What You're Reading ?

Also Read: 10 App Ideas Using Javascript That Turned Into Million-Dollar Startups

The following two tabs change content below.

Reddy Sai

Young Senior Software Developer having 2+years of experience in Web development, skilled in Golang, Ruby, AngularJS, Ruby on Rails & NodeJS. With handful of skills he keep thriving through all the Edge of Web development to become a perfect full stack Golang Expert. Besides, this young techie will always buzz up & surprise us with his beautiful Music collections.

Leave a Reply

Your email address will not be published. Required fields are marked *

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call