By Raj
Automate Google Form to PDF Invoice with Apps Script
You have a Google Form that collects order details—customer name, email, items, and total. Every time someone submits, you manually create an invoice in Google Docs, export it to PDF, and email it. That's a 10-minute task per submission, and it doesn't scale. With Google Apps Script, the same flow takes zero minutes—it runs automatically the moment the form is submitted.
This post walks you through the full setup: a form-linked Sheet, a Docs invoice template with placeholders, and a script that runs on every form submit to fill the template, convert it to PDF, and email it to the customer. You'll see the exact code and how to wire the trigger.
What you need before you start
- A Google Form connected to a Google Sheet (Responses tab).
- A Google Doc invoice template with placeholders like
{{name}},{{items}},{{total}},{{date}}. - The template's Document ID (from the Doc URL:
/d/TEMPLATE_ID/edit).
The workflow
- Customer fills out a Google Form (name, email, items, quantities).
- Form response lands in a Google Sheet.
- Apps Script triggers on form submit: reads the new row, fills a Google Docs template, converts to PDF.
- Script emails the PDF to the customer and saves a copy in Google Drive.
Key code: the form-submit handler
The trigger passes an event object e. e.values is an array of the new row: typically e.values[0] is the timestamp, and the next columns match your form questions (name, email, items, total). Adjust the indices (1, 2, 3, 4) to match your Sheet columns.
function onFormSubmit(e) {
if (!e || !e.values) return; // Guard for manual runs
const row = e.values;
const name = row[1]; // Column B: full name
const email = row[2]; // Column C: email
const items = row[3]; // Column D: description of items
const total = row[4]; // Column E: total amount
// 1. Copy the invoice template (so we don't edit the original)
const templateId = "YOUR_DOC_TEMPLATE_ID"; // Replace with your Doc ID
const copy = DriveApp.getFileById(templateId)
.makeCopy("Invoice - " + name);
const doc = DocumentApp.openById(copy.getId());
const body = doc.getBody();
// 2. Replace placeholders with form data
body.replaceText("{{name}}", name || "");
body.replaceText("{{items}}", items || "");
body.replaceText("{{total}}", total || "");
body.replaceText("{{date}}", new Date().toLocaleDateString());
doc.saveAndClose();
// 3. Convert the filled Doc to PDF (Blob)
const pdfBlob = DriveApp.getFileById(copy.getId())
.getAs("application/pdf");
// 4. Email the PDF to the customer
GmailApp.sendEmail(email, "Your Invoice from Acme",
"Hi " + name + ", please find your invoice attached.",
{ attachments: [pdfBlob], name: "Acme Invoicing" }
);
// 5. Optional: delete temp doc to avoid clutter
DriveApp.removeFile(copy);
}What each part does
Template copy: DriveApp.getFileById(templateId).makeCopy(...) creates a new Doc so the original template stays unchanged. We then open that copy with DocumentApp.openById() and get its body for text replacement.
Placeholders: body.replaceText("{{name}}", name) finds the first occurrence of the placeholder and replaces it. Use unique placeholders (e.g. {{invoice_number}}) if you have multiple. After edits, doc.saveAndClose() is required before exporting.
PDF: getAs("application/pdf") returns a Blob that Gmail can attach. We use the copy's file ID again because we closed the DocumentApp reference.
Optional: save a copy in Drive
Instead of deleting the copy, you can move it to a folder. Get the folder ID from the folder URL, then:
const folderId = "YOUR_INVOICES_FOLDER_ID"; const folder = DriveApp.getFolderById(folderId); const file = DriveApp.getFileById(copy.getId()); file.moveTo(folder);
Setting up the trigger
In the Apps Script editor: click the Triggers icon (clock) → Add trigger. Select function onFormSubmit, event source From spreadsheet, event type On form submit. Save. The first time a response comes in, you may need to authorize the script (Gmail, Drive, Docs). After that, every new form response will generate and email the PDF automatically.
When to get help
Need line-item tables, tax calculations, sequential invoice numbers, or Drive folder organization? These add complexity fast. I can build a production-ready system for you. Get a quote.