本文介绍了如何通过Google Drive API的导出方法将PDF正确读取到nodejs Buffer中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Google Drive v2 API export 方法到nodejs Buffer中,因此可以将其作为附件通过电子邮件发送或保存,但是当我查看它时,它看起来像所有空白一样为空白.我希望PDF的外观与通过Google云端硬盘浏览器应用程序导出和查看时的外观相同.即,由于 content-length:55243 .

I'm trying to read a PDF from the Google Drive v2 API export method into a nodejs Buffer so it can be emailed as an attachment or saved, but when I view it, it looks blank, like all whitespace. I'd expect the PDF to appear the same as when exported and viewed through the Google Drive browser app. Namely that something shows, since the 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读入缓冲区吗?

What needs to be fixed in my code? Can you show how to read the PDF into the Buffer correctly?

推荐答案

  • 您要使用Google云端硬盘中的数组缓冲区将Google文档(电子表格,文档和幻灯片)导出为PDF文件.
  • 您想通过Drive API v2和Node.js的googleapis实现此目标.
  • 您已经能够使用Drive API来获取文件.
  • 如果我的理解是正确的,那么这个答案如何?请认为这只是几个可能的答案之一.

    If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

    在此模式下,使用数组缓冲区.

    In this pattern, the array buffer is used.

    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:

    在此模式下,使用流.

    Pattern 2:

    In this pattern, the stream is used.

    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.
    • Note:

      • Please use the latest version of googleapis.
      • And in above script, when const drive = google.drive({version: "v2"}); is modified to const drive = google.drive({version: "v3"});, the Drive API v3 can be also used.
      • 如果我误解了你的问题,而这不是你想要的方向,我深表歉意.

        If I misunderstood your question and this was not the direction you want, I apologize.

        这篇关于如何通过Google Drive API的导出方法将PDF正确读取到nodejs Buffer中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 19:15