使用 puppeteer 填写表单后,我得到了多个页面。我目前正在使用 puppeteer 的“page.printToPDF”api 来获取 pdf 格式的网页,但问题是我有多个页面,我想将所有页面合并起来并获得一个 pdf 格式。无论如何,我可以用 puppeteer 和 javascript 来实现这一点吗?
最佳答案
这里有一个替代解决方案,有很多用于合并 pdf 文件的包。
以下是如何使用众多 pdf 合并包之一。
const PDFMerge = require('pdf-merge');
const files = [
`${__dirname}/1.pdf`,
`${__dirname}/2.pdf`
];
const finalFile = `${__dirname}/final.pdf`;
以下是打印多页然后合并它们的方法。
// goto first page and save pdf file
await page.goto('http://example1.com', {waitUntil: 'networkidle'});
await page.pdf({path: files[0], format: 'A4', printBackground: true})
// goto first page and save pdf file
await page.goto('http://example2.com', {waitUntil: 'networkidle'});
await page.pdf({path: files[1], format: 'A4', printBackground: true})
// merge two of them and save to another file
await PDFMerge(files, {output: finalFile);
这完全取决于您如何利用您的资源。
关于javascript - 如何合并多个网页并将其转换为 pdf 格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48641218/