To render and save a webpage as a PNG file using Puppeteer, you can use the following code snippet:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.example.com');
await page.screenshot({ path: 'example.png', fullPage: true });
await browser.close();
})();
In this code snippet:
puppeteer.launch()
launches a new browser instancebrowser.newPage()
creates a new page in the browserpage.goto()
navigates to the specified webpagepage.screenshot()
takes a screenshot of the page and saves it as a PNG file with the specified pathbrowser.close()
closes the browser instanceYou can customize the screenshot by specifying options like fullPage
to capture the entire page, or specifying the clip area via the clip
option.