Skip to content

fix(text): keep strings intact when extracting overlapping and layered text - #98

Merged
Mythie merged 3 commits into
mainfrom
fix/text-extraction
Sep 25, 2026
Merged

Mythie merged 3 commits into
mainfrom
fix/text-extraction

Conversation

@Mythie

@Mythie Mythie commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Lines are built from runs (strings drawn as a unit) that only share a line
when their glyphs don't collide, instead of x-sorting every glyph on a
baseline. Text in form XObjects is now extracted, and Tf/q-Q, rise, Tw and
CMap decoding follow the spec.

…d text

Lines are built from runs (strings drawn as a unit) that only share a line
when their glyphs don't collide, instead of x-sorting every glyph on a
baseline. Text in form XObjects is now extracted, and Tf/q-Q, rise, Tw and
CMap decoding follow the spec.
@vercel

vercel Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
core Ready Ready Preview Sep 25, 2026 5:30am UTC

@github-actions

github-actions Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark Results

Comparison

Load PDF

Benchmark Mean p99 RME Samples
libpdf 19.57ms 25.26ms ±3.1% 26
pdf-lib 218.00ms 227.49ms ±1.3% 10
@cantoo/pdf-lib 212.74ms 220.76ms ±1.2% 10

Create blank PDF

Benchmark Mean p99 RME Samples
libpdf 78μs 172μs ±2.7% 6429
pdf-lib 364μs 1.44ms ±3.3% 1376
@cantoo/pdf-lib 360μs 1.51ms ±2.6% 1395

Add 10 pages

Benchmark Mean p99 RME Samples
libpdf 123μs 224μs ±1.1% 4054
pdf-lib 448μs 2.00ms ±3.5% 1116
@cantoo/pdf-lib 417μs 2.20ms ±4.1% 1199

Draw 50 rectangles

Benchmark Mean p99 RME Samples
libpdf 348μs 763μs ±1.1% 1438
pdf-lib 1.33ms 4.81ms ±6.2% 376
@cantoo/pdf-lib 1.65ms 4.51ms ±6.3% 303

Load and save PDF

Benchmark Mean p99 RME Samples
libpdf 19.88ms 27.87ms ±3.7% 26
pdf-lib 319.68ms 334.84ms ±2.2% 10
@cantoo/pdf-lib 576.27ms 603.91ms ±2.6% 10

Load, modify, and save PDF

Benchmark Mean p99 RME Samples
libpdf 344.54ms 358.87ms ±1.8% 10
pdf-lib 308.93ms 323.58ms ±1.4% 10
@cantoo/pdf-lib 550.31ms 601.44ms ±3.9% 10

Extract single page from 100-page PDF

Benchmark Mean p99 RME Samples
libpdf 3.62ms 4.31ms ±0.7% 138
pdf-lib 9.04ms 12.34ms ±2.3% 56
@cantoo/pdf-lib 9.32ms 12.01ms ±1.8% 54

Split 100-page PDF into single-page PDFs

Benchmark Mean p99 RME Samples
libpdf 39.26ms 43.29ms ±2.2% 13
pdf-lib 75.90ms 94.78ms ±10.9% 7
@cantoo/pdf-lib 78.37ms 85.27ms ±6.3% 7

Split 2000-page PDF into single-page PDFs (0.9MB)

Benchmark Mean p99 RME Samples
libpdf 731.39ms 731.39ms ±0.0% 1
pdf-lib 1.32s 1.32s ±0.0% 1
@cantoo/pdf-lib 1.45s 1.45s ±0.0% 1

Copy 10 pages between documents

Benchmark Mean p99 RME Samples
libpdf 4.61ms 5.39ms ±0.9% 109
pdf-lib 11.58ms 15.89ms ±2.5% 44
@cantoo/pdf-lib 13.10ms 14.14ms ±1.4% 39

Merge 2 x 100-page PDFs

Benchmark Mean p99 RME Samples
libpdf 15.48ms 17.05ms ±1.2% 33
pdf-lib 53.24ms 61.78ms ±4.2% 10
@cantoo/pdf-lib 63.25ms 66.59ms ±2.0% 8

Fill FINTRAC form fields

Benchmark Mean p99 RME Samples
libpdf 20.96ms 27.26ms ±4.1% 24
pdf-lib 26.69ms 35.91ms ±4.5% 19
@cantoo/pdf-lib 27.84ms 36.76ms ±5.0% 18

Fill and flatten FINTRAC form

