B.L.A.S.T. OCR
Code Read Against Source, 2026-09-10

How to Create Searchable Sandwich PDFs with Invisible Text in Python

Primary query: create searchable pdf python · raw markdown

How do you create a searchable PDF sandwich with invisible text in Python?

B.L.A.S.T. generates searchable PDF sandwiches with SearchablePDFGenerator, pairing PyMuPDF (fitz) with a ReportLab fallback. It overlays recognized text as an invisible font layer exactly over the raster image coordinates, preserving 100% visual fidelity while enabling search, highlighting, and copy-paste. Verified in blast_ocr/core/searchable_pdf.py.

CLI Quickstart

git clone https://github.com/Ibrahim-Salman19/OCR.git && cd OCR
    pip install -r requirements.txt
    python -m blast_ocr.cli scanned_contract.pdf --formats pdf

Python implementation: exact sandwich geometry

The high-level path is pipeline.process_job(..., formats=["pdf"]) — see the high-throughput guide. To call the sandwich generator directly with your own OCR detections:

from blast_ocr.core.searchable_pdf import SearchablePDFGenerator

    # create_from_page_images() takes matching lists of page images and
    # per-page OCR detections, and picks PyMuPDF if available, else ReportLab
    output_pdf_path = SearchablePDFGenerator.create_from_page_images(
        page_images=["samples/scanned_contract_p1.png"],
        page_ocr_results=[
            {
                "details": [
                    {"bbox": [100, 150, 420, 175], "text": "CONFIDENTIAL SETTLEMENT AGREEMENT", "confidence": 0.98}
                ]
            }
        ],
        output_pdf_path="output/searchable_contract.pdf",
        title="Settlement Agreement",
    )
    print(f"Searchable PDF generated at: {output_pdf_path}")

page_ocr_results[i]["details"] is one of two payload shapes SearchablePDFGenerator accepts natively (_extract_text_boxes) — a flat list of {bbox, text, confidence} items per page.

Author & Engineering Authority

Engineered & maintained by Ibrahim Salman, Full-Stack Software Engineer & AI Systems Architect (UET Taxila).