Document Automation Agent: Generate PDFs from Google Docs Templates - n8n Workflow

Automate legal and business document creation using this advanced n8n workflow. Integrates Google Gemini, Drive, and Apps Script to generate custom, ready-to-sign PDFs instantly from Google Docs templates.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Legal Professionals & HR: Need to generate contracts, NDAs, or employment forms quickly and without errors.
Technical Writers/Integrators: Looking for advanced n8n templates that showcase LangChain agents, custom tooling, and deep Google service integration.
Business Owners: Seeking to automate repetitive administrative tasks involving document generation.
n8n Automation Specialists: Users who want to implement a highly reliable, two-stage document processing n8n workflow.

Overview

This comprehensive n8n workflow, dubbed 'DocAgent', solves the costly and error-prone problem of manual document filling. Instead of copying templates, this automation uses a conversational AI agent to intelligently collect all necessary dynamic data from the user.

The core of this powerful n8n workflow involves a Gemini agent that orchestrates the entire process, using tools to fetch template metadata (placeholders and conditional blocks) before initiating the final document assembly via a dedicated sub-workflow. Every new Google Doc template placed in your Drive folder becomes instantly available for use without editing the main n8n node logic. This ensures a scalable solution, turning Google Docs into dynamic, ready-to-sign PDFs instantly. This specific n8n workflow is a prime example of leveraging custom code (Google Apps Script) alongside standard n8n nodes for complex, file-based operations.

How it Works

The entire process is initiated by the When chat message received n8n trigger, activating the document agent.

Phase 1: Agent Interaction (Main n8n Workflow)


  1. Start & Discovery: The When chat message received n8n trigger starts the chat. The DocAgent (a LangChain Agent) retrieves the list of available document templates (pre-fetched via the Template List n8n node and injected into the prompt).

  2. Metadata Fetch: Once the user selects a template, the Agent calls the GetMetaData tool. This tool, an HTTP request to Google Apps Script, retrieves the document's structure, including mandatory placeholders and optional conditional blocks. This is a crucial n8n node for dynamic data processing.

  3. Data Collection: The Agent enters a conversational loop, guided by the metadata, prompting the user for every required value and determining which conditional blocks to include.

  4. Process Initiation: Upon user approval, the Agent calls the DocProcess tool, which is an n8n workflow reference, passing the collected data and template IDs to the sub-workflow.

Phase 2: Document Processing (Sub-workflow)


  1. Input & Validation: The sub-workflow starts with the When Executed by Another Workflow n8n trigger. It first uses the User Choice Match Check n8n node (powered by Gemini) to validate that the Agent correctly matched the template name with its ID.

  2. Formatting Correction: The Format Control n8n node uses Gemini again to ensure the collected user data is perfectly formatted into the required JSON structure dictated by the template's metadata. The Formatting Correction n8n node (Code node) cleans the LLM output.

  3. Document Copying: The CopyTemplate Google Drive n8n node duplicates the original template into a designated output folder.

  4. Document Filling: The FillDocument HTTP Request n8n node sends the copied document ID and the prepared JSON data to the Google Apps Script endpoint. This script performs the actual document modification (placeholder substitution and removal of excluded conditional blocks).

  5. Delivery or Error Handling: An if n8n node checks the status. On success, the Generate Download Link n8n node retrieves the application/pdf export link. On failure, a Switch n8n node routes errors (e.g., missing fields) back to the Agent so it can re-prompt the user, making this a highly resilient n8n workflow structure.

Installation Guide

To deploy this advanced n8n workflow, follow these steps:


  1. Import the n8n Workflow: Import the provided JSON data into your n8n instance using the 'Import Workflow' option in the settings panel.

  2. Google Drive Setup: Prepare your Google Drive folders (Template folder, Generated output folder) and note the ID of the template parent folder (PARENTID>).

  3. Configure Template List: Open the Template List n8n node. Update the URL query parameter '%3CYOURPARENTID%3E' with your actual template folder ID. Execute this n8n node and manually copy the resulting JSON list.

  4. Update Agent Prompts: Paste the copied template list into the System Prompts of the DocAgent n8n node and the User Choice Match Check n8n node (where it says {{ YOURMANUALTOADDTEMPLATELIST }}). This optimizes token usage.

  5. Deploy Google Apps Script (GAS): Deploy the required GAS files (GetMetaData.gs and FillDocument.gs) as a Web App (access: 'Anyone', execute as: 'Me'). Note the Web App URL (DEPLOY_ID>).

  6. Update HTTP Nodes: Update the URLs in the GetMetaData, FillDocument, and GetMetaData2 n8n nodes with your GAS Web App URL.

  7. Credentials: Configure the required credentials: Google Drive (OAuth2, Full Access), Google Docs (OAuth2), Google Gemini (API Key), and optionally, Postgres for memory management. Every n8n node requiring credentials must be linked correctly.

  8. Sub-workflow Link: Ensure the DocProcess n8n node (in the main workflow) references the correct ID of the sub-workflow that starts with the When Executed by Another Workflow n8n trigger.

Node Details

This complex n8n workflow utilizes several specialized n8n nodes:

When chat message received (n8n trigger): The starting n8n trigger for the conversational agent flow, exposing a public webhook.
DocAgent (LangChain Agent n8n node): The core intelligence, using the Gemini model and a detailed system prompt defining the step-by-step Turkish legal drafting process.
Google Gemini Chat Model (n8n node): Provides the large language model capabilities (using models/gemini-2.5-pro) for the agent and for structured data validation.
Postgres Chat Memory (n8n node): Used by the DocAgent to maintain context and conversation history across multiple messages.
GetMetaData (n8n node) & GetMetaData2 (n8n node): HTTP Request nodes calling the Google Apps Script to dynamically fetch placeholders and conditional block structures of the selected document template.
DocProcess (toolWorkflow n8n node): The link that delegates the document filling task to the dedicated sub-workflow, passing inputs via the workflow's n8n trigger mechanism.
User Choice Match Check (LangChain Chain LLM n8n node): Uses Gemini for AI-based validation of the user's template selection against the internal template catalog.
Format Control (LangChain Chain LLM n8n node): A critical n8n node that uses the LLM to structure the raw user inputs into the precise JSON format required by the document filling script.
CopyTemplate (Google Drive n8n node): Performs the necessary operation of duplicating the chosen template before filling.
FillDocument (n8n node): The final HTTP request n8n node that sends the structured data payload to the Apps Script service to generate the final document.


  • Switch (n8n node): A key flow control n8n node in the sub-workflow for robust error handling, differentiating between technical template errors and incomplete information from the user.

Related n8n Workflows

Free

Nodes: 16 Nodes
Updated: December 26 2025
View all
Created by
Ozgur Karateke
Ozgur Karateke

Featured*