Google Apps Script to create and email a PDF

This script creates a PDF file using a Google Doc template and the values in a Google Spreadsheet. It is a development of this script , where this one allows you to specify the name of the PDF in the GSheet and a recipient to send it to.

This is the development of a simpler script that doesn’t have the renaming and email options. And there is also this variant which does not merge anything into a template document but can be used to convert any type Google Doc (a Google Slide for example) into a PDF and email it.

Get in touch now if you need any help with this or any other GAS scripts or have a look through some other free scripts and snippets.

The script takes the fields from the active row in the active sheet the script is contained in and using a Google Doc template (identified by TEMPLATE_ID) creates a PDF doc with these fields replacing the place-holders in the template. The place-holders are identified by having a % either side, e.g. %Name%. It is invoked by the “Create PDF” menu. You can also optionally specify an name for the PDF file in a column called “File Name” and automatically get the PDF emailed to someone by specifying a recipient in a “Email” column.

So to use:

  1. Create a Google Doc as a template using the place-holders with %’s around the words, e.g. %Name%. Make a note of the ID number ( NUMBER HERE/edit).
  2. Create a new Google Spreadsheet and give it header names (first row) that match up with the placeholders minus the %’s, e.g. Name or Age (it is case-sensitive), and optionally one called “File Name” to specify the name of the PDF file created and “Email” to specify who the PDF should be emailed to.
  3. Copy the script into in the script editor (Tools > Script editor), paste the template ID number into TEMPLATE_ID value (between the ”s) and add a file name for the new PDF in PDF_FILE_NAME if you don’t want to use the template name.
  4. If you are sending the PDF as an email attachment also complete EMAIL_SUBJECT and EMAIL_BODY.
  5. Put the actual values you want to use in the spreadsheet under the appropriate columns, one row for each PDF file.
  6. Click a cell on a row of values you would like to use to select that row and click the custome menu”Create PDF > Create PDF”.

The new PDF will be created after a few moments in the root of your Google Drive.

There is a Gist and A demo spreadsheet take a copy to use and see the script.