【问题标题】:Controlling margins when making a PDF using Playwright使用 Playwright 制作 PDF 时控制边距
【发布时间】:2020-11-23 04:49:41
【问题描述】:

从无头 chrome Playwright 会话(下面的代码)制作 PDF 时,我会在左侧获得 PDF,而如果我通过在 chrome 中保存为 pdf 来制作 PDF,则会获得第二个输出。

我已经明确设置了边距并使用了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;
}

【问题讨论】:

  • 我认为这个问题可以在 v1.12.3 上解决。我有类似的问题,但后来我意识到我使用的是旧版本的剧作家。使用 2021 年 7 月的最后一个版本,问题就消失了。

标签: node.js google-chrome chromium playwright css-print


【解决方案1】:

我试图重现您的问题:

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

显然,许多选项是默认值。见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):

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

【讨论】:

  • 我已经尝试过整合您的修改,但我仍然获得了利润。我已经更新了问题中的代码以反映这一点。我错过了什么重要的东西吗? (使用 Playwright 1.3.0,Chromium 86.0.4217.0)
  • @Ben:不太确定。可能存在一些与环境相关的问题。我正在使用 Chromimum 84.0.4147.105 (Official Build) (64-bit) 和 Playwright 版本是相同的 1.3.0。
  • @Ben:你可以尝试运行我的代码而不是集成吗?
  • 直接运行你的代码也会产生一个带边距的pdf,遗憾的是
  • @Ben:对。也许,您需要在另一台机器上进行测试。
猜你喜欢
  • 2014-08-01
  • 2018-01-07
  • 2019-08-05
  • 1970-01-01
  • 2014-10-07
  • 1970-01-01
  • 2020-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多