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
- 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
- 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.
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
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 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.
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.
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
Related tasks, basics, and apps
- Task guide Save labeled Gmail attachments to Drive
Copy the attachments from chosen Gmail conversations into one Drive folder. You pick the conversations by adding a label to them, and the script saves every attachment it finds there.
- Task guide Create a Google Doc from each Sheet row
Create one short Google Doc for each row on a Projects tab that has no document link yet. The script writes the link to the new Doc back into the row.
- Object reference DriveApp.getFileById and Drive files
DriveApp.getFileById(id) returns a DriveApp.File for a Google Drive file, such as a Doc, Sheet, or PDF. Pass the file ID as a string. The call throws an exception if the file does not exist or the executing account cannot access it.
- Google app Google Drive
DriveApp lets a script open files by ID, find a named folder with getFoldersByName, and save copies or PDFs to a folder you choose.
- Google app Gmail
GmailApp scripts can send messages, search threads, apply labels, and read attachments after you approve Gmail access for the project.