My database contains 600000 rows. I have to read the data from Database page wise(1000 records par page). Then generate the pdf on the fly and stream it as downloadable pdf using node.js
Asked
Active
Viewed 2,082 times
2 Answers
1
I have done it in a sample Hello world code.
Idea from the link: Serve dynamically generated PDF with remote images in Node.js
Here is my code.
var express = require('express'),
request = require('request'),
expressJSON = require('express-json'),
pdfDocument = require('pdfkit');
// Start Express
var app = express();
// Use JSON in POST body
app.use(expressJSON());
// Setup POST response
app.get('/post_pdf', function(req, res) {
// Create PDF
var doc = new pdfDocument();
// Write headers
res.writeHead(200, {
'Content-Type': 'application/pdf',
'Access-Control-Allow-Origin': '*',
'Content-Disposition': 'attachment; filename=Untitled.pdf'
});
// Pipe generated PDF into response
doc.pipe(res);
// Process image
request({
url: 'http://dummyimage.com/640.jpeg',
encoding: null // Prevents Request from converting response to string
}, function(err, response, body) {
if (err) throw err;
var idx =0;
var fource2Stream = function(){
idx++;
if(idx >= 10) {
doc.end(); // Close document and, by extension, response
return;
}
doc.addPage();
// Inject image
doc.image(body); // `body` is a Buffer because we told Request
// to not make it a string
doc.addPage();
doc.text ('Hello world!', 100, 100);
doc.addPage();
doc.text('Hello world!', 100, 100);
doc.flushPages();
setTimeout(fource2Stream, 5000);
};
fource2Stream();
return;
});
});
app.listen(8080);
0
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 to generatePdf
function. You can also design PDFs with HTML and CSS.
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

Chaitanya Mogal
- 321
- 1
- 9