n.s*_*ish 5 pdf-generation google-chrome-headless puppeteer
我正在致力于渲染网站的 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');
})();
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现它?请帮忙。
您所请求的功能没有“开箱即用”的木偶实现。
同时,有几种解决方法,如“能够阻止某些页面上的页眉/页脚”中所述。第一个建议是使用不同的范围进行多次转换并使用不同的页眉/页脚组合;将生成的缓冲区与https://github.com/astefanutti/decktape之类的内容合并后。还有另一个使用@page:firstCSS 样式的 hacky 解决方案。
| 归档时间: |
|
| 查看次数: |
10845 次 |
| 最近记录: |