pdf_oxide 0.3.22

The fastest Rust PDF library with text extraction: 0.8ms mean, 100% pass rate on 3,830 PDFs. 5× faster than pdf_extract, 17× faster than oxidize_pdf. Extract, create, and edit PDFs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env python3
"""
Benchmark all PDF libraries against our Rust library.

Tests the top 10 Python PDF libraries + our Rust library on the PDF corpus:
1. pdf_oxide (our Rust library)
2. PyMuPDF (pymupdf)
3. pymupdf4llm
4. pdfplumber
5. pypdf
6. pdfminer.six
7. Camelot
8. tabula-py
9. pikepdf
10. borb
11. pypdfium2
12. playa-pdf

Outputs markdown files to separate directories for comparison.
"""

import argparse
import json
import sys
import time
from pathlib import Path
from typing import TypedDict


# Test if libraries are available
AVAILABLE_LIBRARIES = {}


def check_library_availability():
    """Check which libraries are installed."""
    libraries = {
        "pymupdf": "fitz",
        "pymupdf4llm": "pymupdf4llm",
        "pdfplumber": "pdfplumber",
        "pypdf": "pypdf",
        "pdfminer.six": "pdfminer",
        "pikepdf": "pikepdf",
        "borb": "borb",
        "pypdfium2": "pypdfium2",
        "playa-pdf": "playa",
    }

    for name, import_name in libraries.items():
        try:
            __import__(import_name)
            AVAILABLE_LIBRARIES[name] = True
            print(f"{name} available")
        except ImportError:
            AVAILABLE_LIBRARIES[name] = False
            print(f"{name} NOT installed")

    # Check for our Rust library
    try:
        import pdf_oxide  # noqa: F401

        AVAILABLE_LIBRARIES["pdf_oxide"] = True
        print("✓ pdf_oxide (Rust) available")
    except ImportError:
        AVAILABLE_LIBRARIES["pdf_oxide"] = False
        print("✗ pdf_oxide (Rust) NOT installed")

    print()


def extract_with_pdf_oxide(pdf_path, output_path):
    """Extract text with our Rust library."""
    import pdf_oxide

    doc = pdf_oxide.PdfDocument(str(pdf_path))
    markdown = doc.to_markdown_all(detect_headings=True)

    with open(output_path, "w", encoding="utf-8") as f:
        f.write(markdown)

    return len(markdown)


def extract_with_pymupdf(pdf_path, output_path):
    """Extract text with PyMuPDF."""
    import fitz

    doc = fitz.open(str(pdf_path))
    text_parts = []

    for page in doc:
        text = page.get_text()
        text_parts.append(text)

    markdown = "\n\n".join(text_parts)

    with open(output_path, "w", encoding="utf-8") as f:
        f.write(markdown)

    doc.close()
    return len(markdown)


def extract_with_pymupdf4llm(pdf_path, output_path):
    """Extract text with pymupdf4llm."""
    import pymupdf4llm

    markdown = pymupdf4llm.to_markdown(str(pdf_path))

    with open(output_path, "w", encoding="utf-8") as f:
        f.write(markdown)

    return len(markdown)


def extract_with_pdfplumber(pdf_path, output_path):
    """Extract text with pdfplumber."""
    import pdfplumber

    text_parts = []
    with pdfplumber.open(str(pdf_path)) as pdf:
        for page in pdf.pages:
            text = page.extract_text()
            if text:
                text_parts.append(text)

    markdown = "\n\n".join(text_parts)

    with open(output_path, "w", encoding="utf-8") as f:
        f.write(markdown)

    return len(markdown)


def extract_with_pypdf(pdf_path, output_path):
    """Extract text with pypdf."""
    from pypdf import PdfReader

    reader = PdfReader(str(pdf_path))
    text_parts = []

    for page in reader.pages:
        text = page.extract_text()
        if text:
            text_parts.append(text)

    markdown = "\n\n".join(text_parts)

    with open(output_path, "w", encoding="utf-8") as f:
        f.write(markdown)

    return len(markdown)


def extract_with_pdfminer(pdf_path, output_path):
    """Extract text with pdfminer.six."""
    from pdfminer.high_level import extract_text

    text = extract_text(str(pdf_path))

    with open(output_path, "w", encoding="utf-8") as f:
        f.write(text)

    return len(text)


