Generating a PDF file
After loading a page, we use thepage.pdf()
command to convert it to a PDF.
pdf-minimal.spec.ts
path
option to have the PDF file actually saved to disk.
⚠️ This feature is currently only supported in Chromium headless in Playwright.
Tweaking the result
It is important to take a quick look at the official docs forpage.pdf()
from Playwright, as it is almost certain that we will want to tweak the appearance of our page in the resulting PDF.
In certain cases, our webpage might look significantly different in our PDF compared to our browser. Depending on the case, it can pay off to experiment with the following:
- We might need to set option
printBackground
to true in case graphical components appear to be missing in the generated PDF. - By default,
page.pdf()
will generate a PDF with adjusted colors for printing. Setting the CSS property-webkit-print-color-adjust: exact
will force rendering of the original colors. - Calling
page.emulateMedia('screen')
changes the CSS media type of the page. - Setting either
width
andheight
orformat
to the appropriate value might be needed for the page to be displayed optimally.
Customising header and footer
We can also have custom headers and footers added to our pages, displaying values such as title, page number and more. Let’s see how this looks on your favourite website:pdf-hd.spec.ts
