Skip to content

Support the UseAttachments page mode, and write /PageMode once (mirrors mpdf/mpdf#2142) - #17

Merged
jakejackson1 merged 1 commit into
gravitypdffrom
mirror/2142-use-attachments
Sep 7, 2026
Merged

Support the UseAttachments page mode, and write /PageMode once (mirrors mpdf/mpdf#2142)#17
jakejackson1 merged 1 commit into
gravitypdffrom
mirror/2142-use-attachments

Conversation

@jakejackson1

Copy link
Copy Markdown
Member

Summary

This mirrors mpdf#2142, so the fork carries the feature while the upstream PR sits open.

DisplayPreferences gains UseAttachments, which asks the reader to open its attachment pane — the counterpart to SetAssociatedFiles(), which already embeds the files but gives no way to say "show them".

writeCatalog() had three separate places writing /PageMode: the outline pane implied by having bookmarks, FullScreen from DisplayPreferences, and UseOC for the layer pane. A document with bookmarks, layers and open_layer_pane set already emitted the key three times in one dictionary, which a PDF dictionary cannot hold. Rather than add a fourth, the choice now happens once.

Try it

$mpdf = new \Mpdf\Mpdf();
$mpdf->SetAssociatedFiles([[
	'name'        => 'invoice.xml',
	'mime'        => 'text/xml',
	'description' => 'Invoice data',
	'AFRelationship' => 'Alternative',
	'path'        => __DIR__ . '/invoice.xml',
]]);
$mpdf->SetDisplayPreferences('UseAttachments');
$mpdf->WriteHTML('<p>Invoice</p>');
$mpdf->Output();

The catalog carries /PageMode /UseAttachments, and the reader opens with the attachment pane showing.

Test plan

  • composer test1054 tests, 2541 assertions, green (gravitypdf is at 1048).
  • composer cs — clean.
  • PHPStan — same 33 findings as gravitypdf, none new.
  • tests/Mpdf/PageModeTest.php — three of its six cases fail on gravitypdf: the two for the new keyword, and testLayerPaneWinsOverEverythingElse, which catches the existing duplicate-key bug.
More info

Where it differs from upstream

Upstream Here
Adds a fourth /PageMode writer next to the FullScreen one Resolves the mode once, in MetadataWriter::getPageMode()
Space-indented in a tab-indented file (composer cs rejects it)
No test Adds six

Precedence

Readers resolve a repeated dictionary key by taking the last one written, so the order below is the one documents get today, with the new keyword slotted in among the explicit requests:

  1. UseOCopen_layer_pane with layers present
  2. FullScreenDisplayPreferences
  3. UseAttachmentsDisplayPreferences
  4. UseOutlines — the only one the document never asks for; it follows from having any bookmark at all

So nothing that resolved to a given mode before resolves differently now.

PDF version

/PageMode /UseAttachments is PDF 1.6, and pdf_version defaults to 1.4. That matches how /PrintScaling (1.6) and /Duplex (1.7) are already handled here — written with a comment noting the version, no header bump — so this follows suit rather than changing when mPDF advertises a higher version.

Also touched

SetDisplayPreferences() carried a comment listing the accepted keywords that had drifted — no FullScreen, no NoPrintScaling. It now lists what the writer actually reads.

DisplayPreferences gains UseAttachments, which opens the reader's
attachment pane on a document carrying embedded files.

writeCatalog() had three independent places writing /PageMode — the
outline pane implied by bookmarks, FullScreen from DisplayPreferences,
and UseOC for the layer pane — so a document with bookmarks, layers and
the pane open already emitted the key three times in one dictionary.
A fourth mode would have made that worse, so the choice now happens in
one place. The order preserves what readers resolve today, where the
last write of a repeated key wins: UseOC, then FullScreen, then the new
UseAttachments, and finally the outline pane, which is the only one the
document never asked for explicitly.

Mirrors mpdf#2142.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@jakejackson1 jakejackson1 added bug Something isn't working fork-only create-upstream-pr and removed fork-only labels Sep 7, 2026
@jakejackson1
jakejackson1 merged commit 3ae3387 into gravitypdf Sep 7, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working create-upstream-pr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant