Automating PDF Generation from Google Docs Using Google Apps Script

Google Apps Script is a versatile tool that allows you to automate various tasks within the Google Workspace ecosystem. One useful application is converting Google Docs to PDF files and saving them to a specific Google Drive folder. In this blog post, we will walk through an exercise to write a script that accomplishes this task. Additionally, we will explore how you can use Apps Script to generate and manipulate PDF files from Google Docs.


Before we start, make sure you have:

  • A Google account
  • Basic understanding of JavaScript
  • A Google Doc and a Google Drive folder ready for this exercise

Step 1: Setting Up Your Google Doc and Drive Folder

  1. Create or Open a Google Doc:
    • Open Google Docs and create a new document or open an existing one that you want to convert to PDF.
  2. Create a Google Drive Folder:
    • Open Google Drive and create a new folder where you want to save the PDF files. Note the folder’s name or ID for later use.

Step 2: Writing the Apps Script

  1. Open the Apps Script Editor:
    • In your Google Doc, go to Extensions > Apps Script to open the script editor.
  2. Create a New Script:
    • Replace any code in the script editor with the following code to convert the Google Doc to a PDF and save it to a specific folder:

function convertDocToPdf() {

  // Replace with your Google Doc ID

  const docId = ‘YOUR_DOCUMENT_ID’;

  // Replace with your Google Drive folder ID

  const folderId = ‘YOUR_FOLDER_ID’;

  // Get the Google Doc as a file

  const doc = DocumentApp.openById(docId);

  const pdfBlob = doc.getAs(‘application/pdf’);

  // Get the destination folder

  const folder = DriveApp.getFolderById(folderId);

  // Save the PDF to the specified folder

  const fileName = doc.getName() + ‘.pdf’;


  Logger.log(‘PDF created and saved to folder: ‘ + fileName);


Step 3: Running the Script

  1. Save and Run the Script:
    • Save your script by clicking the floppy disk icon or pressing Ctrl+S (Windows) or Cmd+S (Mac).
    • Click on the Run button (the play icon) to execute the convertDocToPdf function.
  2. Authorize the Script:
    • The first time you run the script, you will need to authorize it. Click on Review Permissions and follow the steps to grant the necessary permissions.
  3. Check Your Google Drive:
    • After running the script, go to your specified Google Drive folder. You should see the PDF version of your Google Doc saved in the folder.

Step 4: Customizing the Script

  1. Automate the Conversion:
    • To automate the PDF conversion, you can set up a trigger that runs the script at specific intervals or based on certain events (e.g., when the document is updated).
  2. Handle Multiple Documents:
    • If you need to convert multiple Google Docs to PDFs, you can modify the script to loop through a list of document IDs and save each one as a PDF in the specified folder.
  3. Error Handling:
    • Add error handling to manage cases where the document or folder ID is incorrect, or the conversion fails.

Using Apps Script to Generate and Manipulate PDF Files

  1. Generating PDF Files:
    • You can generate PDF files from various Google Workspace applications, including Google Docs, Sheets, and Slides. Use the getAs() method to convert these files to PDFs.
  2. Manipulating PDF Files:
    • Although Apps Script has limited capabilities for directly manipulating PDF content, you can use third-party APIs and libraries to perform more complex tasks like merging, splitting, or modifying PDF files.


With this exercise, you’ve learned how to use Google Apps Script to convert a Google Doc to a PDF and save it to a specific Google Drive folder. This automation can save you time and streamline your document management workflows. By exploring further, you can customize and extend this functionality to meet your specific needs.