puppeteer: While generating pdf, landscape option doesn't work

Steps to reproduce

Tell us about your environment: We are trying to generate pdf using puppeteer. Previously we had the landscape option working on Puppeteer 1.5.0 and node 8.9.4. But, after we moved around the code and updated the node and puppeteer versions to 8.15.0 and 1.11.1 respectively, the landscape option tends to generate a portrait instead of a landscape.

  • Puppeteer version: 1.11.1
  • Platform / OS version: ubuntu
  • URLs (if applicable):
  • Node.js version: 8.15.0

Please include code that reproduces the issue.

function generatePDF(html, options) { // options has landscape param set to true or false browser = await puppeteer.launch({ args: this.puppeteerArgs }); page = await browser.newPage(); const session = await page.target().createCDPSession();

  await session.send('DOM.enable');
  await session.send('CSS.enable');
  await page.goto(`data:text/html,${html}`, { waitUntil: 'networkidle2' });

  this.logger.debug('generatePDF - creating pdf buffer..');
  const buffer = await page.pdf(options);
  await page.close();
  await browser.close();
  return buffer;

} What is the expected result? generates a landscape view

What happens instead? generates a portrait view

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 12
  • Comments: 16

Most upvoted comments

Have you tried altering the CSS rule @page?

@page {
    size: auto;
}

Edit: You can use the CSS rule only for Puppeteer:

page.addStyleTag(
    {'content': '@page {size: auto}'}
)

I’m using PuppeteerSharp v1.20.0. I confirm that landscape option doesn’t work. Explicitly setting page width & height didn’t work also. However, using page.addStyleTag worked for me. You can try page.addStyleTag( {‘content’: ‘@page { size: A4 landscape; }’} )

Seems to be working fine for me. The code that I used:

const browser = await puppeteer.launch();
const page = await browser.newPage();
const options = {
  path: 'pdf/filename.pdf',
  format: 'A0',
  printBackground: true,
  landscape: true,
};

await page.goto('https://somewebsite.com', { waitUntil: 'networkidle2' });
await page.pdf(options);
await browser.close();

This generates a landscape oriented PDF for me. If I leave out the landscape: true in the options const, I get a portrait oriented PDF.

What I noticed is, that if you provide width and height in the options, the landscape is not taken into account. However, when using format, the orientation is working just fine 😃

Versions:

  • Puppeteer: 5.2.1
  • Node: 13.7.0 (express back-end)
  • Node Express: 4.16.1
  • Platform: macOS Catalina (10.15.5)

We had that issue today, but we got it working by calling await page.emulateMedia('screen'); before generating the PDF. After that, landscape : true was working.

The same problem here, landscape: true is not working…

I’m having the same issue with:

  • Puppeteer version: 1.19.0
  • Platform / OS version: Linux (Fedora 30)
  • Node.js version: 10.16.0

Even if I set the landscape option to true, page.pdf still generates a portrait oriented PDF.

Good morning, everyone

CSS

@page {
    size: A4 landscape !important;
}

Node.js

const browser = await Puppeteer.launch({ headless: 'new' });
const page = await browser.newPage();
await page.setContent(HTML, { waitUntil: 'domcontentloaded' });
await page.emulateMediaType('screen');
const pdf = await page.pdf({
    path: pdf_filename,
    margin: { top: '0px', right: '0px', bottom: '0px', left: '0px' },
    printBackground: true,
    format: 'A4',
    landscape: true
});
await browser.close();

I hope this helps.

+1 same here, landscape not working.