Search code examples
c#puppeteer-sharp

How do I download a pdf using puppeteer sharp?


I'm trying to download a pdf using puppeteer sharp. The website has an anchor element and when you click on it, it opens a popup window showing the pdf viewer. There doesn't appear to be a direct link to the pdf file. I'm not sure what to do next. I tried to treat the popup window like any other page and just click on the download button but I can't get the popup page object to do anything. WaitForSelector just times out if I try to search for anything on the popup page.

Here is how I get the page object for the popup page:

var pages = await browser.PagesAsync();

foreach (var p in pages) 
{
    if (p.Url.ToUpper().Contains("FILESTREAMER")) //url of the pdf viewer in the popup
    {
        await p.WaitForSelectorAsync("#viewer"); //times out
    }
}

So I don't know if there's a way for me to access the pdf viewer in the popup window and click the download button or if there's another way I need to do this.


Solution

  • It's hard to intercept the PDF once the viewer is launched. One way to work around this is to generate a Chromium preferences folder where you can disable the PDF viewer and force the download.

    You can create a directory, and inside that directory, create a Default directory, and inside that directory a Preferences file with this content:

    {
      download: {
        open_pdf_in_system_reader: true,
        prompt_for_download: false,
      },
      plugins: {
        always_open_pdf_externally: true,
      },
    };
    

    Once you have that directory, set the path to the UserDataDir launch argument.