简体   繁体   English

如何使用 Drive API 获取 Google Doc 作为 PDF 文件并将该 blob 发送给我的 API 消费者?

[英]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.我相信你的目标如下。

  • You want to export Google Document without creating a file as the base64 data.您想要导出 Google 文档而不创建文件作为 base64 数据。
  • You want to achieve this using googleapis with Node.js.您想使用带有 Node.js 的 googleapis 来实现这一点。
  • You have already been able to export the Google Document using Drive API.您已经能够使用 Drive API 导出 Google 文档。

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数据。

Sample script:示例脚本:

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);
  • In this case, the Google Document is exported as the PDF format.在这种情况下,Google 文档以 PDF 格式导出。 At that time, the data is the arraybuffer.那时,数据就是arraybuffer。 So the base64 data is retrieved using Buffer.from(res.data).toString("base64") .因此,使用Buffer.from(res.data).toString("base64")检索 base64 数据。

References:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 API 从谷歌驱动器下载文件作为 Blob - How to download a file as Blob from google drive using API 使用 Google Drive API v3 将 PDF 文件转换为 Google Doc - Convert a PDF file to Google Doc using Google Drive API v3 Google Drive API v3 - 将文档转换为 pdf - Google Drive API v3 - Convert doc to pdf 使用 Google Drive API v3 将 .docx 或 .doc 文件转换为 google docs 文件 - Using Google Drive API v3 to convert .docx or .doc file to google docs file 如何使用 google docs api 将文本文件转换为 google doc - how to convert a text file into google doc using google docs api 我未登录Google Drive时使用Google Drive API打开Goog​​le Drive文件 - Opening a google drive file using the google drive api while I'm NOT signed in to google drive 使用 Google Drive API v3 中的 drive.files.copy 转换,通过 nodejs 使用 API 将 Word 文档转换为 Google 文档 - convert a Word doc into a Google doc using the API via nodejs using drive.files.copy convert in v3 of Google Drive API 如何对Google Drive API调用的速率进行限制? - How do I rate limit my Google Drive API calls? 如何在使用 Google Drive API v3 移动文件之前检查文件是否已完成上传? - How can I check if a file has finished uploading before moving it with the Google Drive API v3? 如何使用 JS API 将文件上传到 Google 云端硬盘中的特定位置? - How to upload a file into a specific location in the Google Drive using the JS API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM