Rayrun
← Back to Discord Forum

Landscape option in page.pdf not working

huangorino
huangorino

Hi I'm new to Playwright. I have done these step to save html to pdf in .net:

  • added pdf option Landscape = true
  • added @page { size = Landscape; } in css
  • set width and height in css

and the pdf saved still not in landscape mode. What did I miss?

image.png

This thread is trying to answer question "Why is the Landscape option in page.pdf not working despite setting it to true and adjusting the CSS in Playwright?"

0 replies

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.

Related Discord Threads

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 [email protected].