Skip to content

Task guide

Email a Google Doc as a PDF

Send a Google Doc to someone as a PDF attachment. Paste the file ID of the Doc into the script, and it builds the PDF and emails it. The Doc itself is not changed.

1 · Prepare

Put these pieces in place

  • Have a Google Doc you can open, and copy its file ID out of the address bar.
  • Use an account that can send email through Gmail.
  • Leave the recipient set to yourself until the first test looks right.

Change these values

Change these values

Make the code yours before you run it.
Doc file id
PASTE_THE_DOC_ID_HERE

The long code between /d/ and /edit in the address of the Doc.

Recipient
Session.getEffectiveUser().getEmail()

This sends to the account running the script. Replace it with an address in quotes when the test works.

Subject start
PDF copy of

The subject begins with these words and ends with the file name.

2 · Review before running

What the script can access and change

Permissions you approve

  • See, edit, create, and delete all of your Google Drive files. The code only opens the Doc with that file ID to make a PDF copy.
  • Read, compose, send, and permanently delete all your email from Gmail. Google asks for full Gmail access because the code uses GmailApp, but the code only sends one email with the PDF attached.
  • See your primary Google Account email address. The code reads it to send the first test copy to you.

Effects of one run

  • Sends one email with a PDF copy of the Doc attached, named after the Doc.
  • Does not change, move, rename, or share the original Doc. The PDF is built while the script runs and is not saved to Drive.

3 · Copy

Paste this into the Apps Script editor

Create or open a script file, replace the example values marked above, then save the project.

Code.gs
const DOC_FILE_ID = 'PASTE_THE_DOC_ID_HERE'; // The long code between /d/ and /edit in the address of the Doc.
const RECIPIENT = Session.getEffectiveUser().getEmail(); // This sends to the account running the script. Replace it with an address in quotes when the test works.
const SUBJECT_START = 'PDF copy of'; // The subject begins with these words and ends with the file name.

function emailDocAsPdf() {
  const file = DriveApp.getFileById(DOC_FILE_ID);
  const pdf = file.getAs('application/pdf').setName(file.getName() + '.pdf');

  GmailApp.sendEmail(RECIPIENT, SUBJECT_START + ' ' + file.getName(), 'The PDF copy is attached.', {
    attachments: [pdf]
  });

  Logger.log('Sent ' + file.getName() + '.pdf to ' + RECIPIENT);
}

4 · Run

Follow these steps in order

  1. Find the file ID

    Open the Doc and look at the address. It reads https://docs.google.com/document/d/FILE_ID/edit. Copy the part between /d/ and /edit, and paste it over PASTE_THE_DOC_ID_HERE in the code.

  2. Paste and save the function

    Go to script.google.com and click New project. Paste the function over the starter code and save. The text application/pdf tells Drive which format to convert to. MimeType.PDF means the same thing.

  3. Send the first copy to yourself

    Leave RECIPIENT as Session.getEffectiveUser().getEmail(). Choose emailDocAsPdf in the function dropdown above the code, then click Run. Approve access when Google asks. The message lands in your own inbox, so you see the PDF before anyone else does.

  4. Change the recipient

    When the PDF looks right, replace Session.getEffectiveUser().getEmail() with the address you want, in quotes, such as 'you@example.com'. Save and run again. Send only to people who expect the file.

5 · Confirm

How to tell it worked

An email arrives with the subject PDF copy of, followed by the file name, and a PDF attachment that shows the Doc as it looks today.

If the result is different

Troubleshooting

The run stops with "No item with the given ID could be found, or you do not have permission to access it."
Check that DOC_FILE_ID holds only the file ID and not the whole web address, and that the account running the script can open the Doc.
The email went to the wrong address.
Check which account is signed in to the Apps Script editor, or set RECIPIENT to an address in quotes. Session.getEffectiveUser().getEmail() is the address of the account that runs the script.
The run stops with an error about the attachment size.
Shorten the Doc, remove large images, or share a link to the Doc instead. Gmail rejects attachments over 25 MB.

Technical details

Names used in this script

Open these details when you want to adapt the example or look up an exact service or method name.

Show Apps Script symbols and service pages

Apps Script symbols

Service pages

Keep going