github razvandimescu/gopdf v0.9.4
v0.9.4 — recover PDFs with leading bytes before the %PDF header

latest releases: v0.11.2, v0.11.1, v0.11.0...
4 months ago

Fix

PDFs that carry bytes before the %PDF- header now parse instead of failing at xref resolution. Some producers and transports prepend an envelope — per-file hashes, content-type/filename framing, HTTP scaffolding — which shifts every byte offset stored in the file, so seeking to a recorded xref or object offset landed mid-garbage.

Matching Adobe and pdf.js, the reader treats the %PDF- marker as the origin: Open scans the first 1024 bytes for the marker and trims the leading bytes once, so r.data always begins at %PDF- and every stored offset is correct against byte 0. Well-formed files (header at byte 0) are untouched.

Simpler than offsetting each seek

Rather than carry a headerOffset field and add it at every seek site — an invariant each future call site would have to remember — the leading bytes are sliced away a single time in Open. The offset arithmetic disappears from readXRefAt, readXRefStream, and parseObjectAt.

Verification

  • New tests: TestLeadingBytesBeforeHeader (a realistic envelope: 6-byte hash + magic + length-prefixed content-type/filename before the real PDF), TestHeaderOffsetCleanFile (clean files stay at zero displacement).
  • Full pdf suite passes under -race.

Full Changelog: v0.9.3...v0.9.4

Don't miss a new gopdf release

NewReleases is sending notifications on new releases.