ryan.d.williams
ryan.d.williams

Reputation: 650

Serve PDF file as blob in Google Apps Script

I have a google apps script that generates a PDF file. My goal now is to serve that PDF file back to the UI so the user can download or print it. However, the limitation here is that I cannot redirect the user to a Google Drive URL - I need the file to be given to the user on the same page that initiated the file generation. I have seen this post which gives a solution to download, but it uses a Google Drive link. Additionally, this post and this post mention downloading, but it only works for the given file types, which does not include PDF.

I am able to generate the PDF and blob inside the doGet method, but I don't know how to serve it to the UI so the user can then interact with is. Is there a way to pass the blob to the front end and then allow the user to download or print that blob as a PDF?

function doGet(request){    
    var file = generateFile();
    var blob = file.getAs('application/pdf');
    //Goal: return blob to front end so user can download / print
}

Upvotes: 5

Views: 3341

Answers (1)

ryan.d.williams
ryan.d.williams

Reputation: 650

I was able to accomplish this by passing the data down as a base64 encoded file. I used getBytes() to get the file as an array of bytes, and then base64encode() to encode it before sending it down the front end. On the front-end side, I used a variation of this answer to allow the user to download the file.

Upvotes: 2

Related Questions