Skip to content

Commit f156ea9

Browse files
jakejackson1claude
andcommitted
Write a PDF/A-3 annotation's file as an associated file, so it passes 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>
1 parent c93b66a commit f156ea9

3 files changed

Lines changed: 146 additions & 73 deletions

File tree

‎src/Writer/MetadataWriter.php‎

Lines changed: 112 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -261,56 +261,9 @@ public function writeAssociatedFiles() // _putAssociatedFiles
261261
throw new \Mpdf\MpdfException('ext-zlib is required for compression of associated files');
262262
}
263263

264-
// for each file, we create the spec object + the stream object
265264
foreach ($this->mpdf->associatedFiles as $k => $file) {
266-
// spec
267-
$this->writer->object();
268-
$this->mpdf->associatedFiles[$k]['_root'] = $this->mpdf->n; // we store the root ref of object for future reference (e.g. /EmbeddedFiles catalog)
269-
$this->writer->write('<</F ' . $this->writer->string($file['name']));
270-
if (!empty($file['description'])) {
271-
$this->writer->write('/Desc ' . $this->writer->string($file['description']));
272-
}
273-
$this->writer->write('/Type /Filespec');
274-
$this->writer->write('/EF <<');
275-
$this->writer->write('/F ' . ($this->mpdf->n + 1) . ' 0 R');
276-
$this->writer->write('/UF ' . ($this->mpdf->n + 1) . ' 0 R');
277-
$this->writer->write('>>');
278-
if (!empty($file['AFRelationship'])) {
279-
$this->writer->write('/AFRelationship /' . $file['AFRelationship']);
280-
}
281-
$this->writer->write('/UF ' . $this->writer->string($file['name']));
282-
$this->writer->write('>>');
283-
$this->writer->write('endobj');
284-
285-
$fileContent = null;
286-
if (isset($file['path'])) {
287-
$fileContent = @file_get_contents($file['path']);
288-
} elseif (isset($file['content'])) {
289-
$fileContent = $file['content'];
290-
}
291-
292-
if (!$fileContent) {
293-
throw new \Mpdf\MpdfException(sprintf('Cannot access associated file - %s', isset($file['path']) ? $file['path'] : $file['name']));
294-
}
295-
296-
$filestream = gzcompress($fileContent);
297-
$this->writer->object();
298-
$this->writer->write('<</Type /EmbeddedFile');
299-
if (!empty($file['mime'])) {
300-
$this->writer->write('/Subtype /' . $this->writer->escapeSlashes($file['mime']));
301-
}
302-
$this->writer->write('/Length ' . $this->writer->streamLength($filestream));
303-
$this->writer->write('/Filter /FlateDecode');
304-
if (isset($file['path'])) {
305-
// The file's own date, not the document's
306-
$this->writer->write('/Params <</ModDate '.$this->writer->string('D:' . PdfDate::format(filemtime($file['path']))).' >>');
307-
} else {
308-
$this->writer->write('/Params <</ModDate ' . $this->writer->dateString() . ' >>');
309-
}
310-
311-
$this->writer->write('>>');
312-
$this->writer->stream($filestream);
313-
$this->writer->write('endobj');
265+
// we store the root ref of object for future reference (e.g. /EmbeddedFiles catalog)
266+
$this->mpdf->associatedFiles[$k]['_root'] = $this->writeAssociatedFile($file, $this->mpdf->n + 1);
314267
}
315268

316269
// AF array
@@ -325,6 +278,66 @@ public function writeAssociatedFiles() // _putAssociatedFiles
325278
$this->mpdf->associatedFilesRoot = $this->mpdf->n;
326279
}
327280

