3

When making a PDF from a headless chrome Playwright session (code below) I get the PDF on the left, whereas if I make it through the save to pdf in chrome, I get the second output.

enter image description here

I have set the margins explicitly and used preferCSSPageSize: true and both give the same (left) outcome.

How would I get Playwright to give me the same output as chrome does from the print dialog?

An example of the files being printed is here. (In real life, they're all slightly different to account for the spine width.)

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;
}
Ben
  • 12,614
  • 4
  • 37
  • 69
  • 1
    I think the issue could be solved on v1.12.3. I had a similar problem, but then I realised I was using an old version of playwright. With the last version as in july 2021, the problem disappear. – f-spin Jul 01 '21 at 07:34

1 Answers1

2

I tried to reproduce your issue:

I used a different background image URL (online) with your HTML page, see test.html gist.

Apparently, a number of options are the defaults. See page.pdf().

Also, added browser.newContext() and browser.close().

Here's the minimal working NodeJS code:

generate-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");
})();

Console Output:

$ 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

Snapshot of PDF (test.pdf):

test.pdf

You might want to test this separately and see if it works for your use-case.

Azeem
  • 11,148
  • 4
  • 27
  • 40
  • I've tried integrating your modifications, but I still get the margin. I've updated the code in the question to reflect this. Am I missing something significant? (Using Playwright 1.3.0, Chromium 86.0.4217.0) – Ben Aug 13 '20 at 05:42
  • @Ben: Not really sure. There might be some environment-related issues. I'm using Chromimum 84.0.4147.105 (Official Build) (64-bit) and Playwright version is the same 1.3.0. – Azeem Aug 13 '20 at 05:53
  • @Ben: Instead of integration, can you please try to run my code? – Azeem Aug 13 '20 at 05:55
  • running your code directly also produces a pdf with margins, sadly – Ben Aug 13 '20 at 05:59
  • @Ben: Right. Maybe, you need to test on a different machine. – Azeem Aug 13 '20 at 06:05