Skip to content

fix: handle raw-deflate FlateDecode and drop unusable raw images (#615)#625

Closed
kh3rld wants to merge 2 commits into
mainfrom
fix/issue-615-pdf-image-raw-fallback
Closed

fix: handle raw-deflate FlateDecode and drop unusable raw images (#615)#625
kh3rld wants to merge 2 commits into
mainfrom
fix/issue-615-pdf-image-raw-fallback

Conversation

@kh3rld

@kh3rld kh3rld commented Mar 31, 2026

Copy link
Copy Markdown
Member

Summary

Two separate problems caused FlateDecode images to surface as format=\"raw\" with still-compressed bytes that callers cannot use:

1. ZlibDecoder-only — raw-deflate PDFs always fail

decode_flate_to_png only tried flate2::read::ZlibDecoder. Many PDF producers write FlateDecode streams as raw deflate (no zlib header/footer), which ZlibDecoder rejects. These fell straight through to the "raw" fallback regardless of whether pdfium was available. Fix: try DeflateDecoder when ZlibDecoder fails.

2. Unusable raw images returned silently after all fallbacks

When pdfium is not installed (or get_processed_image fails), images still carrying format="raw" with data.len() < width * height * channels were passed to callers — bytes that are definitively the original compressed stream, not pixel data. Fix: filter those images out of the result and surface a ProcessingWarning with the count so callers know image extraction was partial.

Changes

  • pdf/images.rs: fallback from ZlibDecoder to DeflateDecoder in decode_flate_to_png; regression test test_decode_flate_raw_deflate_fallback
  • extractors/pdf/mod.rs: after reextract_raw_images_via_pdfium, drop images where format == "raw" and data.len() < w*h*channels; consolidate fallback warning to cover both pdfium re-extractions and dropped images

Test plan

  • cargo test -p kreuzberg --features pdf -- pdf::images::tests — 14 tests pass including new regression test
  • Confirm PDFs with raw-deflate FlateDecode images now produce valid PNGs
  • Confirm that when pdfium is unavailable, unusable images are absent from the result and a ProcessingWarning is present

Fixes #615

kh3rld added 2 commits March 31, 2026 17:37
…xtractor

Second occurrence of the class of issue fixed in #519. Two sites in
docx/parser.rs matched attr.value.as_ref() without an explicit &[u8] cast,
causing the compiler to lock the type to &[u8; 5] from the first arm and
reject the other arms ("separate" = 8 bytes, "end" = 3 bytes).
Two sites in fictionbook.rs called normalized.as_ref() on a Cow<str> without
an explicit &str annotation, triggering E0282 on newer toolchains.

Fixes #622
Two problems caused FlateDecode images to be returned as format='raw' with
still-compressed bytes:

1. decode_flate_to_png only tried ZlibDecoder. PDFs that use raw deflate
   (no zlib header/footer) silently fell through to the raw fallback.
   Fix: try DeflateDecoder when ZlibDecoder fails.

2. When pdfium bitmap fallback also fails (e.g. pdfium not installed),
   images that are still 'raw' with data smaller than width*height*channels
   were returned to callers — unusable data with no indication of failure.
   Fix: filter those images out and surface a ProcessingWarning listing
   the count so callers know extraction was partial.

Adds regression test: test_decode_flate_raw_deflate_fallback.

Fixes #615
@kh3rld
kh3rld requested a review from Goldziher March 31, 2026 21:56
@kh3rld kh3rld moved this from Todo to In Review in Kreuzberg.dev Kanban Mar 31, 2026
@Goldziher Goldziher closed this Apr 1, 2026
@github-project-automation github-project-automation Bot moved this from In Review to Done in Kreuzberg.dev Kanban Apr 1, 2026
@Goldziher
Goldziher deleted the fix/issue-615-pdf-image-raw-fallback branch April 8, 2026 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

bug: PDF images with failed FlateDecode→PNG conversion returned as format="raw" with unusable/unknown data

2 participants