英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论