Benchmark Mean p99 RME Samples
libpdf 17.28ms 21.25ms ±3.2% 29
pdf-lib FAILED - - 0
@cantoo/pdf-lib 30.16ms 37.10ms ±4.4% 17
Copying

Copy pages between documents

Benchmark Mean p99 RME Samples
copy 1 page 1.13ms 2.10ms ±2.6% 445
copy 10 pages from 100-page PDF 4.65ms 5.75ms ±1.2% 108
copy all 100 pages 7.96ms 9.58ms ±1.2% 63

Duplicate pages within same document

Benchmark Mean p99 RME Samples
duplicate page 0 1.02ms 1.38ms ±0.8% 491
duplicate all pages (double the document) 1.02ms 1.43ms ±0.8% 493

Merge PDFs

Benchmark Mean p99 RME Samples
merge 2 small PDFs 1.57ms 2.03ms ±1.0% 320
merge 10 small PDFs 8.05ms 10.81ms ±1.4% 63
merge 2 x 100-page PDFs 14.64ms 15.23ms ±0.8% 35
Drawing

benchmarks/drawing.bench.ts

Benchmark Mean p99 RME Samples
draw 100 rectangles 633μs 1.26ms ±1.8% 790
draw 100 circles 941μs 1.82ms ±1.7% 532
draw 100 lines 573μs 1.21ms ±1.3% 874
draw 100 text lines (standard font) 1.66ms 2.48ms ±1.5% 302
create 10 pages with mixed content 1.51ms 2.74ms ±2.2% 331
Forms

benchmarks/forms.bench.ts

Benchmark Mean p99 RME Samples
get form fields 3.15ms 5.42ms ±3.0% 159
fill text fields 12.68ms 17.13ms ±4.4% 40
read field values 2.84ms 5.29ms ±2.6% 177
flatten form 7.99ms 10.34ms ±1.6% 63
Loading

benchmarks/loading.bench.ts

Benchmark Mean p99 RME Samples
load small PDF (888B) 64μs 184μs ±2.5% 7868
load medium PDF (19KB) 90μs 124μs ±0.4% 5529
load form PDF (116KB) 1.29ms 2.45ms ±1.9% 389
load heavy PDF (2.0MB) 18.11ms 18.88ms ±1.3% 28
Saving

benchmarks/saving.bench.ts

Benchmark Mean p99 RME Samples
save unmodified (19KB) 110μs 331μs ±1.1% 4560
save with modifications (19KB) 839μs 1.56ms ±1.6% 596
incremental save (19KB) 169μs 348μs ±1.0% 2954
save heavy PDF (2.0MB) 17.83ms 19.92ms ±1.4% 29
incremental save heavy PDF (2.0MB) 18.82ms 21.42ms ±1.6% 27
Splitting

Extract single page

Benchmark Mean p99 RME Samples
extractPages (1 page from small PDF) 1.12ms 2.16ms ±2.8% 447
extractPages (1 page from 100-page PDF) 3.60ms 4.15ms ±0.8% 139
extractPages (1 page from 2000-page PDF) 58.45ms 67.51ms ±4.1% 10

Split into single-page PDFs

Benchmark Mean p99 RME Samples
split 100-page PDF (0.1MB) 40.95ms 45.48ms ±2.5% 13
split 2000-page PDF (0.9MB) 726.52ms 726.52ms ±0.0% 1

Batch page extraction

Benchmark Mean p99 RME Samples
extract first 10 pages from 2000-page PDF 56.62ms 57.95ms ±1.0% 9
extract first 100 pages from 2000-page PDF 60.59ms 62.22ms ±1.6% 9
extract every 10th page from 2000-page PDF (200 pages) 66.16ms 69.02ms ±2.4% 8
Environment
  • Runner: Linux (X64)
  • Runtime: Bun 1.4.2
  • Libraries: @libpdf/core 0.4.2 (this repo), pdf-lib 1.17.1, @cantoo/pdf-lib 2.9.1

Results are machine-dependent.

A stray ")" or "}" made TokenReader loop forever on a zero-length
keyword; an operator inside an unterminated array or dict, or a
non-name dict key, threw and lost the whole page. The reader now always
advances, the tokenizer ends the array/dict and re-emits the operator,
and text extraction treats an undecodable or crashing form XObject as
absent rather than dropping the page's own text.
@Mythie
Mythie merged commit e7df20a into main Sep 25, 2026
6 checks passed

This branch was successfully deployed

1 active deployment
Preview — 8ab4928d Deployed Sep 25, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant