Google驱动器始终通过API导出空白pdf

问题描述 投票:0回答:1

我在Gdrive上有一个Google演示文稿,我想以编程方式将其导出为PDF。它工作正常,但下载的文件始终为空!但是页面数正确。

这是我的代码

function exportFile(auth, id) {
  const drive = google.drive({
    version: "v3",
    auth: auth
  });
  drive.files.export(
    {
      fileId: id,
      mimeType: "application/pdf"
    },
    (err, res) => {
      if (err) {
        console.log(err);
      } else {
        fs.writeFile("local.pdf", res.data, function(err) {
          if (err) {
            return console.log(err);
          }
        });
      }
    }
  );
}

fs.readFile("credentials.json", (err, content) => {
  if (err) return console.log("Error loading client secret file:", err);
  // Authorize a client with credentials, then call the Google drive API.
  authorize(JSON.parse(content), auth => {
    exportFile(auth, "1mtxWDrPCt8EL_UoSUbrLv38Cu8_8LUm0onSv0MPCIbk");
  });
});


这是生成的文件,其中包含正确数量的幻灯片(2),但内容空白:

enter image description here

知道我缺少什么吗?非常感谢!

javascript google-drive-api google-slides-api
1个回答
0
投票

根据您的问题,我可以理解您已经能够使用Drive API从Google云端硬盘导出文件。那么如何修改呢?

修改的脚本:

修改脚本后,请如下修改exportFile()。请按如下方式使用responseType

function exportFile(auth, id) {
  const drive = google.drive({
    version: "v3",
    auth: auth
  });
  drive.files.export(
    {
      fileId: id,
      mimeType: "application/pdf"
    },
    { responseType: "arraybuffer" },  // Added
    (err, res) => {
      if (err) {
        console.log(err);
      } else {
        fs.writeFile("local.pdf", Buffer.from(res.data), function(err) { // Modified
          if (err) {
            return console.log(err);
          }
        });
      }
    }
  );
}

注意:

  • 在这种情况下,假定您正在使用最新的googleapis

参考:

如果这不是您想要的方向,我很抱歉。

© www.soinside.com 2019 - 2024. All rights reserved.