gpt4 book ai didi

javascript - Google Drive 总是通过 API 导出空白 pdf

转载 作者:行者123 更新时间:2023-12-04 10:41:13 24 4
gpt4 key购买 nike

我的 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

知道我错过了什么吗?非常感谢!

最佳答案

从您的问题中,我了解到您已经能够使用 Drive API 从 Google Drive 导出文件。那么这个修改怎么样呢?

修改后的脚本:

当您的脚本被修改时,请修改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 .

  • 引用:
  • Request-level options
  • google-api-nodejs-client

  • 如果这不是您想要的方向,我深表歉意。

    关于javascript - Google Drive 总是通过 API 导出空白 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59921316/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com