用于 PDF 渲染的 Puppeteer 自定义页眉页脚

Puppeteer custom header footer for PDF render

我正在处理网站的 PDF 渲染。我希望第一页的页眉和页脚不同,其余页面也不同。有什么办法吗?

const puppeteer = require('puppeteer');

(async() => {
 var t = Date.now();
 console.log('Current time ' + t + ' msec'); 

 const browser = await puppeteer.launch({executablePath: 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe', ignoreHTTPSErrors:true, headless:true, devtools:false});
const page = await browser.newPage();
await page.setViewport({width: 1920, height: 1080});

await page.setExtraHTTPHeaders({'Report-Print-Preview-Mode':'true'});
await page.goto('https://localhost', {waitUntil: 'networkidle2'});
await page.type('#username', 'scott');
await page.type('#password', 'tiger');  
await page.click('[id="login_button"]');

await page.waitForSelector('[id="new_page_table_id"]');
await page.on('console', msg => console.log('PAGE LOG:', msg.text()));

// page.pdf() is currently supported only in headless mode.
// @see https://bugs.chromium.org/p/chromium/issues/detail?id=753118

await page.pdf({
path: 'report.pdf',
displayHeaderFooter: true,
headerTemplate: '<span style="font-size: 30px; width: 50px; height: 50px; background-color: black; color:black; margin: 20px;">Header</span>', 
footerTemplate: '<span style="font-size: 30px; width: 50px; height: 50px; background-color: black; color:black; margin: 20px;">Footer</span>',
printBackground: true,
format: 'A4',
margin:{top:'2cm',right:'2cm',bottom:'2cm',left:'2cm'}
});

await browser.close();

t = Date.now() - t;
console.log('Execution time ' + t + ' msec');
})();

如何实现?请帮忙。

您请求的功能没有 "out of box" 人偶执行。

与此同时,在“Ability to prevent header/footer on certain pages". The first suggestion is to convert multiple times with different range and use of different header/footer combos; after merge resulting buffers with something like https://github.com/astefanutti/decktape”中描述了几种变通方法。还有另一个使用 @page:first CSS 风格的 hacky 解决方案。