Skip to content

Avoid unused object ids when saving PDF - #10091

Merged
radarhere merged 2 commits into
python-pillow:mainfrom
Rodrigo-Palma:fix/pdf-save-all-transparency-object-ids
Oct 2, 2026
Merged

radarhere merged 2 commits into
python-pillow:mainfrom
Rodrigo-Palma:fix/pdf-save-all-transparency-object-ids

Conversation

@Rodrigo-Palma

Copy link
Copy Markdown
Contributor

Changes proposed in this pull request:

  • When saving a PDF, allocate each image object as it is written, instead of reserving them up front from the mode of the first frame.
  • Add a test.

_save reserves an extra object ID for an SMask whenever im.mode == "P" and "transparency" in im.info, but it checks that once per image, against the current frame, and then reserves the same number for every frame. For a GIF with transparency, the first frame is P with transparency and the later frames are RGBA, so save_all=True reserves objects that are never written. They stay in the xref table with offset 0.

with Image.open("Tests/images/transparent_dispose.gif") as im:
    im.save("out.pdf", save_all=True)

On main, objects 9 and 10 have offset 0. PdfParser.read_indirect() raises "indirect object definition different than expected" for both, and qpdf --check warns "object has offset 0". With this change, every object reads back and qpdf --check is clean.

The extra ID was added in a70ea82 ("Write P transparency as SMask"). Letting write_obj(None, ...) allocate the ID means the count can no longer disagree with what is written.

Comment thread Tests/test_file_pdf.py
@radarhere
radarhere force-pushed the fix/pdf-save-all-transparency-object-ids branch from cbe1619 to 98036ad Compare October 2, 2026 03:46
@radarhere

Copy link
Copy Markdown
Member

Thanks, nice catch. I've pushed a commit to add a comment and rename the test.

@radarhere radarhere changed the title Allocate PDF image objects when they are written Avoid unused object ids when saving PDF Oct 2, 2026
@radarhere
radarhere enabled auto-merge (squash) October 2, 2026 03:48
@radarhere
radarhere merged commit 9712fff into python-pillow:main Oct 2, 2026
50 of 51 checks passed
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.

2 participants