def extract_with_playa(pdf_path, output_path):
    """Extract text with PLAYA-PDF."""
    import playa

    # PLAYA-PDF is more for PDF exploration than text extraction
    # Basic text extraction only
    with playa.open(pdf_path) as pdf:
        text = "\n\n".join(pdf.pages.map(playa.Page.extract_text))

    with open(output_path, "w", encoding="utf-8") as f:
        f.write(text)

    return len(text)


def extract_with_pikepdf(pdf_path, output_path):
    """Extract text with pikepdf (basic extraction)."""
    import pikepdf

    # pikepdf is more for PDF manipulation than text extraction
    # Basic text extraction only
    text_parts = []

    with pikepdf.open(str(pdf_path)) as pdf:
        for _ in pdf.pages:
            # Very basic extraction - pikepdf doesn't have built-in text extraction
            text_parts.append(f"[Page {len(text_parts) + 1}]")

    markdown = "\n\n".join(text_parts)

    with open(output_path, "w", encoding="utf-8") as f:
        f.write(markdown)

    return len(markdown)


def extract_with_borb(pdf_path, output_path):
    """Extract text with borb."""
    from borb.pdf import PDF
    from borb.toolkit.text.simple_text_extraction import SimpleTextExtraction

    text_parts = []

    with open(pdf_path, "rb") as pdf_file:
        doc = PDF.loads(pdf_file)

        for page_num in range(len(doc.get_document_info().get_number_of_pages())):
            extractor = SimpleTextExtraction()
            doc.get_page(page_num).render_to_device(extractor)
            text = extractor.get_text()
            if text:
                text_parts.append(text)

    markdown = "\n\n".join(text_parts)

    with open(output_path, "w", encoding="utf-8") as f:
        f.write(markdown)

    return len(markdown)


def extract_with_pypdfium2(pdf_path, output_path):
    """Extract text with pypdfium2."""
    import pypdfium2 as pdfium

    pdf = pdfium.PdfDocument(str(pdf_path))
    text_parts = []

    for page in pdf:
        textpage = page.get_textpage()
        text = textpage.get_text_range()
        if text:
            text_parts.append(text)
        textpage.close()
        page.close()

    pdf.close()

    markdown = "\n\n".join(text_parts)

    with open(output_path, "w", encoding="utf-8") as f:
        f.write(markdown)

    return len(markdown)


# Library extractors mapping
EXTRACTORS = {
    "pdf_oxide": extract_with_pdf_oxide,
    "pymupdf": extract_with_pymupdf,
    "pymupdf4llm": extract_with_pymupdf4llm,
    "pdfplumber": extract_with_pdfplumber,
    "pypdf": extract_with_pypdf,
    "pdfminer.six": extract_with_pdfminer,
    "pikepdf": extract_with_pikepdf,
    "borb": extract_with_borb,
    "pypdfium2": extract_with_pypdfium2,
    "playa-pdf": extract_with_playa,
}


def benchmark_library(library_name, pdf_files, output_dir):
    """Benchmark a single library on all PDFs."""
    if not AVAILABLE_LIBRARIES.get(library_name, False):
        print(f"Skipping {library_name} (not installed)")
        return None

    extractor = EXTRACTORS.get(library_name)
    if not extractor:
        print(f"No extractor for {library_name}")
        return None

    print(f"\n{'=' * 60}")
    print(f"Benchmarking: {library_name}")
    print(f"{'=' * 60}")

    class BenchmarkResults(TypedDict, total=False):
        library: str
        total_pdfs: int
        successful: int
        failed: int
        total_time: float
        total_output_size: int
        errors: list[str]
        times: list[float]
        avg_time: float
        avg_output_size: float
        success_rate: float

    results: BenchmarkResults = {
        "library": library_name,
        "total_pdfs": len(pdf_files),
        "successful": 0,
        "failed": 0,
        "total_time": 0.0,
        "total_output_size": 0,
        "errors": [],
        "times": [],
    }

    for i, pdf_path in enumerate(pdf_files, 1):
        output_file = output_dir / f"{pdf_path.stem}.md"

        try:
            start_time = time.time()
            output_size = extractor(pdf_path, output_file)
            elapsed = time.time() - start_time

            results["successful"] += 1
            results["total_time"] += elapsed
            results["total_output_size"] += output_size
            results["times"].append(elapsed)

            print(
                f"  [{i}/{len(pdf_files)}] ✓ {pdf_path.name} ({elapsed:.3f}s, {output_size} bytes)"
            )

        except Exception as e:
            results["failed"] += 1
            error_msg = f"{pdf_path.name}: {e!s}"
            results["errors"].append(error_msg)
            print(f"  [{i}/{len(pdf_files)}] ✗ {pdf_path.name} - {str(e)[:100]}")

    # Calculate statistics
    if results["successful"] > 0:
        results["avg_time"] = results["total_time"] / results["successful"]
        results["avg_output_size"] = results["total_output_size"] / results["successful"]
        results["success_rate"] = (results["successful"] / results["total_pdfs"]) * 100
    else:
        results["avg_time"] = 0
        results["avg_output_size"] = 0
        results["success_rate"] = 0

    print(f"\nResults for {library_name}:")
    print(
        f"  Success: {results['successful']}/{results['total_pdfs']} ({results['success_rate']:.1f}%)"
    )
    print(f"  Total time: {results['total_time']:.2f}s")
    print(f"  Avg time/PDF: {results['avg_time'] * 1000:.1f}ms")
    print(f"  Total output: {results['total_output_size']:,} bytes")

    return results


