Custom Filenames in Apps Script PDF Conversion
The default PDF filename is the document title. You can override this with anything that makes sense for your workflow: a date prefix, a project code, a client name, or a combination of metadata fields.
Basic filename patterns
Construct the filename as a string before calling createFile(). Common patterns: docTitle + '.pdf' for the default, clientName + '_' + docTitle + '.pdf' for client work, or Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd') + '_' + docTitle + '.pdf' for date-prefixed archives.
Reading metadata for the filename
Access document properties using DocumentApp.openById(docId). The title, author, and creation date are all available. You can also add custom metadata to a Google Doc using document properties stored in the script, or read values from a named range in a connected Google Sheet.
Sanitizing the filename
File names cannot contain slashes or certain special characters. Use replace() with a regex to strip or replace invalid characters before using the string as a filename. A simple pattern: filename.replace(/[/\\?%*:|"<>]/g, '-') replaces problematic characters with hyphens.
Frequently Asked Questions
Can I include the document's last modified date in the filename?
Yes. Use DriveApp.getFileById(docId).getLastUpdated() to get the modification date, then format it with Utilities.formatDate().
How do I avoid duplicate filenames when the same script runs multiple times?
Include a timestamp in the filename, or check whether a file with the same name already exists before creating it. If a duplicate exists, either overwrite it or append a counter to the new filename.
Related article
Set up a workflow to convert Google Docs to PDF →Ready to Try It?
Install the free Chrome extension and start converting your Google Docs to PDF in one click.
Install Free Extension