Is there a way to generate page number using this tool ?
You can do something like this
var doc = new PDFDocument({autoFirstPage:false});
var pageNumber = 0;
doc.on('pageAdded',
function(){
// Don't forget the reset the font family, size & color if needed
doc.text(++pageNumber, 0.5 * (doc.page.width - 100), 40, {width: 100, align: 'center'});
}
);
doc.addPage();
When using this to add page numbers to a footer, I've had it stack overflow because new pages are continually added due to text positioning.
You can work around this by clearing the margins and then resetting them after writing your text.
// Page number footer
(function () {
let pageNumber = 0;
doc.on('pageAdded', () => {
pageNumber++;
let bottom = doc.page.margins.bottom;
doc.page.margins.bottom = 0;
doc.text(`Page ${pageNumber}`,
0.5 * (doc.page.width - 100),
doc.page.height - 50,
{
width: 100,
align: 'center',
lineBreak: false,
});
// Reset text writer position
doc.text('', 50, 50);
doc.page.margins.bottom = bottom;
});
})();
Most helpful comment
When using this to add page numbers to a footer, I've had it stack overflow because new pages are continually added due to text positioning.
You can work around this by clearing the margins and then resetting them after writing your text.