I want to use puppeteer
to generate pdf file by visiting my website. The problem is that while the screenshot
method works fine, I can’t produce pdf
file. This is my code:
async generatePdfByWisitingWebsite(user: UserEntity, data: InputGetPdfDto) {
const browser = await puppeteer.launch({ headless: true, protocolTimeout: 500000 });
const page = await browser.newPage();
await page.goto('http://localhost:3000/login', { waitUntil: 'networkidle2' });
await page.type('#email', '...');
await page.type('#password', '...');
await page.click('#loginButton');
await page.waitForNavigation({ waitUntil: 'networkidle2' });
await page.goto('http://localhost:3000/app/account', { waitUntil: 'networkidle2' });
await new Promise(function (resolve) {
setTimeout(resolve, 2000);
});
const content = await page.evaluate(() => document.body.innerHTML);
console.log(content);
await page.screenshot({ path: 'output/build1.png' });
await page.pdf({
path: 'output/test.pdf',
format: 'A4',
printBackground: true,
margin: {
top: '10mm',
right: '10mm',
bottom: '10mm',
left: '10mm',
},
timeout: 0,
});
await browser.close();
}
I’m watining for minutes and there is still no response. I tried with headless: false
and the page loads correctly so not sure what am I doing wrong. Any ideas?