π¬ Create custom PDF documents from templates with Gemini & Google Drive
β‘ 9,888 views Β· π¬ Document Extraction & Analysis
Description
This workflow contains community nodes that are only compatible with the self-hosted version of n8n.
1 β What Does It Do / Which Problem Does It Solve?
This workflow turns Google Docs-based contract & form templates into ready-to-sign PDFs in minutesβall from a single chat flow.
- Automates repetitive document creation. Instead of copying a rental, sales, or NDA template and filling it by hand every time, the bot asks for the required values and fills them in.
- Eliminates human error. It lists every mandatory field so nothing is missed, and removes unnecessary clauses via conditional blocks.
- Speeds up approvals. The final draft arrives as a direct PDF linkβone click to send for signing.
- One template β unlimited variations. Every new template you drop in Drive is auto-listed with **zero workflow editsβ**it scales effortlessly.
- 100 % no-code. Runs on n8n + Google Apps Scriptβno extra backend, self-hosted or cloud.
2 β How It Works (Detailed Flow)
-
π Template Discovery
π The TemplateList node scans the Drive folder you specify via the
?mode=metaendpoint and returns anid / title / desclist. The bot shows this list in chat. -
π― Selection & Metadata Fetch
The user types a template name.
π GetMetaData opens the chosen Doc, extracts
META_JSON, placeholders, and conditional blocks, then lists mandatory & optional fields. -
π£ Data-Collection Loop
-
The bot asks for every placeholder value.
-
For each conditional block it asks π’ Yes / π΄ No.
Answers are accumulated in a
dataJSON object.
-
-
β Final Confirmation
The bot summarizes the inputs β when the user clicks Confirm, the DocProcess sub-workflow starts.
-
βοΈ DocProcess Sub-Workflow
π§ Step Node Task 1 User Choice Match Check Verifies nameβID match; throws if wrong 2 GetMetaData (renew) Gets the latest placeholder list 3 Validate JSON Format Checks for missing / unknown fields 4 CopyTemplate Copies the Doc via Drive API 5 FillDocument Apps Script fills placeholders & removes blocks 6 Generate PDF Link Builds an export?format=pdfURL -
π Delivery
The master agent sends π Download PDF & βοΈ Open Google Doc links.
-
π« Error Paths
status:"ERROR", missing:[β¦]β bot lists missing fields and re-asks.unknown:[β¦]β template list is outdated; rerun TemplateList.- Any Apps Script error β the returned
messageis shown verbatim in chat.
3 β π Setup Steps (Full Checklist)
> Goal: Get a flawless PDF on the first run. > > > Mentally tick the βοΈ in front of every line as you go. >
βοΈ A. Google Drive Preparation
| Step | Do This | Watch Out For |
|---|---|---|
| 1 | Create a Templates/ folder β put every template Doc inside | Exactly one folder; no sub-folders |
| 2 | Placeholders in every Doc are {{UPPER_CASE}} | No Turkish chars or spaces |
| 3 | Wrap optional clauses with [[BLOCK_NAME:START]]β¦[[BLOCK_NAME:END]] | The START tag must have a blank line above |
| 4 | Add a META_JSON block at the very end | Script deletes it automatically after fill |
| 5 | Right-click Doc > Details βΈ Description = 1-line human description | Shown by the bot in the list |
| 6 | Create a second Generated/ folder (for copies) | Keeps Drive tidy |
> π Folder ID (long alphanumerical) = <TEMPLATE_PARENT_ID> > > > Weβll paste this into the TemplateList node next. >
Simple sample template β Template Link
π B. Import the Workflow into n8n
Settings βΈ Import Workflow βΈ DocAgent.json
If nodes look Broken afterwards β no community-node problem; you only need to select credentials.
π C. Customize the TemplateList Node
-
Open Template List node βοΈ β replace
'%3CYOUR_PARENT_ID%3E' in parentswith the real folder ID in the URL.
-
Right-click node > Execute Node.
-
Copy the entire JSON response.
-
In the editor paste it into:
-
DocAgent β System Prompt (top)
-
User Choice Match Check β System Prompt (top)
Save.
-
> β οΈ Why manual? Caching the list saves LLM tokens. Whenever you add a template, rerun the node and update the prompts. >
π D. Deploy the Apps Script
| Step | Screen | Note |
|---|---|---|
| 1 | Open Gist files GetMetaData.gs + FillDocument.gs β File βΈ Make a copy | Both files may live in one project |
| 2 | Project Settings > enable Google Docs API βοΈ & Google Drive API βοΈ | Otherwise youβll see 403 errors |
| 3 | Deploy βΈ New deployment βΈ Web app | |
| β’ Execute as | Me | |
| β’ Who has access | Anyone | |
| 4 | On the consent screen allow scopes:β’ β¦/auth/documentsβ’ β¦/auth/drive | Click Advanced βΊ Go if Google warns |
| 5 | Copy the Web App URL (e.g. https://script.google.com/macros/s/ABC123/exec) | If this URL changes, update n8n |
Apps Script source code β Notion Link
π§ E. Wire the Script URL in n8n
| Node | Field | Action |
|---|---|---|
| GetMetaData | URL | <WEB_APP_URL>?mode=meta&id={{ $json["id"] }} |
| FillDocument | URL | <WEB_APP_URL> |
> π‘ Prefer using an .env file? Add GAS_WEBAPP_URL=β¦ and reference it as {{ $env.GAS_WEBAPP_URL }}. >
π F. Add Credentials
- Google Drive OAuth2 β Drive API (v3) Full Access
- Google Docs OAuth2 β same account
- LLM key (OpenAI / Gemini)
- (Optional) Postgres Chat Memory credential for the corresponding node
π§ͺ G. First Run (Smoke Test)
- Switch the workflow Active.
- In the chat panel type
/start. - Bot lists templates β pick one.
- Fill mandatory fields, optionally toggle blocks β Confirm.
- π Download PDF link appears β βοΈ setup complete.
β H. Common Errors & Fixes
| π Error | Likely Cause | Remedy |
|---|---|---|
403: Apps Script permission denied | Web app access set to User | Redeploy as Anyone, re-authorize scopes |
placeholder validation failed | Missing required field | Provide the listed values β rerun DocProcess |
unknown placeholders: β¦ | Template vs. agent mismatch | Check placeholder spelling (UPPER_CASE ASCII) |
Template ID not found | Prompt list is old | Rerun TemplateList β update both prompts |
Cannot find META_JSON | No meta block / wrong tag | Add [[META_JSON_START]] β¦ [[META_JSON_END]], retry |
β Final Checklist
- Drive folder structure & template rules ready
- Workflow imported, folder ID set in node
- TemplateList output pasted into both prompts
- Apps Script deployed, URL set in nodes
- OAuth credentials & LLM key configured
-
/starttest passes, PDF link received
πββοΈ Need Help with Customizations?
Reach out for consulting & support on LinkedIn: ΓzgΓΌr Karateke
Simple sample template β Template Link
Apps Script source code β Notion Link
π Nodes Used
HTTP Request, Google Drive, Execute Workflow Trigger, AI Agent, Basic LLM Chain, Structured Output Parser
π₯ Import
Download workflow.json and import into n8n:
Workflow menu β Import from File