0

How can I set page header and footer using phantomjs with node, basically I'm generating pdf from html and I'm willing to add my header and footer from node, I have tries with following but pdf is not showing any data, and I'm reading empty page and willing to add header and footer, here is my Code:

// Load ejs template
fs.readFile(__dirname + '/../pdf' + pdfpath, 'utf8', function (err, data) {

  // Render the page to variable.
  var html = ejs.render(data, pdfJSON);

  // Set this html as the content for the pdf file.

  page.set('content', html);

  page.set('generatePDF', function (pageNum, numPages) {

    if (pageNum == 1) {
      return "";
    }
    return "<h1>Header <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";
  });

  page.set('paperSize', {
    width: 1200,
    height: 1500,
    header: {
      height: "1cm",
      contents: phantom.generatePDF
    }
  });

  console.log(phantom.generatePDF);//return undifned

  page.set('paperSize', {
    width: 1200,
    height: 1500
  });

  // Generate the pdf.
  var fileName = __dirname + '/pdfdata/' + f.formType + f.formId + '.pdf';
  page.render(fileName, cb);

});

How I can resolve this?

Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
Muhammad Rashid
  • 219
  • 1
  • 6
  • 19

1 Answers1

1

Take a look to these example:

https://github.com/ariya/phantomjs/blob/master/examples/printheaderfooter.js

Edit: Sorry, didn't noticed you asked for Node, you can find some solutions and answers here also:

Footer's contents don't seem to work

Community
  • 1
  • 1
  • 2
    Link only answers are discouraged, as they often break. I would recommend editing your answer to be self-sufficient. – Chris May 09 '14 at 21:12
  • For other folks getting here. In 2021 the first link does not work using phantomjs – aafirvida Nov 18 '21 at 23:02