This tool extracts a structured outline (Title, H1, H2, H3) from text-based PDFs using layout and font-based heuristics. It works fully offline, fits within Docker image constraints, and outputs a clean hierarchical JSON for each input document.
- Uses pdfplumber to extract text, layout (font size, position), and structure from each PDF page
- Determines the document title by selecting the largest and most centered text on the first page
- Remaining text is grouped by font size and numerically prefixed patterns (like
1.,1.2, etc.) to infer heading levels - Heading levels are mapped to:
H1: Largest heading size after titleH2: Next size downH3: Next size down (optional)
- Outputs a structured JSON outline with heading
level,text, andpagenumber
- Python 3.9
- pdfplumber: For PDF parsing and text extraction
- Standard Python libraries:
json,re,pathlib
No ML models or training involved — heuristic, layout-aware approach ensures light weight and fast processing.
.
├── input/ # Place your PDF files here
├── output/ # JSON outputs appear here
├── main.py # Main logic for extraction
├── requirements.txt # Python dependencies
├── Dockerfile # Docker build file
├── Commands to run.txt # Sample terminal commands
└── README.md # You're reading it!
Recommended for reproducible and dependency-free execution.
-
Place your PDF(s) in the
input/folder. -
Build the Docker image:
docker build --platform linux/amd64 -t mysolution:latest .- Run the container:
docker run --rm \
-v /absolute/path/to/input:/app/input \
-v /absolute/path/to/output:/app/output \
--network none mysolution:latestReplace /absolute/path/to/input and /absolute/path/to/output with absolute paths on your system.
- Install requirements:
pip install -r requirements.txt-
Place your PDF(s) in
input/folder. -
Run the script:
python main.pyOutputs will be saved as .json in the output/ directory.
Each output JSON will follow this structure:
{
"title": "Document Title",
"outline": [
{"level": "H1", "text": "Introduction", "page": 1},
{"level": "H2", "text": "1.1 Background", "page": 2},
{"level": "H3", "text": "1.1.1 Problem Statement", "page": 2}
]
}- Scanned/image-only PDFs are not supported (OCR not implemented)
- Works best with digital/text-based PDFs
- Ensure Docker volumes are mounted correctly and paths are absolute
This project is provided as-is for educational and prototyping purposes. Feel free to modify and extend.