def main():
    parser = argparse.ArgumentParser(description="Benchmark all PDF libraries")
    parser.add_argument(
        "--pdfs", default="test_datasets/pdfs", help="Directory containing PDFs to test"
    )
    parser.add_argument(
        "--output", default="test_datasets/benchmark_outputs", help="Output directory for results"
    )
    parser.add_argument(
        "--libraries", nargs="+", help="Specific libraries to test (default: all available)"
    )
    parser.add_argument("--limit", type=int, help="Limit number of PDFs to test")

    args = parser.parse_args()

    # Check library availability
    print("Checking library availability...\n")
    check_library_availability()

    # Find all PDFs
    pdf_dir = Path(args.pdfs)
    if not pdf_dir.exists():
        print(f"Error: PDF directory not found: {pdf_dir}")
        sys.exit(1)

    pdf_files = sorted(pdf_dir.rglob("*.pdf"))
    if not pdf_files:
        print(f"Error: No PDFs found in {pdf_dir}")
        sys.exit(1)

    if args.limit:
        pdf_files = pdf_files[: args.limit]

    print(f"Found {len(pdf_files)} PDFs to test\n")

    # Create output directory
    output_base = Path(args.output)
    output_base.mkdir(parents=True, exist_ok=True)

    # Determine which libraries to test
    if args.libraries:
        libraries_to_test = args.libraries
    else:
        libraries_to_test = [lib for lib, available in AVAILABLE_LIBRARIES.items() if available]

    print(f"Testing libraries: {', '.join(libraries_to_test)}\n")

    # Benchmark each library
    all_results = []

    for library_name in libraries_to_test:
        library_output_dir = output_base / library_name
        library_output_dir.mkdir(parents=True, exist_ok=True)

        results = benchmark_library(library_name, pdf_files, library_output_dir)
        if results:
            all_results.append(results)

    # Save summary report
    summary_file = output_base / "benchmark_summary.json"
    with open(summary_file, "w") as f:
        json.dump(all_results, f, indent=2)

    print(f"\n{'=' * 60}")
    print("BENCHMARK SUMMARY")
    print(f"{'=' * 60}\n")

    # Sort by average time (fastest first)
    all_results.sort(key=lambda x: x["avg_time"])

    print(f"{'Library':<20} {'Success':<12} {'Total Time':<12} {'Avg/PDF':<12} {'Output Size':<15}")
    print(f"{'-' * 20} {'-' * 12} {'-' * 12} {'-' * 12} {'-' * 15}")

    for result in all_results:
        print(
            f"{result['library']:<20} "
            f"{result['successful']}/{result['total_pdfs']:<9} "
            f"{result['total_time']:>10.2f}s "
            f"{result['avg_time'] * 1000:>10.1f}ms "
            f"{result['total_output_size']:>13,} bytes"
        )

    print(f"\nResults saved to: {output_base}")
    print(f"Summary: {summary_file}")

    # Show relative performance
    if len(all_results) > 1:
        baseline = all_results[0]
        print(f"\n{'=' * 60}")
        print(f"RELATIVE PERFORMANCE (vs {baseline['library']})")
        print(f"{'=' * 60}\n")

        for result in all_results[1:]:
            speedup = result["avg_time"] / baseline["avg_time"]
            print(f"{result['library']:<20} {speedup:>6.1f}× slower")


if __name__ == "__main__":
    main()