Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Header and footer content to pdfkit for node.js

I would like to generate a pdf using node js (express). I need to add header and footer to every page with page numbers. Any help would be appreciated.

Thanks.

like image 899
Akhil Gopan Avatar asked Mar 03 '17 06:03

Akhil Gopan


People also ask

How do I create a header and footer in Wkhtmltopdf?

Footers And Headers: Headers and footers can be added to the document by the --header-* and --footer* arguments respectively. In header and footer text string supplied to e.g. --header-left, the following variables will be substituted.

How do I use node JS Pdfkit?

Creating a PDF Document using PDFKit We'll pipe the contents of our PDF file into a fs 's writeable stream to save it. Let's take a look at how to do that: const PDFDocument = require('pdfkit'); const fs = require('fs'); let pdfDoc = new PDFDocument; pdfDoc. pipe(fs.

How do I create an invoice on Pdfkit?

To get started, use the following commands: git clone https://github.com/PSPDFKit-labs/pdfkit-invoice.git npm install # Install dependencies npm start # This will create an invoice. pdf file in the root of the project.


1 Answers

Adding a Footer on all pages

doc.addPage()

let bottom = doc.page.margins.bottom;
doc.page.margins.bottom = 0;
doc.text('Page 1', 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;

let pageNumber = 1;

doc.on('pageAdded', () => {
    pageNumber++
    let bottom = doc.page.margins.bottom;
    doc.page.margins.bottom = 0;

    doc.text(
        'Pág. ' + 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;
})
like image 61
jonathan gutierrez villamarin Avatar answered Nov 15 '22 11:11

jonathan gutierrez villamarin