Convert HTML to PDF using Node.js

In this article, we will see how we will convert an HTML template to pdf in Node.js using a library called pdf-creator-node.

We can download the pdf from our folder itself.

Converting HTML to PDF in Node.js

We will use the pdf-creator-node module for converting. Enter the following in the terminal:

npm i pdf-creator-node

Then we will require the following module. The file system (fs) module in node.js will allow interaction with files in our computer system.

var pdf = require("pdf-creator-node");
var fs = require("fs");

Now we will create our HTML template, we will add images and style to check if that is also included in our pdf or not.

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pdf</title>
    
</head>
<style>
    body{
        background-color: red;
    }
    h1{
        color: white;
    }
 </style>
<body>
    <h1>Hello </h1>
    <img src="https://cdn.codespeedy.com/wp-content/themes/CodeSpeedy-March-2019/img/CodeSpeedy-Logo.png" alt="alternatetext">
    <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Et, natus! Deserunt amet magnam quas consequatur nemo asperiores et porro eveniet modi mollitia reiciendis magni praesentium corporis esse in, tempore quibusdam?
    </p>
</body>

</html>

 

Then we will read our HTML template, in fs.readFileSync we will pass the path of our HTML page and encoding,

var html = fs.readFileSync("index.html", "utf8");

Now we will set some parameters, for example

In options, we will set orientation, format, header, and footer for our pdf.

var options = {
    format: "A4",
    orientation: "portrait",
    border: "8mm",
    header: {
        height: "20mm",
    },
    footer: {
        height: "20mm",
        contents: {
            default:"This is footer."
        }
    }
};

In the next parameter document, we will provide Html, output path, data, and type. For data, we will either pass the data if we have it or pass an empty dictionary in case we don’t have data.

var document = {
    html: html,
    data: {},
    path: "./output.pdf",
    type: "",
  };

After the parameters are done, we call pdf.create method and pass these parameters. Then we will console log where the file is downloaded and in case an error occurs we will print the error.

pdf
  .create(document, options)
  .then((res) => {
    console.log(res);
  })
  .catch((error) => {
    console.error(error);
  });

Here is the complete code:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pdf</title>
    
</head>
<style>
    body{
        background-color: red;
    }
    h1{
        color: white;
    }
 </style>
<body>
    <h1>Hello </h1>
    <img src="https://cdn.codespeedy.com/wp-content/themes/CodeSpeedy-March-2019/img/CodeSpeedy-Logo.png" alt="alternatetext">
    <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Et, natus! Deserunt amet magnam quas consequatur nemo asperiores et porro eveniet modi mollitia reiciendis magni praesentium corporis esse in, tempore quibusdam?
    </p>
</body>

</html>

index.js

var pdf = require("pdf-creator-node");
var fs = require("fs");
var html = fs.readFileSync("index.html", "utf8");
var options = {
    format: "A4",
    orientation: "portrait",
    border: "8mm",
    header: {
        height: "20mm",
    },
    footer: {
        height: "20mm",
        contents: {
            default:"This is footer."
        }
    }
};
var document = {
    html: html,
    data: {},
    path: "./output.pdf",
    type: "",
  };
  pdf
  .create(document, options)
  .then((res) => {
    console.log(res);
  })
  .catch((error) => {
    console.error(error);
  });

Output:

In terminal:

{
filename: 'C:\\Users\\LENOVO\\Desktop\\HtmlPdf\\output.pdf'
}

In our Folder:

Convert HTML to PDF using Node.js

pdf-creator-node

This way we can convert our HTML template into pdf.

Leave a Reply

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