Apps Script: Save PDF to a Drive Folder
This page focuses on the specific Apps Script code for saving a converted PDF to a Drive folder, covering the common patterns and edge cases you will encounter in real projects.
The createFile method
The core method is folder.createFile(name, content, mimeType). For PDF conversion, pass the filename as a string, the PDF blob from UrlFetchApp, and MimeType.PDF as the MIME type. The file is created in the specified folder immediately.
Getting the folder by ID vs. by name
getFolderById(id) is the most reliable method because folder IDs are stable. Folder names can change. Only use getFoldersByName() as a fallback when you do not have the folder ID, and always handle the case where multiple folders share the same name by taking the first result and logging a warning.
Error handling around file creation
Wrap createFile() in a try-catch. Common errors include insufficient permissions, storage quota exceeded, and invalid file names. Log the error and the target folder ID so you can diagnose the issue quickly without examining the whole script.
Frequently Asked Questions
What is the Apps Script MIME type constant for PDF?
Use MimeType.PDF, which resolves to 'application/pdf'. You can also use the string directly, but the constant is more readable and less error-prone.
Related article
Use Docs to PDF with Google Drive →Ready to Try It?
Install the free Chrome extension and start converting your Google Docs to PDF in one click.
Install Free Extension