Tuesday, 16 November 2021

Convert Google Doc to PDF in a given folder

The following Google Apps Script is designed to create a PDF file of a Google Doc in a Drive folder that you specify, with the option to delete the original Doc. This snippet of code is from larger solutions developed on this blog and allows you to understand and replicate the process.

This post is somewhat of an updated version of this Convert Doc to PDF and move into a new folder blog post. Whilst the code in it still works it was part of my early days of learning Apps Script and it was also when Google allowed for files to exist in more than one place (a feature now replaced by Shortcuts).

The Code

Firstly we get our Google Doc (that we want to convert to a PDF) and the Google Drive folder where we want our PDF to be stored:

  // get Google Drive folder
  var folder = DriveApp.getFolderById('ENTER ID HERE');

  // get Google Doc file
  var file = DriveApp.getFileById('ENTER ID HERE');

Next we get the content of our Google Doc as a PDF (blob) and create our new file in the Google Drive folder:

  // get file content as PDF blob
  var pdfBlob = file.getAs('application/pdf');

  // create new PDF file in Google Drive folder

Finally, and optionally if you want to comment this part out, we delete the original Google Doc that we no longer need:

  // delete original Google Doc file


Convert Google Doc to PDF in a given folder download here (please use 'Overview' > 'Make a copy' for your own version).

No comments:

Post a Comment