1

So, I have the codes shown below, and it works fine!. The pdf file in question is named 'targetfile.pdf, which already exists.

Question is:

How do I take the pdf file and move/copy it to the browser download file in the local computer. I just need the codes for that. The idea is that the code will run and the pdf file is automatically sent to the 'download' folder in the PC.

I have tried several coding but to no avail. This is my request for help after I exhausted all other actions. Please help. Any assistance is greatly appreciated. Thank you in advance.

function starthere(){

var doc = SpreadsheetApp.getActiveSpreadsheet();

var key = doc.getId();

var folder = DriveApp.getFolderById(key);            // testFormSheet

var folderid = folder.getParents().next().getId();   // id of folder

var folder = DriveApp.getFolderById(folderid);       // testFormSheet


Logger.log(folder);

Logger.log(folderid);

Logger.log(folder);

Logger.log(doc.getName());    //testdownload


// for this test, the target file to download is 'targetfile.pdf'. 
// we must find a way to download the file. 

var dfile = folder.getFilesByName("targetfile.pdf");

  while(dfile.hasNext()){

    var currfile = dfile.next();
  };

Logger.log(currfile.getName());     //  targetfile.pdf  

Logger.log(currfile.getId());       //  file id of targetfile.pdf
Toto
  • 89,455
  • 62
  • 89
  • 125
  • Apps Script does not have access to the local computer's files, so it's not possible to send a pdf to your downloads folder using Apps Script. Maybe if you explain some context of what you are trying to do, we can offer some workaround? – iansedano Jun 29 '21 at 07:26
  • Context: a pdf file is created or already in a google drive folder. A user runs a script by clicking on a link, and the pdf file is sent to the browser's download folder of the User. I saw an example for a .csv and .txt file, but I cannot do the same for a pdf file. Any work-arounds? – Azlan Abu Hassan Jun 30 '21 at 08:38
  • Can you share the example? – iansedano Jun 30 '21 at 09:12
  • This is an example I found in Stack Overflow: ////////////////////////////////////////////////////////////// [link](https://stackoverflow.com/questions/20281272/initiate-a-download-from-google-apps-script) But I need to download a pdf file instead. – Azlan Abu Hassan Jun 30 '21 at 13:56
  • And what is DocsList anyway? – Azlan Abu Hassan Jun 30 '21 at 14:03
  • I'm a bit confused still, why do you need a script to download the pdf? Why can't you just download it with the Drive Web UI? – iansedano Jul 01 '21 at 07:07
  • Because I wanted to hide the URL. That's actually the most important thing. I have managed to do a HTML centered solution whereby when a user clicks a button, then the pdf file will appear on a new window. But I can't seem to hide the URL address. – Azlan Abu Hassan Jul 02 '21 at 15:12

2 Answers2

0

You can send a base 64 encoded string to a web app

This will allow you to hide both the id and the URL.

See the following example:

index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <button id="button">download pdf</button>
  </body>

  <script>
    const button = document.getElementById("button")
    button.addEventListener("click", downloadPdf)

    // Will send a request to the back-end to get the base 64 encoded string
    function downloadPdf(){
      google.script.run.withSuccessHandler(serverResponseHandler).getPdfBlob()
    }

    // Will create a hidden download link with the pdf and download it.
    function serverResponseHandler(base64String){
      const linkSource = `data:application/pdf;base64,${base64String}`;
      const downloadLink = document.createElement("a");
      const fileName = "abc.pdf";
      downloadLink.href = linkSource;
      downloadLink.download = fileName;
      downloadLink.click();
    }
  </script>
</html>

Code.gs

function doGet() {
  return HtmlService.createHtmlOutputFromFile("index")
}

// Converts a pdf to a base 64 encoded string
function getPdfBlob(){
  const file = DriveApp.getFileById("[FILE_ID]")
  const blob = file.getBlob()
  const bytes = blob.getBytes()
  const b64 = Utilities.base64Encode(bytes)
  return b64
}

Walkthrough

  • The HTML contains a button with a click handler.
  • When the button is pushed it sends a request to the Code.gs file to get the base 64 encoded pdf.
  • With the base 64 string, it will create a link to the pdf with the base 64 encoded string. This way, at no point do you need to reveal the id of the file or the URL.

Be sure to replace the [FILE_ID] with the one you want.

References

iansedano
  • 6,169
  • 2
  • 12
  • 24
0

So I have this 'global variable' in my codes: global variables

And in the doGet(request) function I assigned values to those variables. Assigning values

I have created 2 functions in code.gs to be accessed by the 'download.html' file. Also, 2 extra functions not used. They are: functions

Then in the download.html file I have the following JavaScript codes: javascript in download.html

I know. You can see that I'm a novice. But it works, and that's what matters. And the codes written this way makes it easier for me to understand. At least now my GAS project can move forward.

Thanks for all who have helped me with this. I appreciate this a lot. Thank you X 100. :)