gpt4 book ai didi

node.js - 使用 Playwright 制作 PDF 时控制页边距

转载 作者:行者123 更新时间:2023-12-05 05:00:07 30 4
gpt4 key购买 nike

当从 headless chrome Playwright session (下面的代码)制作 PDF 时,我在左侧获得 PDF,而如果我通过在 chrome 中保存为 pdf,我得到第二个输出。

enter image description here

我已经明确地设置了边距并使用了 preferCSSPageSize: true 并且两者都给出了相同的(左)结果。

我如何让 Playwright 提供与 chrome 打印对话框中相同的输出?

files being printed is here 的示例. (在现实生活中,它们都略有不同以解释书脊宽度。)

const fs = require("fs");
const path = require("path");
const { chromium } = require("playwright");

const directoryPath = "outside";

(async () => {
const filesToPrint = getFilesToPrintList(directoryPath);
filesToPrint.forEach((f, i) => console.log(i, f));
const browser = await chromium.launch({ headless: true });
const context = await browser.newContext();

for (let i = 0; i < filesToPrint.length; i++) {
const htmlFilename = path.join(directoryPath, filesToPrint[i]);
const pdfFilename = makePDFfilename(htmlFilename);
console.log("[html file]", htmlFilename);
console.log("[pdf file]", pdfFilename);
const page = await context.newPage();
await page.goto(
"file:///" + path.resolve(htmlFilename),
(waitUntil = "networkidle")
);
const options = {
path: pdfFilename,
pageRanges: "1",
preferCSSPageSize: true,
};
console.log(
`\n\nAbout to print:\n ${pdfFilename}\n`,
`with:\n${JSON.stringify(options, null, 2)}`
);
await page.pdf(options);
}
await browser.close();
console.log("done");
})();

function makePDFfilename(htmlFilename) {
const parts = htmlFilename.split(/[\\\._]/);
const pdfFilename = path.join(
directoryPath,
`Experimental_${parts[1]}_${parts[2]}.pdf`
);
return pdfFilename;
}

function getFilesToPrintList(directoryPath) {
let theFiles = fs
.readdirSync(directoryPath)
.filter((f) => f.includes("fp_") && f.includes(".html"));
return theFiles;
}

最佳答案

我试图重现您的问题:

我在您的 HTML 页面中使用了不同的背景图片 URL(在线),请参阅 test.html要点。

显然,许多选项都是默认值。参见 page.pdf() .

此外,添加了 browser.newContext()browser.close()

这是最简单的 NodeJS 代码:

生成-pdf.js

const { chromium } = require("playwright");

(async () => {
const htmlFilename = "file:///<file-path>/test.html";
console.log("[webpage]", htmlFilename);

const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto(htmlFilename);

const pdfFilename = "./test.pdf";
console.log("[pdf file]", pdfFilename);

const options = {
path: pdfFilename,
pageRanges: "1",
preferCSSPageSize: true
};

console.log(
`\n\nAbout to print:\n ${pdfFilename}\n`,
`with:\n${JSON.stringify(options, null, 2)}`
);

await page.pdf(options);
await browser.close();

console.log("done");
})();

控制台输出:

$ node generate-pdf.js
[webpage] file:///<file-path>/test.html
[pdf file] ./test.pdf


About to print:
./test.pdf
with:
{
"path": "./test.pdf",
"pageRanges": "1",
"preferCSSPageSize": true
}
done

PDF 快照(test.pdf):

test.pdf

您可能想单独测试它,看看它是否适用于您的用例。

关于node.js - 使用 Playwright 制作 PDF 时控制页边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63224034/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com