πŸ”¬ 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.


2 β€” How It Works (Detailed Flow)

  1. πŸ“ Template Discovery

    πŸ“‚ The TemplateList node scans the Drive folder you specify via the ?mode=meta endpoint and returns an id / title / desc list. The bot shows this list in chat.

  2. 🎯 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.

  3. πŸ—£ Data-Collection Loop

    • The bot asks for every placeholder value.

    • For each conditional block it asks 🟒 Yes / πŸ”΄ No.

      Answers are accumulated in a data JSON object.

  4. βœ… Final Confirmation

    The bot summarizes the inputs β†’ when the user clicks Confirm, the DocProcess sub-workflow starts.

  5. βš™οΈ DocProcess Sub-Workflow

    πŸ”§ StepNodeTask
    1User Choice Match CheckVerifies name–ID match; throws if wrong
    2GetMetaData (renew)Gets the latest placeholder list
    3Validate JSON FormatChecks for missing / unknown fields
    4CopyTemplateCopies the Doc via Drive API
    5FillDocumentApps Script fills placeholders & removes blocks
    6Generate PDF LinkBuilds an export?format=pdf URL
  6. πŸ“Ž Delivery

    The master agent sends πŸ”— Download PDF & ✏️ Open Google Doc links.

  7. 🚫 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 message is 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

StepDo ThisWatch Out For
1Create a Templates/ folder β†’ put every template Doc insideExactly one folder; no sub-folders
2Placeholders in every Doc are {{UPPER_CASE}}No Turkish chars or spaces
3Wrap optional clauses with [[BLOCK_NAME:START]]…[[BLOCK_NAME:END]]The START tag must have a blank line above
4Add a META_JSON block at the very endScript deletes it automatically after fill
5Right-click Doc > Details β–Έ Description = 1-line human descriptionShown by the bot in the list
6Create 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

  1. Open Template List node βš™οΈ β†’ replace

    '%3CYOUR_PARENT_ID%3E' in parents

    with the real folder ID in the URL.

  2. Right-click node > Execute Node.

  3. Copy the entire JSON response.

  4. 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

StepScreenNote
1Open Gist files GetMetaData.gs + FillDocument.gs β†’ File β–Έ Make a copyBoth files may live in one project
2Project Settings > enable Google Docs API βœ”οΈ & Google Drive API βœ”οΈOtherwise you’ll see 403 errors
3Deploy β–Έ New deployment β–Έ Web app
β€’ Execute asMe
β€’ Who has accessAnyone
4On the consent screen allow scopes:β€’ …/auth/documentsβ€’ …/auth/driveClick Advanced β€Ί Go if Google warns
5Copy 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

NodeFieldAction
GetMetaDataURL&lt;WEB_APP_URL&gt;?mode=meta&id={{ $json["id"] }}
FillDocumentURL&lt;WEB_APP_URL&gt;

> πŸ’‘ Prefer using an .env file? Add GAS_WEBAPP_URL=… and reference it as {{ $env.GAS_WEBAPP_URL }}. >


πŸ” F. Add Credentials


πŸ§ͺ G. First Run (Smoke Test)

  1. Switch the workflow Active.
  2. In the chat panel type /start.
  3. Bot lists templates β†’ pick one.
  4. Fill mandatory fields, optionally toggle blocks β†’ Confirm.
  5. πŸ”— Download PDF link appears β†’ β˜‘οΈ setup complete.

❌ H. Common Errors & Fixes

πŸ†˜ ErrorLikely CauseRemedy
403: Apps Script permission deniedWeb app access set to UserRedeploy as Anyone, re-authorize scopes
placeholder validation failedMissing required fieldProvide the listed values β†’ rerun DocProcess
unknown placeholders: …Template vs. agent mismatchCheck placeholder spelling (UPPER_CASE ASCII)
Template ID not foundPrompt list is oldRerun TemplateList β†’ update both prompts
Cannot find META_JSONNo meta block / wrong tagAdd [[META_JSON_START]] … [[META_JSON_END]], retry

βœ… Final Checklist


πŸ™‹β€β™‚οΈ Need Help with Customizations?

Reach out for consulting & support on LinkedIn: Γ–zgΓΌr Karateke

Full Documentation β†’ Notion

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

πŸ“– Importing guide Β· πŸ”‘ Credential setup