281+
/**
282+
* Writes an associated file's specification as object $specId, then its embedded file stream as the one after
283+
*
284+
* @param mixed[] $file name, and optionally description, AFRelationship and mime, with the file's path or content
285+
* @param int $specId
286+
*
287+
* @return int the file specification's object number
288+
*/
289+
private function writeAssociatedFile(array $file, $specId)
290+
{
291+
$this->beginObject($specId);
292+
$this->writer->write('<</F ' . $this->writer->string($file['name']));
293+
if (!empty($file['description'])) {
294+
$this->writer->write('/Desc ' . $this->writer->string($file['description']));
295+
}
296+
$this->writer->write('/Type /Filespec');
297+
$this->writer->write('/EF <<');
298+
$this->writer->write('/F ' . ($specId + 1) . ' 0 R');
299+
$this->writer->write('/UF ' . ($specId + 1) . ' 0 R');
300+
$this->writer->write('>>');
301+
if (!empty($file['AFRelationship'])) {
302+
$this->writer->write('/AFRelationship /' . $file['AFRelationship']);
303+
}
304+
$this->writer->write('/UF ' . $this->writer->string($file['name']));
305+
$this->writer->write('>>');
306+
$this->writer->write('endobj');
307+
308+
$fileContent = null;
309+
if (isset($file['path'])) {
310+
$fileContent = @file_get_contents($file['path']);
311+
} elseif (isset($file['content'])) {
312+
$fileContent = $file['content'];
313+
}
314+
315+
if (!$fileContent) {
316+
throw new \Mpdf\MpdfException(sprintf('Cannot access associated file - %s', isset($file['path']) ? $file['path'] : $file['name']));
317+
}
318+
319+
$filestream = gzcompress($fileContent);
320+
$this->beginObject($specId + 1);
321+
$this->writer->write('<</Type /EmbeddedFile');
322+
if (!empty($file['mime'])) {
323+
$this->writer->write('/Subtype /' . $this->writer->escapeSlashes($file['mime']));
324+
}
325+
$this->writer->write('/Length ' . $this->writer->streamLength($filestream));
326+
$this->writer->write('/Filter /FlateDecode');
327+
if (isset($file['path'])) {
328+
// The file's own date, not the document's
329+
$this->writer->write('/Params <</ModDate '.$this->writer->string('D:' . PdfDate::format(filemtime($file['path']))).' >>');
330+
} else {
331+
$this->writer->write('/Params <</ModDate ' . $this->writer->dateString() . ' >>');
332+
}
333+
334+
$this->writer->write('>>');
335+
$this->writer->stream($filestream);
336+
$this->writer->write('endobj');
337+
338+
return $specId;
339+
}
340+
328341
public function writeCatalog() //_putcatalog
329342
{
330343
$this->writer->write('/Type /Catalog');
@@ -710,12 +723,16 @@ public function writeAnnotations() // _putannots
710723
$f = preg_replace('/^.*\//', '', $f);
711724
$f = preg_replace('/[^a-zA-Z0-9._]/', '', $f);
712725

713-
$annot .= '/FS <</Type /Filespec /F ' . $this->writer->string($f);
714-
if ($this->mpdf->PDFA) {
715-
$annot .= ' /UF ' . $this->writer->string($f);
726+
if (isset($ids['filespec'])) {
727+
$annot .= '/FS ' . $ids['filespec'] . ' 0 R /AF [' . $ids['filespec'] . ' 0 R]';
728+
} else {
729+
$annot .= '/FS <</Type /Filespec /F ' . $this->writer->string($f);
730+
if ($this->mpdf->PDFA) {
731+
$annot .= ' /UF ' . $this->writer->string($f);
732+
}
733+
$annot .= '/EF <</F ' . $ids['file'] . ' 0 R>>';
734+
$annot .= '>>';
716735
}
717-
$annot .= '/EF <</F ' . $ids['file'] . ' 0 R>>';
718-
$annot .= '>>';
719736

720737
} else {
721738
$annot .= '/Subtype /Text';
@@ -799,14 +816,23 @@ public function writeAnnotations() // _putannots
799816
throw new \Mpdf\MpdfException('mPDF Error: Cannot access file attachment - ' . $pl['opt']['file']);
800817
}
801818

802-
$filestream = gzcompress($file);
803-
$this->beginObject($ids['file']);
804-
$this->writer->write('<</Type /EmbeddedFile');
805-
$this->writer->write('/Length ' . $this->writer->streamLength($filestream));
806-
$this->writer->write('/Filter /FlateDecode');
807-
$this->writer->write('>>');
808-
$this->writer->stream($filestream);
809-
$this->writer->write('endobj');
819+
if (isset($ids['filespec'])) {
820+
$this->writeAssociatedFile([
821+
'name' => $f,
822+
'AFRelationship' => 'Unspecified',
823+
'mime' => 'application/octet-stream',
824+
'content' => $file,
825+
], $ids['filespec']);
826+
} else {
827+
$filestream = gzcompress($file);
828+
$this->beginObject($ids['file']);
829+
$this->writer->write('<</Type /EmbeddedFile');
830+
$this->writer->write('/Length ' . $this->writer->streamLength($filestream));
831+
$this->writer->write('/Filter /FlateDecode');
832+
$this->writer->write('>>');
833+
$this->writer->stream($filestream);
834+
$this->writer->write('endobj');
835+
}
810836

811837
} elseif ($this->writesPopup($pl)) {
812838
$this->beginObject($ids['popup']);
@@ -867,7 +893,8 @@ public function writeAnnotations() // _putannots
867893
* Numbers the objects writeAnnotations() writes, in the order it writes them, starting at $id
868894
*
869895
* Each page gets its links, then its annotations, then its form widgets. An annotation's ids name the annotation
870-
* itself, its embedded file or else its popup, and its appearance under PDF/A. The radio groups come last.
896+
* itself, its embedded file (under PDF/A-3 its file specification, then the file) or else its popup, and its
897+
* appearance under PDF/A. The radio groups come last.
871898
*
872899
* @param int $id the object number after the last page's
873900
*
@@ -893,7 +920,10 @@ public function numberAnnotations($id)
893920
foreach ($this->mpdf->PageAnnots[$n] as $key => $pl) {
894921
$ids = ['annot' => $annots[$n][] = $id++];
895922

896-
if ($pl['opt']['file']) {
923+
if ($pl['opt']['file'] && $this->associatesAnnotationFiles()) {
924+
$ids['filespec'] = $id++;
925+
$ids['file'] = $id++;
926+
} elseif ($pl['opt']['file']) {
897927
$ids['file'] = $id++;
898928
} elseif (!empty($pl['opt']['popup'])) {
899929
$ids['popup'] = $annots[$n][] = $id++;
@@ -943,7 +973,7 @@ private function settleAnnotations()
943973
}
944974

945975
/**
946-
* Begins the object numberAnnotations() gave $id, which is the next after the objects already written
976+
* Begins object $id, numbered by numberAnnotations() or next after the objects already written
947977
*
948978
* @param int $id
949979
*/
@@ -953,6 +983,23 @@ private function beginObject($id)
953983
$this->mpdf->n = $id;
954984
}
955985

986+
/**
987+
* Whether an annotation's file is written as a PDF/A-3 associated file, with a MIME type, an AFRelationship and
988+
* the annotation's /AF naming it
989+
*
990+
* @return bool
991+
*/
992+
private function associatesAnnotationFiles()
993+
{
994+
if (!$this->mpdf->PDFA) {
995+
return false;
996+
}
997+
998+
list($part) = $this->mpdf->pdfaConformance();
999+
1000+
return $part === '3';
1001+
}
1002+
9561003
/**
9571004
* Whether the PDF/A part lets the document embed a file: PDF/A-1 embeds none, PDF/A-2 only PDF/A documents
9581005
*

‎tests/Mpdf/PDFA2Test.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ public function annotationConfigs()
198198
'files not allowed' => [[]],
199199
'files allowed' => [['allowAnnotationFiles' => true]],
200200
'PDF/A-2 appearances' => [['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => '2-B', 'allowAnnotationFiles' => true]],
201+
'PDF/A-3 associated files' => [['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => '3-B', 'allowAnnotationFiles' => true]],
201202
];
202203
}
203204

@@ -270,6 +271,28 @@ public function testPdfa2AutoLeavesOutFileItCannotEmbed()
270271
$this->assertStringContainsString('/F (' . $name . ') /UF (' . $name . ')', $pdf);
271272
}
272273

274+
/**
275+
* PDF/A-3 writes an annotation's file as an associated file: a file specification with an AFRelationship, named by
276+
* the annotation's /AF, and a stream with a MIME type
277+
*/
278+
public function testPdfa3AssociatesAnnotationFile()
279+
{
280+
$pdf = $this->render('<p><annotation content="File" file="' . __FILE__ . '" /></p>', ['allowAnnotationFiles' => true] + $this->pdfaConfig('3-B'));
281+
282+
$annotation = $this->annotations($pdf)[0];
283+
$this->assertSame(1, preg_match('/\/FS (\d+) 0 R \/AF \[\1 0 R\]/', $annotation, $spec));
284+
$this->assertSame(1, preg_match('/\/AP <<\/N (\d+) 0 R>>/', $annotation, $appearance));
285+
$this->assertStringContainsString('/Subtype /Form', $this->object($pdf, $appearance[1]));
286+
287+
$filespec = $this->object($pdf, $spec[1]);
288+
$this->assertStringContainsString('/Type /Filespec', $filespec);
289+
$this->assertStringContainsString('/AFRelationship /Unspecified', $filespec);
290+
$this->assertStringContainsString('/UF (PDFA2Test.php)', $filespec);
291+
292+
$this->assertSame(1, preg_match('/\/EF <<\s*\/F (\d+) 0 R/', $filespec, $stream));
293+
$this->assertStringContainsString("/Type /EmbeddedFile\n/Subtype /application#2Foctet-stream", $this->object($pdf, $stream[1]));
294+
}
295+
273296
/**
274297
* An image at 40% opacity and a gradient that fades out
275298
*

‎tests/Mpdf/PdfaConformanceTest.php‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,24 @@ public function testDocumentConforms($version, $config)
6262
}
6363

6464
/**
65-
* A document that attaches a PDF/A document and a plain PDF conforms, keeping only the PDF/A one
65+
* A document that attaches a PDF/A document, a plain PDF and a PHP file conforms: PDF/A-2 keeps only the PDF/A
66+
* one, PDF/A-3 all three
6667
*
67-
* @dataProvider pdfa2Versions
68+
* @dataProvider attachmentVersions
6869
*/
69-
public function testAttachmentsConform($version)
70+
public function testAttachmentsConform($version, $embedded)
7071
{
7172
$attachment = $this->write($this->pdfa('2-B'));
7273

7374
$mpdf = $this->pdfa($version, ['allowAnnotationFiles' => true]);
7475
$mpdf->WriteHTML(
7576
'<p>PDF/A <annotation content="PDF/A" file="' . $attachment . '" /></p>'
7677
. '<p>Plain <annotation content="Plain" file="' . __DIR__ . '/../data/pdfs/2-Page-PDF_1_4.pdf" /></p>'
78+
. '<p>PHP <annotation content="PHP" file="' . __FILE__ . '" /></p>'
7779
);
7880
$file = $this->write($mpdf);
7981

80-
$this->assertSame(1, substr_count(file_get_contents($file), '/Type /EmbeddedFile'));
82+
$this->assertSame($embedded, substr_count(file_get_contents($file), '/Type /EmbeddedFile'));
8183
$this->assertConforms($file, $this->flavour($mpdf));
8284
}
8385

@@ -118,13 +120,14 @@ public function documents()
118120
}
119121

120122
/**
121-
* The PDF/A-2 versions, whose attachments must themselves be PDF/A
123+
* The PDF/A versions that embed files, with how many of the three attachments each embeds: PDF/A-2 only the
124+
* PDF/A document
122125
*
123-
* @return string[][]
126+
* @return mixed[][]
124127
*/
125-
public function pdfa2Versions()
128+
public function attachmentVersions()
126129
{
127-
return [['2-B'], ['2-U']];
130+
return [['2-B', 1], ['2-U', 1], ['3-B', 3], ['3-U', 3]];
128131
}
129132

130133
/**

0 commit comments

Comments
 (0)