Task guide
Email a Google Doc as a PDF
Give the script the file ID of a Doc. It makes a PDF copy and emails it as an attachment. 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
- Doc file id
PASTE_THE_DOC_ID_HEREThe 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 ofThe 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
- Read the Google Doc with that file ID through Google Drive.
- Send email from your Google account, with an attachment.
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.
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 safely
Follow these steps in order
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.
Paste and save the function
Go to script.google.com, choose New project, paste the function, and save. The text application/pdf tells Drive what to convert to. Writing MimeType.PDF instead means the same thing.
Send the first copy to yourself
Leave the recipient as Session.getEffectiveUser().getEmail() and run emailDocAsPdf. Approve the access when Google asks. The message lands in your own inbox, so you see the PDF before anyone else does.
Change the recipient
Once the PDF looks right, replace Session.getEffectiveUser().getEmail() with the address you want, in quotes, such as you@example.com. 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 error says No item with the given ID could be found, or you do not have permission to access it.
- Check that you pasted only the file ID and not the whole web address, and that the account running the script can open the Doc.
- The email was sent to the wrong person.
- Session.getEffectiveUser().getEmail() sends to the account that runs the script. Check which account is signed in to the Apps Script editor, or set the address yourself.
- Gmail refuses the message because the attachment is too large.
- Gmail rejects attachments over 25 MB. Split a very long Doc, remove large images, or share a link to the Doc instead.
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
Related tasks, basics, and apps
- Task guide Save labeled Gmail attachments to Drive
Choose the email threads yourself by adding a Gmail label, then copy their attachments into an existing Drive folder.
- Task guide Create a Google Doc from each Sheet row
Keep a person and a project note in a Sheet, then create one short Doc per unfinished row.
- Object reference Drive file
A DriveApp.File is one item in Google Drive, such as a Doc, Sheet, or PDF. Open it with DriveApp.getFileById(id) to read its metadata, copy it, or export a PDF.
- Google app Google Drive
DriveApp lets a script open files by ID, find a named folder with getFoldersByName, and save copies or PDF exports in one known place.
- Google app Gmail
GmailApp scripts can send messages, search threads, apply labels, and read attachments after you approve Gmail access for the project.