Rayrun

What are the possible reasons for the Landscape option in page.pdf not working in Playwright despite setting it to true and adjusting the CSS?

Answer

Landscape Option in Playwright's page.pdf()

If you're having trouble with the landscape option in page.pdf(), there could be a few reasons why.

Browser Engine

Firstly, remember that landscape orientation is only supported in Chromium headless. If you're using Firefox or WebKit, this option won't work.

CSS Styles

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().

EmulateMedia

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.

Other Factors

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.

References

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Discord Threads

Related Questions

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

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 luc@ray.run.