To save a webpage as a PDF using Puppeteer, you can follow these steps:
npm install puppeteer
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.example.com', { waitUntil: 'networkidle2' });
await page.pdf({ path: 'example.pdf', format: 'A4' });
await browser.close();
})();
node savePageAsPDF.js
This will open a new instance of a headless Chrome browser using Puppeteer, navigate to the specified webpage, take a screenshot, save it as a PDF file named 'example.pdf', and then close the browser.
You can customize the PDF output further by specifying options such as page size, margins, and so on in the page.pdf()
method. Check out the Puppeteer documentation for more details on available options.