1

I am new in node js, and I want to use different header and footer for first and last pages in node js,

I am using html-pdf module which is not working when I use the same code as they have provided.

please have a look into the code

var fs = require('fs');
var pdf = require('html-pdf');
var html = fs.readFileSync('test.html', 'utf8');
var options = { format: 'Letter' };

pdf.create(html, options).toFile('businesscard.pdf', function(err, res) {
  if (err) return console.log(err);
  console.log(res); // { filename: '/app/businesscard.pdf' }
});

Reference Link: https://www.npmjs.com/package/html-pdf

When I am using below code It will not work on the last page only It also print for middles pages.

<div id="pageHeader-last">Header on last page</div>
...
<div id="pageFooter-last">Footer on last page</div>
Ravi Gupta
  • 13
  • 1
  • 3

2 Answers2

2

you can use html-pdf for node https://www.npmjs.com/package/html-pdf It is working in the html-pdf version 2.1.0 you can install it via npm install html-pdf@2.1.0 There is some issue with latest version (2.2.0)

Abhinav bhardwaj
  • 2,205
  • 19
  • 21
1

html-pdf is basically a wrapper for phantomJS that is very convenient for certain pdf generation. It's worth looking under the hood if you're going to use it much. There are limits on how much you can adjust the header and footer from the javascript side, for example, without modifying the script that node-html-pdf uses or calling phantomJS on your own.

Sometimes it is easier to assemble a PDF from smaller PDF files using node-pdftk than it is to coax node-html-pdf or phantomJS into doing what you want in one pass.

html-pdf sets the header and footer in pdf_a4_portrait.js

function createSection (section, content, options) {
...
      if (pageNumFinal === 1 && !html) html = o.first || c.first
      if (numPagesFinal === numPages && !html) html = o.last || c.last
      return (html || o.default || c.default || '')
        .replace(/{{page}}/g, pageNumFinal)
        .replace(/{{pages}}/g, numPagesFinal) + content.styles
...
}
Tom
  • 909
  • 5
  • 9