我在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),但内容空白:
知道我缺少什么吗?非常感谢!
根据您的问题,我可以理解您已经能够使用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);
}
});
}
}
);
}
如果这不是您想要的方向,我很抱歉。