google-chromepdfpuppeteerprinting-web-page

Height of white space above header in chrome while converting to pdf


What is the size of the white space visible above the header in the Chromium/Chrome when converting to pdf? Is it hardcoded somewhere in the source?

Space above header


Solution

  • The header is part of the margin of the page which can be specified when calling page.pdf. By default, this value is set to no margin.

    As an example: To make sure, there are 20mm of space at the top for your header, you can use the following code:

    await page.pdf({
        displayHeaderFooter: true,
        headerTemplate: '<div style="font-size:10mm;">Some header text</div>',
        margin: {
            top: '20mm',
            bottom: '0',
            right: '0',
            left: '0'
        },
        path: 'file.pdf',
    });
    

    In case you are interested in the default margins of Chrome to use them in puppeteer, you can find them in the print dialog, when you switch from "Default" to "Custom" (0.4 inch):

    Chrome print dialog