If you're having trouble with the landscape option in page.pdf()
, there could be a few reasons why.
Firstly, remember that landscape orientation is only supported in Chromium headless. If you're using Firefox or WebKit, this option won't work.
Secondly, page.pdf()
uses print CSS media. If your CSS doesn't have rules for printing in landscape mode, the PDF will still be in portrait orientation. You can add this CSS rule:
@media print {
@page {
size: A4 landscape;
}
}
This sets the page size to A4 with a landscape orientation when printing. Apply this rule before calling page.pdf()
.
If you've called page.emulateMedia({ media: 'screen' })
before generating the PDF, it might override any attempts to set the page orientation using print media rules. You might need to remove or modify this line.
Lastly, other factors might affect how your PDF is rendered or printed. Some printers or PDF viewers may have their own default settings for page orientation which could override any settings specified in Playwright.
In short, if you're having issues with the landscape option in page.pdf()
, make sure you're using Chromium headless, include appropriate print media CSS rules for landscape orientation, and consider any other factors that might affect the PDF rendering or printing.
If you still have questions, please ask a question and I will try to answer it.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].