Google Apps Script to create PDF

This is a little script to create a PDF file using a Google Doc template and the values in a Google Spreadsheet.

Further to user requests, I have created many variations on the PDF creation theme.

Here is a function for formatting various number and date types properly into your final document.

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.

So to use:

  1. Create a Google Doc as a template using the placeholders 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).
  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. Put the actual values you want to use in the spreadsheet under the appropriate columns, one row for each PDF file.
  5. Click a cell on a row of values you would like to use to select that row and click Create PDF > Create PDF.

The new PDF will be created after a few moments in the root of your Google Drive with the name ‘Copy of  <template name>’.pdf.

There is a Gist where you can also see the code and A demo spreadsheet.

There is also this variation that allows placeholders to be used in the filename.