marcbachmann / node-html-pdf

This repo isn't maintained anymore as phantomjs got dreprecated a long time ago. Please migrate to headless chrome/puppeteer.
MIT License
3.55k stars 544 forks source link

Generate multiple pdf with the same " value replace " #664

Open MonsterDKing opened 2 years ago

MonsterDKing commented 2 years ago

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

image

this is my code but i need create multiple times this pdf but the diferent values .

urjit2498 commented 2 years ago

You have to use for loop or map for your data and write you replace code inside that for loop/map function.

chan1di commented 1 year ago

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 :)