4

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
}
ryan.d.williams
  • 620
  • 5
  • 14

1 Answers1

2

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.

ryan.d.williams
  • 620
  • 5
  • 14
  • Could you add code example to your answer? I tried following those but I get error `document is not defined` or `window is not defined` – Elliott B Jul 23 '23 at 20:52