最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to create PDF file in node js with data from mongodb? - Stack Overflow

matteradmin6PV0评论

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?

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?

Share Improve this question asked Feb 14, 2022 at 21:01 usuariousuario 11 silver badge1 bronze badge
Add a ment  | 

2 Answers 2

Reset to default 3

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

Last time I created a PDF with nodejs is quite some time ago but I used a npm package called PDFkit.

https://www.npmjs./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.

Post a comment

comment list (0)

  1. No comments so far