Created
April 5, 2019 18:44
-
-
Save learnwell/91ccbe15b7c874f4048e75a58e2522f5 to your computer and use it in GitHub Desktop.
pdfkit - add page numbers in footer while new pages are added automatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a document and pipe to a blob | |
var doc = new PDFDocument(); | |
var stream = doc.pipe(blobStream()); | |
doc.text('MY COVER PAGE'); | |
doc.text('i will not add a page number in the footer here'); | |
doc.text('please scroll down to see other pages'); | |
doc.text('they do have page numbers starting with 1'); | |
function footer(counter) { | |
doc.text(String(counter), doc.page.width / 2, doc.page.height - 50, { | |
lineBreak: false, | |
align: 'center' | |
}); | |
} | |
let pageCounter = 2; | |
doc.on('pageAdded', () => { | |
footer(pageCounter - 1); | |
pageCounter += 1; | |
/** | |
* HACK: reset the x and y back to the top of the page | |
* [72,72] is the default margin and we assume 12px is | |
* enough height for an empty line and the next line | |
* will start close to y=72. | |
* | |
* WHY? Otherwise pdfkit's autolayout mechanism will | |
* try to add lines without specific (x,y) coordinates | |
* AFTER the footer content, which is not what we want. | |
*/ | |
doc.text(' ', 72, 72-12); // empty line for resetting x & y | |
}); | |
doc.addPage(); | |
doc.text('hello'); | |
doc.text(lorem, { | |
columns: 3, | |
columnGap: 15, | |
height: 100, | |
width: 465, | |
align: 'justify' | |
}).moveDown(7); | |
doc.text('i was moved down by 7 by the preceding command').moveUp(5); | |
doc.text('i was moved UP by 5 by the preceding command', 200).moveUp(3); | |
doc.restore(); | |
doc.text('i was moved UP by 3 by the preceding command').moveUp(1); | |
doc.text('i was moved UP by 1 by the preceding command').moveDown(11); | |
doc.text('1'); | |
doc.text(' 2'); | |
doc.text(' 3'); | |
doc.text(' 4'); | |
doc.text(' 5'); | |
doc.text(' 6'); | |
doc.text(' 5'); | |
doc.text(' 4'); | |
doc.text(' 3'); | |
doc.text(' 2'); | |
doc.text('1'); | |
doc.text('1'); | |
doc.text(' 2'); | |
doc.text(' 3'); | |
doc.text(' 4'); | |
doc.text(' 5').moveUp(5); | |
doc.text(' 5'); | |
doc.text(' 4'); | |
doc.text(' 3'); | |
doc.text(' 2'); | |
doc.text('1'); | |
doc.text('1', 5, null, {}); | |
doc.text(' 2', 5, null, {}); | |
doc.text(' 3', 5, null, {}); | |
doc.text(' 4', 5, null, {}); | |
doc.text(' 5', 5, null, {}).moveUp(5); | |
doc.text(' 5', 5, null, {}); | |
doc.text(' 4', 5, null, {}); | |
doc.text(' 3', 5, null, {}); | |
doc.text(' 2', 5, null, {}); | |
doc.text('1', 5, null, {}); | |
doc.text('1', 5, null, {}); | |
doc.text(' 2', 5, null, {}); | |
doc.text(' 3', 5, null, {}); | |
doc.text(' 4', 5, null, {}); | |
doc.text(' 5', 5, null, {}).moveUp(5); | |
doc.text(' 5', 5, null, {}); | |
doc.text(' 4', 5, null, {}); | |
doc.text(' 3', 5, null, {}); | |
doc.text(' 2', 5, null, {}); | |
doc.text('1', 5, null, {}); | |
doc.text('1').moveUp(); | |
doc.text('2'); | |
doc.text('1'); | |
doc.text(' 2'); | |
doc.text(' 3'); | |
doc.text(' 4'); | |
doc.text(' 5'); | |
doc.text(' 6'); | |
doc.text(' 5'); | |
doc.text(' 4'); | |
doc.text(' 3'); | |
doc.text(' 2'); | |
doc.text('1'); | |
doc.text('1'); | |
doc.text(' 2'); | |
doc.text(' 3'); | |
doc.text(' 4'); | |
doc.text(' 5').moveUp(5); | |
doc.text(' 5'); | |
doc.text(' 4'); | |
doc.text(' 3'); | |
doc.text(' 2'); | |
doc.text('1'); | |
doc.text('1', 5, null, {}); | |
doc.text(' 2', 5, null, {}); | |
doc.text('1 1afgksfbdkgjbsd .sl/cg',50).moveUp(); | |
doc.text('lsbfljgnsd/lfgnbkl/sdfng /lbsdnfbg/nsdfkl/lsbfljgnsd/lfgnbkl/sdfng /lbsdnfbg/nsdfkl/lsbfljgnsd/lfgnbkl/sdfng /lbsdnfbg/nsdfkl/',200,undefined,{ align: 'justify'}); | |
doc.text('2 1afgksfbdkgjbsd .sl/cg',50).moveUp(); | |
doc.text('lsbfljgnsd/lfgnbkl/sdfng /lbsdnfbg/nsdfkl/lsbfljgnsd/lfgnbkl/sdfng /lbsdnfbg/nsdfkl/lsbfljgnsd/lfgnbkl/sdfng /lbsdnfbg/nsdfkl/',200,undefined,{ align: 'justify'}); | |
doc.text('3 1afgksfbdkgjbsd .sl/cg',50).moveUp(); | |
doc.text('lsbfljgnsd/lfgnbkl/sdfng /lbsdnfbg/nsdfkl/lsbfljgnsd/lfgnbkl/sdfng /lbsdnfbg/nsdfkl/lsbfljgnsd/lfgnbkl/sdfng /lbsdnfbg/nsdfkl/',200,undefined,{ align: 'justify'}); | |
doc.text(' 3', 5, null, {}); | |
doc.text(' 4', 5, null, {}); | |
doc.text(' 5', 5, null, {}).moveUp(5); | |
doc.text(' 5', 5, null, {}); | |
doc.text(' 4', 5, null, {}); | |
doc.text(' 3', 5, null, {}); | |
doc.text(' 2', 5, null, {}); | |
doc.text('1', 5, null, {}); | |
doc.text('1', 5, null, {}); | |
doc.text(' 2', 5, null, {}); | |
doc.text(' 3', 5, null, {}); | |
doc.text(' 4', 5, null, {}); | |
doc.text(' 5', 5, null, {}).moveUp(5); | |
doc.text(' 5', 5, null, {}); | |
doc.text(' 4', 5, null, {}); | |
doc.text(' 3', 5, null, {}); | |
doc.text(' 2', 5, null, {}); | |
doc.text('1', 5, null, {}); | |
doc.text('1afgksfbdkgjbsd .sl/cg lxnglvljfg bljab lsbfljgnsd/lfgnbkl/sdfng /lbsdnfbg/nsdfkl/',200,undefined,{ align: 'justify'}); | |
doc.text('hello', 10); | |
doc.text(lorem, { | |
columns: 3, | |
columnGap: 15, | |
height: 100, | |
width: 465, | |
align: 'justify' | |
}).moveDown(7); | |
doc.text('hello'); | |
doc.text(lorem, { | |
columns: 3, | |
columnGap: 15, | |
height: 100, | |
width: 465, | |
align: 'justify' | |
}).moveDown(7); | |
// end and display the document in the iframe to the right | |
doc.end(); | |
stream.on('finish', function() { | |
iframe.src = stream.toBlobURL('application/pdf'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment