简体   繁体   English

如何下载在 puppeteer 的新标签页中打开的 pdf?

[英]How to download a pdf that opens in a new tab in puppeteer?

I have a page with a button.我有一个带按钮的页面。 When I click that button, it opens a PDF in a new tab.当我单击该按钮时,它会在新选项卡中打开一个 PDF。

How can I download the PDF as a file with puppeteer?如何使用 puppeteer 将 PDF 作为文件下载?

Maybe I can write a file with the buffer from the new tab.也许我可以用新选项卡中的缓冲区编写一个文件。 But I am not sure how.但我不确定如何。

A simple solution is to use the fetch api to execute the GET request.一个简单的解决方案是使用fetch api 来执行GET请求。 This way you can read the response, pass it to your backend and save it to disk.通过这种方式,您可以读取响应,将其传递到后端并将其保存到磁盘。

Use this sample code as reference:使用此示例代码作为参考:

import fs from 'fs';

async function downloadImage(page: any, url: string, fullpath: string) {
  const data = await page.evaluate(
    // tslint:disable-next-line no-shadowed-variable
    async ({ url }) => {
      function readAsBinaryStringAsync(blob) {
        return new Promise((resolve, reject) => {
          const fr = new FileReader();
          fr.readAsBinaryString(blob);
          fr.onload = () => {
            resolve(fr.result);
          };
        });
      }

      const r = await fetch(url, {
        credentials: 'include',
        headers: {
          accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp, */*;q=0.8',
          'cache-control': 'no-cache',
          pragma: 'no-cache',
          'sec-fetch-mode': 'navigate',
          'sec-fetch-site': 'same-site',
          'upgrade-insecure-requests': '1'
        },
        referrerPolicy: 'no-referrer-when-downgrade',
        body: null,
        method: 'GET',
        mode: 'cors'
      });

      return await readAsBinaryStringAsync(await r.blob());
    },
    { url }
  );

  fs.writeFileSync(fullpath, data, { encoding: 'binary' });
}

Use puppeteer-extra node module.使用 puppeteer-extra 节点模块。

Puppeteer-extra 木偶戏

const puppeteer = require('puppeteer-extra');
...
...
puppeteer.use(require('puppeteer-extra-plugin-user-preferences')({userPrefs: {
   download: {
     prompt_for_download: false,
     open_pdf_in_system_reader: true
  },
  plugins: {
    always_open_pdf_externally: true // this should do the trick
  }
}}))

const browser = await puppeteer.launch();

browser.on('targetcreated', async (target) => {
   console.log('targetcreated');
   if (target.type() !== 'page') {
     return;
   }
   try {
     const pageList = await browser.pages();
     pageList.forEach((page) => {
       page._client.send('Page.setDownloadBehavior', {
         behavior: 'allow',
         downloadPath: './pdfDownloaded/',
       });
     });
   } catch (e) {
     console.log("targetcreated", e);
   }
});
...
...

But when i set the always_open_pdf_externally: true chrome crashes.但是当我设置always_open_pdf_externally: true chrome 崩溃。

try if it works for you, and please post back the answer if you found any尝试它是否适合您,如果您找到任何答案,请发回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM