Save Apps Script PDF Output to a Google Drive Folder

Exporting to PDF is only half the task. Saving the result to the right place in Drive is the other half. Apps Script gives you full control over where the PDF lands, what it is named, and how existing files are handled.

Specifying the target folder

Get your target folder using DriveApp.getFolderById(targetFolderId). Pass this folder object to the createFile() call: folder.createFile(filename, pdfBlob, MimeType.PDF). The PDF is created directly inside that folder.

Naming the output file

A good naming convention includes the source document name and a timestamp: sourceName + '_' + Utilities.formatDate(new Date(), 'UTC', 'yyyy-MM-dd') + '.pdf'. This makes files sortable by date and avoids naming conflicts when the same document is exported multiple times.

Overwriting vs. creating new files

DriveApp's createFile() always creates a new file even if a file with that name already exists in the folder. To overwrite, search for an existing file with the same name using folder.getFilesByName(filename) and delete it with file.setTrashed(true) before creating the new one.

Frequently Asked Questions

How do I find the folder ID for a Google Drive folder?

Open the folder in Google Drive. The folder ID is the string at the end of the URL after /folders/.

Can I save to a folder in a shared drive?

Yes. Get the shared drive folder using DriveApp.getFolderById() with the shared drive folder's ID. The script must run under an account that has Contributor or higher access to the shared drive.

Ready to Try It?

Install the free Chrome extension and start converting your Google Docs to PDF in one click.

Install Free Extension