【问题标题】:How to correctly read PDF into nodejs Buffer from Google Drive API's export method?如何从 Google Drive API 的导出方法将 PDF 正确读入 nodejs 缓冲区?
【发布时间】:2020-02-02 15:16:29
【问题描述】:

我正在尝试从 Google Drive v2 API export 方法将 PDF 读取到 nodejs 缓冲区中,以便可以将其作为附件通过电子邮件发送或保存,但是当我查看它时,它看起来像所有空白一样是空白的。我希望 PDF 与通过 Google Drive 浏览器应用导出和查看时的外观相同。即 something 显示,因为content-length: 55243。

我尝试了以下方法:

  var drive = google.drive('v2');
  drive.files.export({
    auth: auth,
    fileId: ...,
    mimeType: 'application/pdf'
  })
  .then(res => {

      let buf = Buffer.from(res.data, 'utf-8');//tried 'latin1', didn't work
      fs.writeFileSync("file.pdf", buf);
  })

原始 HTTP 响应

    HTTP/1.1 200 
    cache-control: private, max-age=0, must-revalidate, no-transform
    content-encoding: gzip
    content-length: 55243
    content-type: text/plain; charset=UTF-8
    date: Sat, 01 Feb 2020 15:04:46 GMT
    etag: "x2jdlkqYTB8kzPmV7jH2KPtlR68/iXa-VTcVlqvfqgBwCPMhdnUXfUk"
    expires: Sat, 01 Feb 2020 15:04:46 GMT
    server: GSE
    vary: Origin, X-Origin
    Content-Type: application/pdf

    %PDF-1.4
    ...
    ...

我的代码中需要修复什么?你能展示如何正确地将 PDF 读入缓冲区吗?

【问题讨论】:

    标签: node.js pdf google-drive-api


    【解决方案1】:
    • 您想使用 Google Drive 中的数组缓冲区将 Google 文档(电子表格、文档和幻灯片)导出为 PDF 文件。
    • 您希望使用 Drive API v2 和 Node.js 的 googleapis 来实现此目的。
    • 您已经能够使用 Drive API 获取文件。

    如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

    模式一:

    在此模式中,使用数组缓冲区。

    修改脚本:

    const drive = google.drive({version: "v2"});
    drive.files.export(
      {
        auth: auth,
        fileId: "###",  // Please set the file ID of Google Docs.
        mimeType: "application/pdf"
      },
      { responseType: "arraybuffer" },
      (err, res) => {
        if (err) {
          console.log(err);
        } else {
          fs.writeFile("file.pdf", Buffer.from(res.data), function(err) {
            if (err) {
              return console.log(err);
            }
          });
        }
      }
    );
    

    模式 2:

    在此模式中,使用流。

    修改脚本:

    const drive = google.drive({version: "v2"});
    var dest = fs.createWriteStream("file.pdf");
    drive.files.export(
      {
        auth: auth,
        fileId: "###",  // Please set the file ID of Google Docs.
        mimeType: "application/pdf"
      },
      { responseType: "stream" },
      function(err, response) {
        if (err) {
          console.log(err);
          return;
        }
        response.data
          .on("end", function() {
            console.log("Done.");
          })
          .on("error", function(err) {
            console.log("Error during download", err);
            return process.exit();
          })
          .pipe(dest);
      }
    );
    

    注意:

    • 请使用最新版本的 googleapis。
    • 并且在上述脚本中,当const drive = google.drive({version: "v2"});修改为const drive = google.drive({version: "v3"});时,也可以使用Drive API v3。

    参考资料:

    如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

    【讨论】:

    • @aalv 感谢您的回复。很高兴您的问题得到解决。
    猜你喜欢
    • 2018-12-24
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2023-03-02
    • 1970-01-01
    • 2011-01-03
    • 2017-08-23
    • 1970-01-01
    相关资源
    最近更新 更多