node-html-pdf
node-html-pdf copied to clipboard
Generate multiple pdf with the same " value replace "
I am using this library to generate pdf through pug and everything is perfectly first I generate the html and then I pass it to the library... and it works perfectly the question is how I could generate many pages with the same replacement value (but with other values ) as a massive impression

this is my code but i need create multiple times this pdf but the diferent values .
You have to use for loop or map for your data and write you replace code inside that for loop/map function.
Hi @MonsterDKing for multiple pdf you could use promise . I had json array object and from that I wanted to create multiple pdf with different data and after that i needed all those files inside zip folder . Check out the below code if it helps
const data = require('./sampleData.json');
var html = fs.readFileSync("template.html", "utf8");
// const base = path.resolve('./static').replace(/\\/g, '/');
let base = path.resolve('./static') + '/';
base = base.replace(new RegExp(/\\/g), '/');
var options = {
format: "A3",
orientation: "portrait",
localUrlAccess: true,
"base": "file:///" + base,
// border: "10mm",
header: {
height: '20px'
}
};
let promiseArrValue = data.map(function (resource) {
var users = resource;
var document = {
html: html,
data: {
users: users,
},
path: __dirname+'/zip-files/Providers/'+resource.chargeTo+'.pdf',
type: "",
};
// return the promise to array
return pdf.create(document, options).then(function (res) { //a Promise
//do something with the resource that has been modified with someFunction
return res;
})
});
Promise.all(promiseArr).then(function(resultsArray){
// do something after the loop finishes
const archiver = require('archiver');
// create a file to stream archive data to.
const output = fs.createWriteStream(__dirname + '/example.zip');
const archive = archiver('zip', { store: true })
output.on('close', function() {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
});
archive.on('warning', function(err) {
if (err.code === 'ENOENT') {
// log warning
} else {
// throw error
throw err;
}
});
archive.on('error', function(err) {
throw err;
});
archive.pipe(output);
archive.directory('./zip-files/Providers/', 'FM');
archive.finalize();
console.log(resultsArray, "final value");
}).catch(function(err){
// do something when any of the promises in array are rejected
console.log(err, 'error value');
})
I hope it helps :)