Segment and Structure PDF Documents using Gemini AI and Chunkr.ai - n8n Workflow

Automate robust PDF segmentation using Chunkr.ai and Google Gemini within this powerful n8n workflow. Generate structured JSON, HTML, and Markdown outputs, ideal for RAG and knowledge base pipelines. Download this n8n templates solution today.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • AI Engineers and Developers building Retrieval Augmented Generation (RAG) systems.

  • Data analysts needing automated, structured extraction from complex PDF documents.

  • Users looking for advanced document processing automation using n8n templates.

  • Organizations requiring reliable PDF to Markdown or HTML conversion.

Overview

Processing highly unstructured PDF documents is challenging, especially when attempting to accurately map content to its hierarchical structure (like sections and subsections). This sophisticated n8n workflow solves this by combining the powerful document parsing capabilities of Chunkr.ai with the advanced reasoning of the Google Gemini AI model. The result is a precise 'Golden Table of Contents' which is then used by a custom n8n node (Code) to slice the document's content into clean, contextually accurate segments. This robust n8n workflow provides outputs ready for downstream use, such as embedding into vector databases or publishing as structured HTML/Markdown, making it one of the most useful n8n templates for document automation.

How it Works

This comprehensive n8n workflow starts with a dual-path n8n trigger system. Users can either manually execute the workflow (using the manual n8n trigger to download a sample file) or trigger it via an external source using the 'When Executed by Another Workflow' n8n trigger with a PDF URL input.


  1. Initialization: The downloaded PDF is converted to base64 encoding and metadata is set using the 'Set File Name' n8n node.

  2. Chunkr Processing: The document is sent to Chunkr.ai via an HTTP Request n8n node for deep parsing, converting the PDF into structured chunks (segments, text, HTML, Markdown).

  3. Polling and Status Check: The workflow uses a Wait n8n node followed by a GET request to poll the Chunkr task status. A Switch n8n node routes the flow based on the result, ensuring the workflow stops if processing fails.

  4. AI Analysis Preparation: If successful, two auxiliary Code n8n nodes prepare data for the AI Agent: one extracts the first 10 chunks to check for an embedded Table of Contents, and another extracts all programmatic Section Headers as a fallback.

  5. AI ToC Generation: The 'Table of Content Agent' n8n node (an AI Agent backed by Google Gemini) uses the extracted text and structured JSON parsing to generate the definitive, nested Table of Contents, ensuring accurate hierarchy and levels.

  6. Document Segmentation: The critical 'Return each section individually' Code n8n node or 'Return the whole document' Code n8n node utilizes the AI-generated ToC to map headings back to the raw Chunkr output, accurately extracting the content corresponding to each section. This complex logic ensures precise boundaries.

  7. Final Output: The segmented data is either returned as individual n8n items (one per section) or aggregated into a single output item. Optional final steps use dedicated n8n node operations to convert the structured output into a fully formatted HTML or Markdown file.

Installation Guide

To use this powerful n8n workflow, follow these steps:


  1. Import: Copy the provided JSON data and paste it into your n8n instance using the 'New' menu > 'Import from JSON'.

  2. Chunkr.ai Credentials: The workflow requires authorization for the Chunkr.ai API. In the 'POST Chunkr Task' and 'GET Chunkr Task' HTTP Request n8n nodes, replace CHUNKRAPI_KEY> in the Authorization header parameter with your actual key.

  3. Google Gemini Credentials: Configure the credentials for the 'Google Gemini Chat Model' and 'Google Gemini Chat Model1' n8n node instances. This typically requires a Google AI Studio API key.

  4. Testing: Choose your preferred n8n trigger. If using 'When clicking ‘Execute workflow’', ensure it is enabled and click 'Execute Workflow' to test the full pipeline with the included sample PDF.

Node Details

ManualTrigger / ExecuteWorkflowTrigger (n8n trigger): Defines the entry point for the n8n workflow, supporting both manual testing and API-driven execution.
Convert the PDF to base64 (n8n node): Essential step for communicating binary file content to external APIs like Chunkr.ai via JSON payloads.
HTTP Request (POST Chunkr Task / GET Chunkr Task): Handles communication with the Chunkr.ai API. This n8n node initiates the parsing task and subsequently polls for the completion status.
Wait (n8n node): Introduces a necessary delay (10 seconds) before polling the Chunkr service, preventing immediate failure due to processing time.
Code (Take beginning of Document / Extract Sections headers): Custom JavaScript logic within this versatile n8n node prepares and structures the input data for the LLM agent, including text extraction and listing fallback headers.
AI Agent (Table of Content Agent): This specialized n8n node utilizes the connected Gemini LLM for complex reasoning, converting raw document cues into a strict, nested JSON structure (the desired Table of Contents).
Code (Return each section individually / Return the whole document): Contains the core segmentation logic. This robust Code n8n node performs two-pass mapping to anchor the AI-generated headings to the correct segments in the Chunkr output, extracting full section content (text, HTML, Markdown) based on these boundaries. This is the heart of this particular n8n workflow.
Code / HTML / Move Binary Data (n8n node set): Final n8n nodes used to compile the segmented document content back into a complete, downloadable HTML file.

Related n8n Workflows

Free

Nodes: 19 Nodes
Updated: December 26 2025
View all
Created by
Lukas Kunhardt
Lukas Kunhardt

Automation Expert with a focus on structured data extraction from messy inputs.

Featured*