Sunday 13 January 2019

Convert Doc to PDF and move into a new folder

The following Google Apps Script code was developed to create a PDF version of a Google Doc, move it into a new folder, remove its parents so it only existed in the new folder (typically also exists in My Drive) and then trash the Doc. I first used this tool during the Exceptional Circumstances system, but have used it again during a script for saving emails from Gmail so I thought it was worth sharing on its own here.

In my example I have used fictional IDs but you would get these directly from existing files/folders via the 'DriveApp'. Firstly I get the Doc via 'getFileById' then 'getAs' PDF and 'createFile' so I now have a PDF version. Now I can 'getParents' to find the current location of where the PDF exists in Google Drive - and this is usually My Drive. Next I move the PDF file into the new folder location ('addFile') and then loop through its parent locations, removing it from there ('removeFile') so it only exists in the new location. By getting the parent locations before moving the file I ensure that the new folder I move it to is not included and hence the PDF will remain there.

The final step is to delete the original Google Doc file ('setTrashed').

Convert Doc to PDF and (please use 'Overview' > 'Make a copy' for your own version).


  1. Has someone altered this successfully to make pdf's from a folder full of Docs?
    (we would not delete the originals for our use and comment out line 25 :)

    1. Hi Richard

      Unfortunately I don't have a version that would do this at this point - though it's perfectly feasible and would require an iterator like in the post here:

      Kind regards