Skip to content

Write PDF/A-2b and PDF/A-2u documents that keep their transparency - #330

Merged
jakejackson1 merged 16 commits into
gravitypdffrom
feature/pdfa-2-conformance
Sep 23, 2026
Merged

jakejackson1 merged 16 commits into
gravitypdffrom
feature/pdfa-2-conformance

Conversation

@jakejackson1

@jakejackson1 jakejackson1 commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

Summary

PDFAversion now accepts 2-B and 2-U (plus 3-U), and the default is 2-B. Documents at every level mPDF writes pass veraPDF (1-B, 2-B, 2-U, 3-B, 3-U), in RGB and with a CMYK output intent. A new Conformance workflow checks this on every PR.

PDF/A-2 keeps what PDF/A-1 forbids. Mpdf::transparencyAllowed() replaces the PDFA || PDFX checks wherever they were about transparency. Under PDF/A-2 and PDF/A-3 the document keeps:

What PDF/A-2 and PDF/A-3 require in return:

  • Every annotation except links and popups gets an appearance stream (6.3.3).
  • Annotation files. Files are still loaded through the asset fetcher and annotationFileAllowList. On top of that, PDF/A-1 embeds none, and PDF/A-2 embeds a file only if it is a PDF/A document, judged by its uncompressed pdfaid:part claim (6.8). The claim is read from the contents the asset fetcher loaded. A file left out is reported in PDFAXwarnings unless PDFAauto is set. Under PDF/A-3 the file is written as an associated file (A file attachment on an annotation fails PDF/A-3: no MIME subtype, AFRelationship or /AF association #347): a file specification with /AFRelationship /Unspecified, named by the annotation's /AF, and a stream carrying the MIME type detected for the file.
  • Active forms (Active forms cannot be written as PDF/A: checkboxes throw on ZapfDingbats, and widgets rely on NeedAppearances #348). Every text field, choice and push button now gets its own appearance stream, in every document, so viewers that ignore /NeedAppearances still draw them. Checkboxes and radio buttons always use the vector shapes, so the formUseZapD property is gone. /NeedAppearances is written only when a field holds text the appearances can't shape (RTL or complex scripts), and never under PDF/A. /ZaDb joins /DR only alongside that flag, which stops Poppler's "Unknown font tag ZaDb" warning (Poppler warns 'Unknown font tag ZaDb' for every active checkbox and radio button #59). Under PDF/A, JavaScript, submit and reset actions are left out, and hidden and noprint widgets are printed. Without PDFAauto, each of these is reported as a warning.

PDF/A-1b now conforms too (#346):

  • CID font subsets carry a CIDSet.
  • Symbolic TrueType subsets drop their (1,0) cmap subtable.
  • SVG opacity goes through allowedAlpha().
  • Blurred box shadows are drawn unblurred rather than through a soft mask.
  • SVG gradient stops paint opaque.
  • Annotations carry no /C under a CMYK output intent.

Annotation object numbers (#352). MetadataWriter::numberAnnotations() numbers every link, annotation (with its popup or file, and its PDF/A appearance), form widget and radio group once, in the order writeAnnotations() writes them. PageWriter builds /Annots from those numbers instead of predicting offsets.

The default is now 2-B (was 1-B). A document that sets PDFA => true without a PDFAversion now gets PDF/A-2b. It keeps its transparency and allows watermarks, and its catalog declares PDF 1.7. To keep the old output, set PDFAversion => '1-B'. The config now describes when to use each version.

Version validation. PDFAversion values mPDF can't produce now throw rather than being written into the metadata: 1-A, 2-A, 3-A, 1-U.

Bugs fixed along the way:

  • With PDFAauto, an rgba() colour was converted to CMYK in an RGB document, and cmyka() was converted to RGB under PDF/X.
  • Under a CMYK output intent, a greyscale PNG with transparency now goes through convertImage() like other alpha PNGs, so it keeps its mask where transparency is allowed.
  • CMYK patch meshes (blurred box shadows) wrote 254 instead of 255 for 100% ink, through a float-to-int conversion PHP 8.1+ deprecates. One helper now rounds them, along with the patch mesh opacity bytes, which truncated the same way.
  • An annotation's file name kept a Windows path's drive and directories (D:\a\x.php became Dax.php).
  • PDF/A-1 and PDF/X refused layers only after registering them, so optional content leaked into the catalog.

Rebased on gravitypdf

The branch is rebased onto the current gravitypdf, and the two merge commits from #358 and #361 are now linear. Where the two sides met:

  • settleAnnotations() first runs gravitypdf's loadAnnotationFiles(), now private. The PDF/A embed check reads the loaded contents instead of calling file_get_contents() on the path. It does this before numberAnnotations() counts any objects.
  • Layers and hidden content raise the header version through setMinPdfVersion() (The PDF header keeps the version it was written with, however pdf_version is raised later #339).
  • The last commit adapts this PR's tests:
    • Annotation tests attach tests/data/annotation-files/sample.txt with allowHtmlAnnotationFiles, instead of a .php file the allow list rejects.
    • The newer SVG alpha, header version and cross-reference tests that mean PDF/A-1b now set PDFAversion => '1-B'.
    • The form-fields-* and page-break-avoid-forms fixtures are refreshed for selects written as combo boxes (Write a select without a size as a combo box (#396) #419).

Tests

  • PDFA2Test, PDFA1Test, PdfaOptionalContentTest, AnnotationObjectNumbersTest, FormAppearanceTest, ColorSpaceRestrictorTest, FontSubsetterCmapTest, CmykPatchMeshTest, and new version cases in PDFATest.
  • PdfaConformanceTest runs veraPDF on 1-B, 2-B, 2-U, 3-B and 3-U, each with the default sRGB output intent and with a CMYK one. It also covers attachments, layers and active forms. It is in the conformance group and uses a new VeraPdf test trait, which takes any veraPDF flavour (2b, ua1, …).
  • New workflow, conformance.yml: installs a pinned veraPDF (VERAPDF_VERSION, cached per version) and runs --group=conformance --fail-on-skipped, so a broken install fails instead of skipping. The main matrix has no veraPDF and skips these tests.
  • CMYK profile: data/iccprofiles/SWOP2006_Coated3v2.icc (2.7 MB), with a NOTICE.md for its licence. IDEAlliance allows redistribution unmodified. Nothing embeds it unless ICCProfile points at it.
  • Snapshots:
    • New: form-fields-static, form-fields-active and form-fields-pdfa.
    • Refreshed: page-break-avoid-forms and page-break-avoid-references (widget appearances), shadow (fully opaque blurred edges, RMSE 0.0002), and image-border-radius and svg-class, whose SVG form XObjects now name /Resources 2 0 R for their graphics states (6.2.2).
  • After the rebase, all of these pass locally: the full suite (3745 tests), the conformance group against veraPDF (26 tests), composer cs, PHPStan and a PHP 5.6 lint of the changed files.

Fixes #346. Fixes #347. Fixes #348. Fixes #350. Fixes #351. Fixes #352. Fixes #59.

🤖 Generated with Claude Code

@jakejackson1 jakejackson1 added documentation Improvements or additions to documentation enhancement New feature or request create-upstream-pr labels Sep 22, 2026
@jakejackson1
jakejackson1 force-pushed the feature/pdfa-2-conformance branch from 8e388e9 to c93b66a Compare September 23, 2026 01:30
@jakejackson1

Copy link
Copy Markdown
Member Author

Rebased onto gravitypdf, which had gained separate fixes for #343 (#356) and #344 (#362), AES-256 encryption (#329) and an associated-files change. The merge commits of #357, #359 and #360 are replayed as their own commits.

How the overlaps were resolved:

Gates on c93b66a:

#358 and #361 were moved onto the new head with --onto.

jakejackson1 and others added 14 commits September 23, 2026 21:12
PDFAversion now takes 2-B and 2-U (and 3-U), and refuses the parts and
levels mPDF cannot produce instead of claiming them. Under PDF/A-2 and
PDF/A-3 a document keeps what PDF/A-1 forbids: opacity, rgba()/cmyka()
colours, gradient and PNG alpha as soft masks, watermarks and page
transparency groups.

What PDF/A-2 asks in return:
- annotations carry an appearance stream
- an annotation embeds a file only if it is a PDF/A document
- every glyph maps to Unicode: the ToUnicode CMap's single
  <0000> <FFFF> range mapped nothing past <00FF>, so each row of 256
  codes now gets a range of its own (fixture refresh)
- SVG form objects name the resource dictionary their graphics states
  are in (fixture refresh)

Also fixes PDFAauto drawing an rgba() colour as CMYK in an RGB document,
and the /Annots list pointing at the wrong objects when an annotation's
file was not allowed.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The workflow installs a pinned veraPDF (cached per version) and runs the
conformance test group, failing on a skip so a broken install cannot pass
quietly. The VeraPdf test trait checks a document against any flavour
veraPDF knows, so PDF/UA and further PDF/A tests join the same group.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
PDF/A-2b is the level archives and validators most often expect, so a
document that sets PDFA without a PDFAversion now gets it rather than
PDF/A-1b, and keeps its transparency. The config describes when to pick
each supported version.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Bundles the SWOP2006_Coated3v2 CMYK profile (IDEAlliance, freely
redistributable unmodified; see data/iccprofiles/NOTICE.md) so the tests
can write a CMYK output intent. Its box shadow exposed CMYK patch meshes
turning 100% into 254 through a float to int conversion PHP 8.1
deprecates; they now round.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The patch mesh opacity bytes truncated 100% to 254 as its colour bytes
did; one helper now rounds both, and the shadow fixture's blurred edges
come out fully opaque where they should.

The conformance tests ask whether veraPDF runs once, and skip before
building documents where it does not. Their helpers, and those of the
PDF/A-2 and ToUnicode tests, build on PageStreams and pdfaConformance()
instead of repeating them.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
/Subj was left out under any PDF/A or PDF/X setting because it is a PDF 1.5
key. PDF/A-2 and PDF/A-3 are based on PDF 1.7, so it is now left out only
under PDF/X-1a and PDF/A-1.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
PDF/A-2 and PDF/A-3 allow optional content when every configuration
dictionary has a /Name, none has /AS, and /Order lists every group.
Mpdf::optionalContentAllowed() gates layers and visibility on the same
parts as transparency; the default configuration now carries a /Name,
and /AS is left out of PDF/A documents.

Print-only and screen-only content is still refused under PDF/A, since
it is switched by /AS alone. PDF/A-1 and PDF/X now refuse layers and
visibility before registering them, so no optional content leaks into
the catalog as it did before.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…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>
…, shadow blur and annotation colours (#346)

veraPDF rejected PDF/A-1b documents on five rules:

- 6.3.5-3: a CID font subset now carries a CIDSet naming each CID its
  CIDToGIDMap maps, and .notdef. Written only under PDF/A-1.
- 6.3.7-3: a font subset written as a symbolic TrueType font leaves out its
  format 6 (1,0) cmap subtable under PDF/A-1, keeping the (3,0) one.
- 6.4-5/6.4-6: SVG opacity, fill-opacity and stroke-opacity go through
  Mpdf::allowedAlpha(), as SetAlpha() does, so they paint opaque where
  transparency is not allowed.
- 6.4-1: a blurred box-shadow fades out through a soft mask, so without
  transparency it is drawn unblurred, with its edge at the blur's midpoint.
- 6.5.3-3: an annotation carries no /C under PDF/A-1 with a CMYK output
  intent; its appearance still has the colour.

PdfaConformanceTest now validates its documents at 1-B too.

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

Under PDF/A-3 the file of an <annotation file="..."> now goes through the
same writer as SetAssociatedFiles(): an indirect file specification with
/AFRelationship /Unspecified, named by the annotation's own /AF, and an
embedded file stream with a MIME /Subtype of application/octet-stream.
numberAnnotations() gives the file specification its own object number,
ahead of the stream's. PDF/A-1, PDF/A-2 and non-PDF/A output are unchanged.

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

A file annotation's name kept everything after the last forward slash, so on
Windows the drive and directories stayed in the name (D:\a\x.php became
Dax.php). Strip up to the last slash or backslash.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…to redraw only text it cannot shape (#348, #59) (#387)

* Draw every active form widget in a PDF/A document instead of asking the viewer to (#348)

Checkboxes and radio buttons switched to ZapfDingbats, which mPDF cannot
embed, so PDF/A and PDF/X threw. They now draw the shapes mPDF already has
for formUseZapD = false, in the colour space the output intent allows.

Under PDF/A, text fields, choices and push buttons get an appearance
stream laid out when the widget is placed (value, password mask, wrapped
textarea, selected option, list box highlight, caption or icon), the
AcroForm drops /NeedAppearances, hidden inputs and noprint buttons are
printed, and JavaScript, submit and reset actions are left out. Without
PDFAauto the dropped actions and noprint are reported as PDF/A warnings.

Other documents still ask the viewer to redraw widgets, and now name the
ZaDb font it redraws checkboxes with in /DR, which stops poppler warning
of an unknown font tag (#59).

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

* Give every active text field, choice and push button its own appearance

Viewers that ignore /NeedAppearances, as many mobile, print and preview
ones do, drew these widgets empty. The appearances PDF/A needed are now
written for every document, with core-font text in Windows-1252 and
embedded-font text in UTF-16. Outside PDF/A the form still asks the viewer
to redraw them, and keeps its actions and its hidden and noprint flags.

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

* Ask the viewer to redraw form fields only for text their appearances cannot shape

Every widget now carries an appearance, so /NeedAppearances is written only
when a field's value, options or caption is in a right-to-left script or a
script with a shaper of its own (Otl::hasComplexScript), which mPDF's
appearances neither reorder nor shape (#408). PDF/A never writes it. /ZaDb
joins /DR only alongside the flag, when there is a checkbox or radio button
to redraw (#59).

Without the flag, viewers draw checkboxes and radio buttons from their own
appearances, and the ZapfDingbats ones have no bounding box and no off state
to draw. Every document now uses the vector shapes PDF/A and PDF/X already
drew, and the formUseZapD property that chose between them is gone.

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

* Refresh the form snapshots for the appearances every widget now carries

Ghostscript draws a widget from its appearance where it has one. In
page-break-avoid-forms the select shows its selected option rather than its
export value, the buttons show their captions, and the checkboxes and radio
buttons are the vector shapes; in page-break-avoid-references only the text
field is drawn differently. Neither document asks the viewer to redraw its
widgets any more, so /NeedAppearances and /ZaDb are gone from both.

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

* Keep the check box and radio paths on one source line, so a Windows checkout writes no CR

The paths were string literals running over two lines. With text=auto, Git checks them
out with CRLF on Windows, and the carriage return reached the appearance stream, so the
page-break-avoid-forms snapshot differed there.

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

* Snapshot every form field, drawn statically, as active widgets and in PDF/A

The same page of fields renders three ways: into the page without active
forms (form-fields-static), as widgets with their own appearances
(form-fields-active) and as a PDF/A-2b document drawn from the appearances
alone (form-fields-pdfa). The page has text, password, read-only, disabled,
required and maxlength inputs, short and wrapped text areas, a combo box, a
list box and a multiple select, checkboxes and a radio group, submit, reset,
script and image buttons, a hidden input, a field styled in CSS, and an
Arabic value, which has the active document ask the viewer to redraw its
widgets.

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

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
… a test means it

gravitypdf now reads an annotation's file through the asset fetcher and embeds only the types
annotationFileAllowList maps, and <annotation file=""> also needs allowHtmlAnnotationFiles. The
annotation tests attached a .php file from HTML, so they now attach tests/data/annotation-files/sample.txt
with allowHtmlAnnotationFiles on. A PDF/A-3 file is written with the MIME type detected for it.

PDF/A documents now default to PDF/A-2b, so the SVG alpha, header version and cross-reference tests
that mean PDF/A-1b say so. The form field fixtures are refreshed for selects written as combo boxes and
disabled text greyed through the colour converter.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@jakejackson1
jakejackson1 force-pushed the feature/pdfa-2-conformance branch from 1bb1b2c to 5cfcc1d Compare September 23, 2026 11:19
jakejackson1 and others added 2 commits September 23, 2026 21:40
… resolves it on Windows

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