Skip to content

Give each annotation its object numbers once, instead of predicting them in PageWriter - #360

Merged
jakejackson1 merged 1 commit into
feature/pdfa-2-conformancefrom
refactor/352-explicit-annotation-ids
Sep 23, 2026
Merged

jakejackson1 merged 1 commit into
feature/pdfa-2-conformancefrom
refactor/352-explicit-annotation-ids

Conversation

@jakejackson1

Copy link
Copy Markdown
Member

Stacks on #330.

Mechanism

MetadataWriter::numberAnnotations($id) replaces settleAnnotations() + annotationObjectCount() and the two counting loops in PageWriter::writePages(). It settles each annotation's file first, then walks the pages once, in the order writeAnnotations() writes:

  • each link gets a number (kept in a private $linkIds[$page][$key], so PageLinks entries keep their shape)
  • each annotation gets $pl['ids'] = ['annot' => …, 'file' => … | 'popup' => …, 'appearance' => …] (appearance only under PDF/A), plus pageobj
  • form widgets are numbered by Form::addFormIds(), which now appends to an array rather than a string
  • radio groups get their obj_id after the last page

It returns each page's /Annots list: links, annotations, popups that have no file, and widgets. PageWriter just writes that list.

writeAnnotations() starts each object with beginObject($id), which calls BaseWriter::object($id) and sets $mpdf->n to that number so the form code that follows can keep numbering in sequence. /EF, /Popup, /Parent and /AP now use $ids[…] instead of n + 1, n - 1 and n + annotationObjectCount() - 1. The layout rule is now in one place. Other changes: Form::countPageForms() is gone, and PageWriter no longer takes Form.

Output is byte-identical

  • composer test -- --group=snapshot: 85/85 pass, no "bytes differ" or snapshot:update notes. None in the full run either.
  • I also rendered 7 deterministic documents before and after the change and compared them with cmp. All were identical. They covered two pages of links, popups (default size and placed), a file annotation, a coloured note, and active forms with text/JS, checkbox, select and two radio groups, under no files / files allowed / PDF/A-1b / PDF/A-2b / PDF/A-3b. The encrypted variants are random on every run, so I left them out of the comparison.

Tests

The new tests/Mpdf/AnnotationObjectNumbersTest.php builds two pages with links, notes with and without popups, a file note, and (without PDF/A) active widgets with a radio group on each page. It runs under four setups: file not allowed + forms, file allowed + forms, PDF/A-2b (file dropped) and PDF/A-3b (file embedded). It checks that:

  • every number below /Size is written
  • each page's /Annots lists exactly the expected subtypes in order, each a /Type /Annot
  • /P names the page
  • /Popup names a listed Popup whose /Parent points back
  • /EF names an EmbeddedFile, and /AP names a Form XObject
  • each radio widget's /Parent lists it in /Kids

I mutated the code four ways (a wrong /Parent, /EF, /AP or radio group number). The test caught every one. PDFA2Test::testAnnotsListOnlyAnnotations passes unchanged.

Gates

  • composer test: 3226 tests OK. The 5 skips are the usual unpacked-Unicode/HarfBuzz/ICU generator checks.
  • composer cs: clean
  • PHPStan (level 2, baseline): no errors
  • PHP 5.6 php -l on the changed files: OK
  • phpunit --group=conformance --fail-on-skipped (veraPDF): 10/10 OK

Fixes #352

🤖 Generated with Claude Code

…hem in PageWriter

MetadataWriter::numberAnnotations() numbers every link, annotation (with its
popup or embedded file and PDF/A appearance), form widget and radio group in
the order writeAnnotations() writes them. writeAnnotations() then writes each
object under its number and refers to the others by number, not by offset
from the one it is writing. PageWriter writes /Annots from the numbers it is
handed. The output is byte-identical.

Fixes #352

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant