Skip to content
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

[Question] Is it possible to set AlwaysOpenPdfExternally #3509

Closed
waynerobinson opened this issue Aug 18, 2020 · 9 comments
Closed

[Question] Is it possible to set AlwaysOpenPdfExternally #3509

waynerobinson opened this issue Aug 18, 2020 · 9 comments

Comments

@waynerobinson
Copy link

waynerobinson commented Aug 18, 2020

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

@waynerobinson
Copy link
Author

waynerobinson commented Aug 18, 2020

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).

@pavelfeldman
Copy link
Member

You could also page.route the network, intercept the pdf requests and download them on the Node side.

@osmenia
Copy link

osmenia commented Aug 19, 2020

You could also page.route the 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!

@dgozman
Copy link
Contributor

dgozman commented Nov 24, 2020

It seems like the workaround from this comment solves the issue.

@dgozman dgozman closed this as completed Nov 24, 2020
@ibratoev
Copy link

ibratoev commented Dec 3, 2020

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'),
    );

@ibash
Copy link

ibash commented Jan 3, 2023

@waynerobinson a slight variation, instead of setting user data you can set the preferences file in chromium args:

e.g.

      headless: false,
      args: [`--initial-preferences-file="path/to/preferences_file"`]

There's a basic preferences file here: https://support.google.com/chrome/a/answer/187948
And that argument is documented here: https://peter.sh/experiments/chromium-command-line-switches/

@wizpresso-steve-cy-fan
Copy link

Can we reopen this issue because it is still unsolved?

@mxschmitt
Copy link
Member

We have another issue where we keep track of it here #7822.

@mandras73
Copy link

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.

do you have a python version of the workaround?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants