Hi!
On creation of a browser or context (permanent or otherwise), is it possible to set the config value AlwaysOpenPdfExternally or the equivalent plugins.always_open_pdf_externally preference?
I've tried a kludgy solution that has me navigate to chrome://settings/content/pdfDocuments on load and click the option. But we need to use Chromium in headless to export PDFs of pages as well as download PDF files that the browser always opens in its embedded viewer… and there seems to be no way of getting the actual PDF content out of that viewer.
Any help would be appreciated.
Cheers, Wayne
OK, after many hours of searching in the last 48 hours for a solution to this, I think I've managed to discover something soon after posting here that works.
Would anyone mind taking a look at this solution and seeing if it's going to cause me any unexpected problems?
For each run:
const tmpDir = fs.mkdtempSync(`/tmp/pwtest`);
fs.mkdirSync(`${tmpDir}/userdir/Default`, { recursive: true });
const defaultPreferences = {
plugins: {
always_open_pdf_externally: true,
},
}
fs.writeFileSync(`${tmpDir}/userdir/Default/Preferences`, JSON.stringify(defaultPreferences));
const context = await chromium.launchPersistentContext(`${basePath}/userdir`, { acceptDownloads: true });
This seems to work and forces the browser to download files vs displaying them in the in-browser PDF viewer. I just want to make sure I'm not shooting myself in the foot by only having a tiny preferences file to start (although it gets filled-in on first start).
You could also page.route the network, intercept the pdf requests and download them on the Node side.
You could also
page.routethe network, intercept the pdf requests and download them on the Node side.
can you pls give us example code with page.route
tnx a lot!
It seems like the workaround from this comment solves the issue.
I hit the same problem today and the way I resolved it is by adding a download attribute before clicking the pdf link. Example:
await page.$eval(pdfLinkSelector, (el) =>
el.setAttribute('download', 'download'),
);
Most helpful comment
OK, after many hours of searching in the last 48 hours for a solution to this, I think I've managed to discover something soon after posting here that works.
Would anyone mind taking a look at this solution and seeing if it's going to cause me any unexpected problems?
For each run:
This seems to work and forces the browser to download files vs displaying them in the in-browser PDF viewer. I just want to make sure I'm not shooting myself in the foot by only having a tiny preferences file to start (although it gets filled-in on first start).