I'm creating an application in node.js. I need to create a report in PDF to show the data of my collection in the database. The data is fetched from mongodb. How can I do to show the data from my collection in the pdf?
2 Answers
The simplest way to generate PDFs using NodeJS is to use the pdf-master
package.
You can generate static and dynamic PDFs using HTML with one function call.
Just provide data fetched from MongoDB to generatePdf
function and it to HTML template.
Installation
npm install pdf-master
Example
Step 1 - Add required packages and generate a PDF
const express = require("express");
const pdfMaster = require("pdf-master");
const app = express();
app.get("", async (req, res) => {
var PDF = await pdfMaster.generatePdf("template.hbs");
res.contentType("application/pdf");
res.status(200).send(PDF);
});
generatePdf() syntax and parameters
generatePdf(
templatePath, //<string>
data, //<object> Pass data to template(optional)
options //<object> PDF format options(optional)
);
Step 2 - Create your HTML Template (save the template with .hbs extension instead of .html)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
Render dynamic data in template and PDF format options
const express = require("express");
const pdfMaster = require("pdf-master");
const app = express();
app.get("", async (req, res) => {
var students = {
{
id: 1,
name: "Sam",
age: 21
},
{
id: 2,
name: "Jhon",
age: 20
},
{
id: 3,
name: "Jim",
age: 24
}
}
let options = {
displayHeaderFooter: true,
format: "A4",
headerTemplate: `<h3> Header </h3>`,
footerTemplate: `<h3> Copyright 2023 </h3>`,
margin: { top: "80px", bottom: "100px" },
};
let PDF = await pdfMaster.generatePdf("template.hbs", students, options);
res.contentType("application/pdf");
res.status(200).send(PDF);
});
template for the above example (save the template with .hbs extension)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<h1>Student List</h1>
<ul>
{{#each students}}
<li>Name: {{this.name}}</li>
<li>Age: {{this.age}}</li>
<br />
{{/each}}
</ul>
</body>
</html>
To learn more about pdf-master
visit

- 321
- 1
- 9
-
can one add images to the PDF? – orimdominic Jan 10 '23 at 20:47
-
1Yes, add the image URL in your HTML template. Example -
. And for images from the image folder in your project, you have to configure static images in the express and then use that image path in your HTML template. – Chaitanya Mogal Jan 13 '23 at 04:49
Last time I created a PDF with nodejs is quite some time ago but I used a npm package called PDFkit.
https://www.npmjs.com/package/pdfkit
PDFkit is quite easy to learn and if you would like an example of how to make a datatable it is in the following stackoverflow question: HTML table in pdfkit (Expressjs-Nodejs)
and in the following stackoverflow question is an example on how to put json in your pdf: generate-pdf-from-json-array-objects-with-proper-tabular-format
I hope this helps you with generating a pdf of your data.

- 11
- 1
-
Hey thanks. put a json in a pdf works!! but it generates only a pdf and i need re run the application to generate another:( – usuario Feb 15 '22 at 01:00
-
To be honest for me it is to long ago to remember how I exactly did it, but it should be possible to create multiple pdf files while the application is running. – thijphil Feb 15 '22 at 09:09