Neven Subotic
Neven Subotic

Reputation: 1429

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. I am trying to respond to an API call to my endpoint with a blob of a PDF File. 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.

Quick Overview of what is happening. I am stuck at steps 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. 3. I export the Document as PDF. 4. I send a response to my API consumer with a blob of the Document from step 3.

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. 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
})

Upvotes: 2

Views: 2474

Answers (1)

Tanaike
Tanaike

Reputation: 201553

I believe your goal as follows.

  • You want to export Google Document without creating a file as the base64 data.
  • You want to achieve this using googleapis with Node.js.
  • You have already been able to export the Google Document using Drive API.

For this, how about this answer?

Unfortunately, I cannot see your script of updateDoc( fileId, req.body ), exportDocAsPdf(doc) and 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.

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.

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. At that time, the data is the arraybuffer. So the base64 data is retrieved using Buffer.from(res.data).toString("base64").

References:

Upvotes: 3

Related Questions