If you're looking to utilize Puppeteer for generating PDFs, you can refer to the npm package here.
For generating PDFs from a specified URL:
const puppeteer = require('puppeteer');
async function createPDFfromURL(url, outputPath) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
await page.pdf({ path: outputPath, format: 'A4' });
await browser.close();
}
// Example
createPDFfromURL('https://example.com', 'example.pdf')
.then(() => console.log('PDF generated successfully'))
.catch(err => console.error('Error generating PDF:', err));
If you want to generate PDFs from custom HTML content:
const puppeteer = require('puppeteer');
async function createPDFfromHTML(htmlContent, outputPath) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(htmlContent);
await page.pdf({ path: outputPath, format: 'A4' });
await browser.close();
}
// Example
const htmlContent = '<h1>Greetings</h1><p>This is some custom HTML.</p>';
createPDFfromHTML(htmlContent, 'custom.pdf')
.then(() => console.log('PDF generated successfully'))
.catch(err => console.error('Error generating PDF:', err));