-
Notifications
You must be signed in to change notification settings - Fork 9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Downloading PDF files using Puppeteer #8010
Comments
I believe it should be possible to use CDP's setDownloadBehaviour https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-setDownloadBehavior but there is no dedicated API in Puppeteer for this yet:
|
This allows us to set the download path while downloading the files. PDFs will open in the tab itself in this case as well. |
I see. It looks like puppeteer-extra writes those settings into the user profile directory and to me it looks like the approach might be a bit fragile if the structure of the directory changes. But I am not aware of any other way to achieve this. Most likely, it's possible to replicate what puppeteer-extra does and create the profile with the right setting but I think using puppeteer-extra is fine too. |
Really surprised puppeteer still doesn't support this. Playwright does. |
I had to twick a lot till i found this solution 😄 And Downloads the pdf automatically which means you do not need to do anything else // WAITS TILL THE OBJECT IS RENDERED
await page.waitForSelector('SELECTOR FOR PDF OBJECT')
//SET DOWNLOAD PATH
await page._client.send("Page.setDownloadBehavior", {
behavior: "allow",
downloadPath: PATH,
})
await page.evaluate(() => {
const pdfObj = document.querySelector("SELECTOR FOR PDF OBJECT")
const btn = document.createElement("a")
btn.setAttribute("href", pdfObj.data)
btn.setAttribute("download","FILE_NAME")
btn.click()
}) |
We're marking this issue as unconfirmed because it has not had recent activity and we weren't able to confirm it yet. It will be closed if no further activity occurs within the next 30 days. |
We will definitely need a better API for this. |
This is also not a perfect solution, since you should technically use |
I am trying to download PDF files using puppeteer, when .pdf URLs are hit the chrome opens the pdf in the tab, instead I want the pdf to be downloaded. This can be achieved through changing the user-preferences of browser and Sites automatically follow this setting when you visit them by default it is Open PDFs in Chrome.
I found that we can use puppeteer-extra for downloading the PDF files. Is that a legit NPM which I could use in my project instead of puppeteer-core ?
Is there any alternatives available from Puppeteer itself ?
The text was updated successfully, but these errors were encountered: