I am trying to download a pdf upon clicking a button. But I am not able to download it as it opens in new tab and not downloading it.
Following Solutions I have tried, but nothing seems to be working. Please help me .
1) Listening to the targetcreated
, and coverting the page to pdf
browser.on('targetcreated', async (target) => {
if (target.type() !== 'page') {
return;
} else {
let newPage = await target.page();
await page.waitFor(5 * 1000); // To stop proceeding to other clicks in current page
await newPage.waitFor(5 * 1000); // To let it load the pdf
newPage.pdf({path: "./pdfDownloaded/" + new Date().getTime() + ".pdf"})
}
});
Solution did not work
as converted pdf is blank
2) Setting flag always_open_pdf_externally: true
puppeteer.use(require('puppeteer-extra-plugin-user-preferences')({userPrefs: {
download: {
prompt_for_download: false,
},
plugins: {
always_open_pdf_externally: true
}
}}))
Solution did not work
as Chrome/Chromium crashed with always_open_pdf_externally: true
3) Giving print() in page evaluate, and as the print preview opens find the save
button and click .
Solution did not work
as it opens prompt asking location to save pdf and Puppeteer has no access to the Operating System driven prompt