【发布时间】: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