Skip to content

pdf parser#173

Open
Ro-shni wants to merge 2 commits intowikimedia:mainfrom
Ro-shni:pdf-int
Open

pdf parser#173
Ro-shni wants to merge 2 commits intowikimedia:mainfrom
Ro-shni:pdf-int

Conversation

@Ro-shni
Copy link
Copy Markdown

@Ro-shni Ro-shni commented Mar 30, 2026

How It Works
User submits a PDF URL (e.g., https://upload.wikimedia.org/wikipedia/commons/.../file.pdf) at /pdf
PDF is downloaded and each page rendered to a PNG image via Imagick
Each page image is classified using contour analysis:
Text pages: only small text-like regions detected
Image+Text pages: mix of large image regions and small text regions
Image pages: only large image regions (these are skipped for OCR)
Only Text and Image+Text pages are sent to the selected OCR engine
Results show per-page classifications, per-page text, and combined full text
API Usage

GET /api/pdf?image=<PDF_URL>&engine=google&langs[]=en&start_page=1&end_page=10
Returns JSON with per-page classifications, OCR text, and summary.

To Run

Install Imagick (macOS)

pecl install imagick
brew install ghostscript

Or use Docker

./docker/setup.sh && ./docker/run.sh

Then visit http://localhost:8000/pdf

New Files Created
PdfProcessor.php - Core PDF processing service that:

Downloads PDFs from Wikimedia Commons URLs
Converts PDF pages to images using Imagick (ImageMagick)
Classifies each page as Text, Image, or Image+Text using GD-based contour analysis (ported from the Python app.py logic)
Uses flood-fill connected component analysis at 1/4 resolution for performance
Supports page range filtering (start_page, end_page)
PdfPageClassification.php - Immutable DTO for a classified page (page number, type, image data, hasText() helper)

PdfResult.php - Immutable DTO for the full PDF result (page classifications, per-page OCR text, combined text, warnings, helpers for OCR'd/skipped pages)

PDF processing with:

PDF URL input with page range options
Engine and language selection
Processing summary (total/OCR'd/skipped pages)
Page classification table with color-coded labels
Per-page text output in expandable panels
Full combined text with copy-to-clipboard

will further try imrpving the ui and make the code more modular and try fixing:
-Possible crash in Transkribus line model mapping (missing key handling)
-Transkribus polling loop has no timeout (can block indefinitely)

  • Docker setup mismatch (PHP/Node versions not aligned with project)
    -OCR response uses JSON_NUMERIC_CHECK which can corrupt numeric text
    -Image URL validation isn’t strict enough (edge cases + SSRF risk)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant