How to set screenshot size in Puppeteer?
Problem:
You are trying to make a screenshot using Puppeteer like this:
await page.screenshot({path: 'screenshot.png'});
but the screenshot is always 800x600 pixels. How can you increase the size of the screenshot?
Solution:
You can’t set the size for the screenshot, you need to set the size for the viewport in puppeteer.launch
:
const browser = await puppeteer.launch({
defaultViewport: {width: 1920, height: 1080}
});
Full example to produce a 1920x1080
screenshot:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
defaultViewport: {width: 1920, height: 1080}
});
const page = await browser.newPage();
await page.goto('https://techoverflow.net', {waitUntil: 'domcontentloaded'});
// Wait until page has loaded completely
// Make a screenshot
await page.screenshot({path: 'screenshot.png'});
await browser.close();
})();