To recover smaller PDF files from a large concatenated PDF where each page is an image (e.g., scanned documents), you must first determine the page ranges that correspond to each original PDF. Unfortunately, PDFs do not inherently store metadata indicating where one original file ends and another begins unless explicitly marked (e.g., via bookmarks, separators, or embedded metadata). Therefore, manual or external knowledge of the split points is required.
✅ Assumptions
- You have a large PDF composed of multiple smaller PDFs concatenated together.
- Each page in the large PDF is an image (no OCR text).
- You know the page ranges for each original PDF (e.g., pages 0–4, 5–9, etc.).
- You want to split the large PDF into individual files based on these ranges.
🛠️ Python Code Using PyPDF2
This script will take a large PDF file and split it into smaller PDFs based on a list of page ranges.
from PyPDF2 import PdfReader, PdfWriter
def split_large_pdf(input_pdf_path, output_pdf_paths, page_ranges):
"""
Splits a large PDF into smaller PDFs based on specified page ranges.
:param input_pdf_path: Path to the large concatenated PDF.
:param output_pdf_paths: List of output file paths for each small PDF.
:param page_ranges: List of tuples indicating start and end page indices (inclusive, 0-based).
"""
reader = PdfReader(input_pdf_path)
for output_path, (start, end) in zip(output_pdf_paths, page_ranges):
writer = PdfWriter()
for page_num in range(start, end + 1):
writer.add_page(reader.pages[page_num])
with open(output_path, 'wb') as output_file:
writer.write(output_file)
print(f"Saved: {output_path}")
# Example usage:
input_pdf = "large_scanned_document.pdf"
output_files = ["doc1.pdf", "doc2.pdf", "doc3.pdf"]
page_ranges = [(0, 4), (5, 9), (10, 15)] # Adjust based on your knowledge of the original splits
split_large_pdf(input_pdf, output_files, page_ranges)
🔍 How to Determine Page Ranges
Since the PDFs are scanned images, you can't extract text for indexing. Here are possible strategies to determine page ranges:
- Manual Inspection: Visually check the PDF and note where one document ends and another begins.
- Separator Pages: If the original documents were merged with a separator page (e.g., a blank page or a page with a specific watermark), you can detect that using image analysis tools (e.g.,
opencv, Pillow) and automate split detection.
- Known Structure: If each original document had a fixed number of pages (e.g., all were 10-page reports), split accordingly.
- Metadata Logging: If the original concatenation process was done via a script, consider logging the split points during the merge phase.
🧠 Optional: Splitting Based on Separator Pages
If the original documents were separated by a specific image (e.g., a blank page), you can use image comparison to detect those pages:
from pdf2image import convert_from_path
from PIL import Image
import numpy as np
import os
def detect_separator_pages(pdf_path, threshold=100000):
images = convert_from_path(pdf_path)
separator_indices = []
for i, img in enumerate(images):
img = img.convert('L') # Convert to grayscale
arr = np.array(img)
variance = np.var(arr)
if variance < threshold: # Low variance = mostly blank page
separator_indices.append(i)
return separator_indices
def generate_page_ranges(separator_indices, total_pages):
ranges = []
prev = 0
for idx in separator_indices:
ranges.append((prev, idx - 1))
prev = idx + 1
if prev <= total_pages - 1:
ranges.append((prev, total_pages - 1))
return ranges
# Example usage
separator_indices = detect_separator_pages("large_scanned_document.pdf")
reader = PdfReader("large_scanned_document.pdf")
total_pages = len(reader.pages)
ranges = generate_page_ranges(separator_indices, total_pages)
# Then use the ranges in the split_large_pdf function
This method uses image variance to detect blank pages, which could act as separators between original documents.
📦 Summary
- You must know or detect the page ranges to recover the original PDFs.
- Use
PyPDF2 to split the PDF based on those ranges.
- If separator pages were used, use image analysis to detect them.
- Without external knowledge or markers, it's not possible to recover the original files programmatically.
This approach gives you a clean, reusable method to recover the original documents once the split points are known.