Basic Apps Script: Convert a Google Doc to PDF
The simplest Apps Script PDF conversion takes a document ID, fetches the export URL, and saves the PDF blob to a Drive folder. This is the foundation you build every other automation on.
Getting the document ID
Open the Google Doc you want to convert. The document ID is the long alphanumeric string in the URL between /d/ and /edit. Copy it and paste it into your script as a string variable.
The export URL pattern
Google Drive exposes a PDF export endpoint at https://docs.google.com/document/d/FILE_ID/export?format=pdf. You can fetch this URL using UrlFetchApp.fetch() with the user's OAuth token in the Authorization header. The response body is the raw PDF binary.
Saving the PDF to Drive
Take the response blob from the fetch call and pass it to DriveApp.createFile(). Provide a filename ending in .pdf and optionally specify a target folder using DriveApp.getFolderById(). The file appears in Drive immediately.
Running the script
In the Apps Script editor, select your function from the dropdown and click Run. The first run asks for OAuth permissions to access Drive. After you approve, the script runs and the PDF appears in the folder you specified.
Frequently Asked Questions
Where do I find the Apps Script editor?
Go to script.google.com and click New Project. You can also open it from within a Google Doc via Extensions > Apps Script.
Does the script need to run in the same account that owns the Doc?
The script runs under the account that authorizes it. That account needs at least Viewer access to the Google Doc.
Related article
Bulk 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