Skip to content

Leave screen-only and unwritable hidden content out under PDFAauto and PDFXauto (#397) - #416

Merged
jakejackson1 merged 6 commits into
feature/pdfa-2-conformancefrom
fix/397-pdfa-auto-visibility
Sep 23, 2026
Merged

jakejackson1 merged 6 commits into
feature/pdfa-2-conformancefrom
fix/397-pdfa-auto-visibility

Conversation

@jakejackson1

Copy link
Copy Markdown
Member

What changed

Some visibility values can't be written as optional content: printonly and screenonly under PDF/A-2 and PDF/A-3, and all three values under PDF/A-1b and PDF/X-1a.

  • Without PDFAauto / PDFXauto: no change. These are refused with a PDF/A warning, and Output() throws.
  • With auto:
    • printonly is drawn normally. The archive or print file stands for the printed document.
    • screenonly is left out.
    • hidden is left out where optional content isn't allowed (PDF/A-1b, PDF/X-1a). Before this change it leaked as visible text. Under PDF/A-2 and PDF/A-3 it keeps its hidden optional content group, as before.

The warning is still recorded in auto mode. That matches the existing visibility tests, and Output() only reports warnings without auto.

How content is left out

Content that is left out is still laid out, so it keeps its CSS visibility: hidden space. Where SetVisibility() would write /OC … BDC and EMC, it writes a pair of markers instead. PageWriter removes everything between each pair when it writes the page. The markers go through the same column, keep-with-table and header buffers as the marked content they replace, and AddPage() and Close() already end and restart visibility at page boundaries. So this works the same for blocks, spans and content across page breaks.

After a dropped region, resumeAfterDroppedContent() clears the pageoutput cache and writes the font, line width, colours and spacing that are in force. Without this, a setter called inside the region would leave the cache claiming a state the stream no longer has.

Nothing that is left out registers anything:

  • printobjectbuffer() skips objects that are left out, so they add no form field, annotation, bookmark, TOC or index entry, and no image link.
  • Link() and IndexEntry() (which covers the tag-time xref path) return early.
  • Page backgrounds that are left out are not generated, so no shadings or patterns are left orphaned.

Span visibility now reaches objects inside the span. Objects buffered through _saveTextBuffer() pick it up in the chunk loops. The four tags that push straight into the buffer (annotation, bookmark, indexentry, tocentry) stamp it themselves, through Tag::withSpanVisibility(). One side effect: an image inside a hidden span is now hidden in ordinary documents too, where before it showed.

I went with markers rather than skipping at the tag level the way display: none does. Skipping at the tag level would lose the layout space.

Verification

  • New tests/Mpdf/AutoVisibilityTest.php:
    • checks whether block and span text is in the page content streams, for each value under 1-B, 2-B and X-1a auto
    • checks the warning and the throw without auto
    • checks that a link, active form field, annotation, bookmark and index entry register when visible, and that none do when screen-only, for both a block and a span
    • checks that screen-only content across page breaks keeps the same page count and the same position for the text after it
    • checks that drawing resumes with the colour and line width that were set
    • fails without the fix
  • New PdfaConformanceTest::testVisibilityConforms runs a document with all three values, as blocks and spans with a link and an annotation, at 1-B, 2-B and 2-U. veraPDF passes all three.
  • Ghostscript txtwrite on 1-B, 2-B and X-1a auto output shows the print-only text and none of the screen-only or hidden text.
  • composer test (3401 tests), --group=snapshot (85 tests, no fixture changes), --group=conformance (19 tests), composer cs and PHPStan are all clean.

Fixes #397

🤖 Generated with Claude Code

@jakejackson1 jakejackson1 added the bug Something isn't working label Sep 23, 2026
@jakejackson1
jakejackson1 force-pushed the feature/pdfa-2-conformance branch from 1bb1b2c to 5cfcc1d Compare September 23, 2026 11:19
jakejackson1 and others added 4 commits September 23, 2026 21:28
…d PDFXauto (#397)

PDF/A-2 forbids the /AS entry that print-only and screen-only content depend on, and PDF/A-1b and PDF/X-1a forbid optional content altogether. Without auto these are still refused with a warning, so Output() throws. Under auto, print-only content is now drawn, because the archive stands for the printed document. Screen-only content is left out, and so is hidden content where optional content is not allowed. Under PDF/A-2 and PDF/A-3, hidden content stays in its hidden group.

Content that is left out is still laid out, so it keeps its space. It is written between markers where the optional content operators would go, and PageWriter removes it together with the markers. The font, colours and line width in force are then written again. Its backgrounds are skipped. Its objects register no link, form field, annotation, bookmark or index entry. An object inside a span now carries the span's visibility.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
… parse

PHP 5.6 cannot apply isset() to an element of a class constant array.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…am parser

An image's title annotation now takes the visibility of its span, like the
other objects a tag puts straight into the text buffer. The visibility
groups reuse OptionalContentWriter's table rather than repeat its bits, the
per-object drop check no longer re-tests the block's visibility each time,
and IndexEntry() drops a guard the object buffer already covers. The page
content parser moves into PageStreams as pageContents().

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@jakejackson1
jakejackson1 force-pushed the fix/397-pdfa-auto-visibility branch from 11ce795 to d3c33ae Compare September 23, 2026 11:29
…ument under PDFAauto

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@jakejackson1
jakejackson1 merged commit 656219b into feature/pdfa-2-conformance Sep 23, 2026
29 checks passed
jakejackson1 added a commit that referenced this pull request Sep 23, 2026
…d PDFXauto (#397) (#416)

* Leave screen-only and unwritable hidden content out under PDFAauto and PDFXauto (#397)

PDF/A-2 forbids the /AS entry that print-only and screen-only content depend on, and PDF/A-1b and PDF/X-1a forbid optional content altogether. Without auto these are still refused with a warning, so Output() throws. Under auto, print-only content is now drawn, because the archive stands for the printed document. Screen-only content is left out, and so is hidden content where optional content is not allowed. Under PDF/A-2 and PDF/A-3, hidden content stays in its hidden group.

Content that is left out is still laid out, so it keeps its space. It is written between markers where the optional content operators would go, and PageWriter removes it together with the markers. The font, colours and line width in force are then written again. Its backgrounds are skipped. Its objects register no link, form field, annotation, bookmark or index entry. An object inside a span now carries the span's visibility.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* Look up a visibility group with array_key_exists(), which PHP 5.6 can parse

PHP 5.6 cannot apply isset() to an element of a class constant array.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* Leave out an image title in left-out content, and share the page stream parser

An image's title annotation now takes the visibility of its span, like the
other objects a tag puts straight into the text buffer. The visibility
groups reuse OptionalContentWriter's table rather than repeat its bits, the
per-object drop check no longer re-tests the block's visibility each time,
and IndexEntry() drops a guard the object buffer already covers. The page
content parser moves into PageStreams as pageContents().

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* Say what the left-out content comments mean in fewer words

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* Snapshot print-only, screen-only and hidden content in a PDF/A-1b document under PDFAauto

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@jakejackson1
jakejackson1 deleted the fix/397-pdfa-auto-visibility branch September 30, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant