Skip to main content

Segment PDFs by table of contents with Gemini AI and Chunkr.ai

Workflow preview

Workflow preview
100%
Segment PDFs by table of contents with Gemini AI and Chunkr.ai preview
Open on n8n.io

Important notice

This workflow is provided as-is. Please review and test before using in production.

1. Workflow Overview

Intelligently Segment PDFs by Table of Contents This workflow empowers you to automatically process PDF documents, intelligently identify or generate a hierarchical Table of Contents (ToC), and the...

Best for

  • Document Extraction automation workflows
  • AI Summarization automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

n8n-nodes-base.manualtrigger, n8n-nodes-base.switch, @n8n/n8n-nodes-langchain.lmchatgooglegemini, @n8n/n8n-nodes-langchain.outputparserstructured, @n8n/n8n-nodes-langchain.outputparserautofixing, n8n-nodes-base.set, n8n-nodes-base.executeworkflowtrigger, n8n-nodes-base.code

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Lukas Kunhardt.

Original n8n.io source

1.1 Workflow description

Title
Segment PDFs by table of contents with Gemini AI and Chunkr.ai
Workflow name
Segment PDFs by table of contents with Gemini AI and Chunkr.ai

Intelligently Segment PDFs by Table of Contents

This workflow empowers you to automatically process PDF documents, intelligently identify or generate a hierarchical Table of Contents (ToC), and then segment the entire document's content based on these ToC headings. It effectively breaks down a large PDF into its constituent sections, each paired with its corresponding heading and hierarchical level.

Why It's Useful

Unlock the true structure of your PDFs for granular access and advanced processing:

  • AI Agent Tool: A key use case is to provide this workflow as a tool to an AI agent. The agent can then use the segmented output to "read" and navigate to specific sections of a document to answer questions, extract information, or perform tasks with much greater accuracy and efficiency.
  • Targeted Content Extraction: Programmatically pull out specific chapters or subsections for focused analysis, summarization, reporting, or repurposing content.
  • Enhanced RAG Systems: Improve your Retrieval Augmented Generation (RAG) pipelines by feeding them well-defined, contextually relevant document sections instead of entire, monolithic PDFs. This leads to more precise AI-generated responses.
  • Modular Document Processing: Process different parts of a document using distinct logic in subsequent n8n workflows by acting on individual sections.
  • Data Preparation: Seamlessly convert lengthy PDFs into a structured format where each section (including its heading, level, and content in multiple formats) becomes a distinct, manageable item.

How It Works

  1. Ingestion & Advanced Parsing: The workflow ingests a PDF (via a provided URL or a pre-set one for manual runs). It then utilizes Chunkr.ai to perform Optical Character Recognition (OCR) and parse the document into detailed structural elements, extracting text, HTML, and Markdown for each segment.
  2. AI-Powered Table of Contents Generation: A Google Gemini AI model analyzes the initial pages of the document (where a ToC often resides) along with section headers extracted by Chunkr as a fallback. This allows it to construct an accurate, hierarchical Table of Contents in a structured JSON format, even if the PDF lacks an explicit ToC or if it's poorly formatted.
  3. Precise Content Segmentation: Sophisticated custom code then meticulously maps the AI-generated ToC headings to their corresponding content within the parsed document from Chunkr. It intelligently determines the precise start and end of each section.
  4. Structured & Flexible Output:
    • The primary output provides each identified section as an individual n8n item. Each item includes the heading text, its hierarchical level (e.g., 1, 1.1, 2), and the full content of that section in Text, HTML, and Markdown formats.
    • Optionally, the workflow can also reconstruct the entire document into a single, navigable HTML file or a clean Markdown file.

What You Need

To run this workflow, you'll need:

  • Input PDF:
    • When triggered by another workflow: A URL pointing to the PDF document.
    • When triggered manually: The workflow uses a pre-configured sample PDF from Google Drive for demonstration (this can be customized).
  • Chunkr.ai API Key: Required for the initial parsing and OCR of the PDF document. You'll need to insert this into the relevant HTTP Request nodes.
  • Google Gemini API Credentials: Necessary for the AI model to intelligently generate the Table of Contents. This should be configured in the Google Gemini Chat Model nodes.

Outputs

The workflow primarily generates:

  • Individual Document Sections: A series of n8n items. Each item represents a distinct section of the PDF and contains:
    • heading: The text of the section heading.
    • headingLevel: The hierarchical level of the heading (e.g., 1 for H1, 2 for H2).
    • sectionText: The plain text content of the section.
    • sectionHTML: The HTML content of the section.
    • sectionMarkdown: The Markdown content of the section.

Alternatively, you can configure the workflow to output:

  • Full Reconstructed Document:
    • A single HTML file representing the entire processed document.
    • A single Markdown file representing the entire processed document.

This workflow is ideal for anyone looking to deconstruct PDFs into meaningful, manageable parts for advanced automation, AI integration, or detailed content analysis.

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - When clicking ‘Execute workflow’

Type / Role
n8n-nodes-base.manualTrigger - manualTrigger
Config choices
Version 1

Block 2 - Status is:

Type / Role
n8n-nodes-base.switch - switch
Config choices
Version 3.2

Block 3 - Google Gemini Chat Model

Type / Role
@n8n/n8n-nodes-langchain.lmChatGoogleGemini - lmChatGoogleGemini
Config choices
Version 1

Block 4 - Structured Output Parser

Type / Role
@n8n/n8n-nodes-langchain.outputParserStructured - outputParserStructured
Config choices
Version 1.2

Block 5 - Auto-fixing Output Parser

Type / Role
@n8n/n8n-nodes-langchain.outputParserAutofixing - outputParserAutofixing
Config choices
Version 1

Block 6 - Set File Name

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 7 - When Executed by Another Workflow

Type / Role
n8n-nodes-base.executeWorkflowTrigger - executeWorkflowTrigger
Config choices
Version 1.1

Block 8 - Extract Sections headers as fallback

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 9 - Take beginning of Document to look for Table of contents

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 10 - Stop and Error

Type / Role
n8n-nodes-base.stopAndError - stopAndError
Config choices
Version 1

Block 11 - Google Gemini Chat Model1

Type / Role
@n8n/n8n-nodes-langchain.lmChatGoogleGemini - lmChatGoogleGemini
Config choices
Version 1

Block 12 - Sticky Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 13 - Convert the PDF to base64

Type / Role
n8n-nodes-base.extractFromFile - extractFromFile
Config choices
Version 1

Block 14 - Sticky Note1

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 15 - Sticky Note2

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 16 - Download PDF from URL

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.2

Block 17 - Download PDF from Google Drive

Type / Role
n8n-nodes-base.googleDrive - googleDrive
Config choices
Version 3

Block 18 - Sticky Note3

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 19 - Wait Before Polling the Chunkr Result

Type / Role
n8n-nodes-base.wait - wait
Config choices
Version 1.1

Block 20 - Sticky Note4

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 21 - Sticky Note5

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 22 - Return each section individually

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 23 - Table of Content Agent

Type / Role
@n8n/n8n-nodes-langchain.agent - agent
Config choices
Version 2

Block 24 - Return the whole document

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Showing the first 24 of 36 workflow blocks. Download the JSON for the full node graph.

3. Summary Table

Workflow Segment PDFs by table of contents with Gemini AI and Chunkr.ai
Complexity advanced
Nodes 36
Categories Document Extraction, AI Summarization
Author Lukas Kunhardt
Published 05 Jun 2025

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/4697/4697.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Segment PDFs by table of contents with Gemini AI and Chunkr.ai do?

Intelligently Segment PDFs by Table of Contents This workflow empowers you to automatically process PDF documents, intelligently identify or generate a hierarchical Table of Contents (ToC), and the...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Document Extraction, AI Summarization use case.