如何使用 Puppeteer Sharp 下载 PDF?

huangapple go评论78阅读模式
英文:

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.

英文:

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.

答案1

得分: 0

很难在启动查看器后拦截PDF。解决方法之一是生成一个Chromium首选项文件夹,在其中禁用PDF查看器并强制下载。

您可以创建一个目录,在该目录内创建一个名为Default的目录,然后在其中创建一个名为Preferences的文件,文件内容如下:

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

一旦您拥有该目录,将路径设置为UserDataDir启动参数。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月8日 04:53:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定