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