[英]How can I fetch a Google Doc as a PDF file using the Drive API and send that blob to my API consumer?
I am using Express, Node and the Google Drive API.我正在使用 Express、Node 和 Google Drive API。 I am trying to respond to an API call to my endpoint with a blob of a PDF File.
我正在尝试使用 PDF 文件的 blob 来响应对我的端点的 API 调用。 But I do not want to save the file when I fetch it from the Drive API, I basically just want to store it in a variable, convert it to base64 and send it to my API consumer.
但是当我从驱动器 API 获取文件时我不想保存文件,我基本上只是想将它存储在一个变量中,将其转换为 base64 并将其发送给我的 ZDB974238714CA8DE634A7CE1D083A14 消费者。
Quick Overview of what is happening.快速概览正在发生的事情。 I am stuck at steps 3-4.
我被困在第3-4步。 1. Consumer calls my API endpoint with a payload containing payment information 2. I create a new Document from Template, and use the payload to populate the document using Docs API.
1. 消费者使用包含付款信息的有效负载调用我的 API 端点 2. 我从模板创建一个新文档,并使用有效负载通过 Docs API 填充文档。 3. I export the Document as PDF.
3. 我将文档导出为 PDF。 4. I send a response to my API consumer with a blob of the Document from step 3.
4. 我向我的 API 消费者发送响应,其中包含步骤 3 中的文档 blob。
How can I achieve this?我怎样才能做到这一点?
Why do I want to achieve this?我为什么要实现这个目标? Basically, I am trying to avoiding creating extra work to download the file and storing it somewhere, because then I need another connection to something.
基本上,我试图避免创建额外的工作来下载文件并将其存储在某处,因为那时我需要另一个连接到某些东西。 If I cannot avoid this, when I would want to try to handle this on GCP using Buckets.
如果我无法避免这种情况,那么当我想尝试使用 Buckets 在 GCP 上处理这个问题时。 So recommendations there would be helpful as well.
因此,那里的建议也会有所帮助。
Below is an overview of my code,下面是我的代码的概述,
// this works
const driveAPI = google.drive({version:"v3", auth: client});
const fileId = await createDocFromTemplate();
const doc = updateDoc( fileId, req.body );
// now it gets tricky
const PDF_FILE = exportDocAsPdf(doc); // how can I temporarily store pdf to a variable?
const PDF_AS_BASE = transformPdfToBase64(PDF_FILE); // how can I convert pdf to base64?
// this is what I want to send back to the API consumer
res.send({
id: fileId,
fileAsPdf : PDF_AS_BASE
})
I believe your goal as follows.我相信你的目标如下。
For this, how about this answer?为此,这个答案怎么样?
Unfortunately, I cannot see your script of updateDoc( fileId, req.body )
, exportDocAsPdf(doc)
and transformPdfToBase64(PDF_FILE)
.不幸的是,我看不到您的
updateDoc( fileId, req.body )
、 exportDocAsPdf(doc)
和transformPdfToBase64(PDF_FILE)
。 So in this answer, I would like to propose a sample script for returning the base64 data of PDF format by inputting the file ID of Google Document.所以在这个答案中,我想提出一个示例脚本,通过输入谷歌文档的文件ID来返回PDF格式的base64数据。
In this case, the input and output values are the file ID of Google Document and the base64 data of PDF format, respectively.在这种情况下,输入和 output 值分别是 Google Document 的文件 ID 和 PDF 格式的 base64 数据。
async function exportFile(drive, documentId) {
const res = await drive.files.export(
{
fileId: documentId,
mimeType: "application/pdf",
},
{ responseType: "arraybuffer" }
);
return Buffer.from(res.data).toString("base64");
}
const documentId = "###"; // Please set the Google Document ID
const driveAPI = google.drive({ version: "v3", auth: client });
const base64 = await exportFile(driveAPI, documentId).catch((err) => {
if (err) console.log(err);
});
console.log(base64);
Buffer.from(res.data).toString("base64")
.Buffer.from(res.data).toString("base64")
检索 base64 数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.