Skip to content

Commit 0087334

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. annotationObjectCount() counts the extra object. 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 b69cf1d commit 0087334

3 files changed

Lines changed: 145 additions & 71 deletions

File tree

‎src/Writer/MetadataWriter.php‎

Lines changed: 111 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -256,56 +256,9 @@ public function writeAssociatedFiles() // _putAssociatedFiles
256256
throw new \Mpdf\MpdfException('ext-zlib is required for compression of associated files');
257257
}
258258

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

311264
// AF array
@@ -320,6 +273,66 @@ public function writeAssociatedFiles() // _putAssociatedFiles
320273
$this->mpdf->associatedFilesRoot = $this->mpdf->n;
321274
}
322275

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

672-
$annot .= '/FS <</Type /Filespec /F (' . $f . ')';
673-
if ($this->mpdf->PDFA) {
674-
$annot .= ' /UF (' . $f . ')';
685+
if ($this->associatesAnnotationFiles()) {
686+
$annot .= '/FS ' . ($this->mpdf->n + 1) . ' 0 R /AF [' . ($this->mpdf->n + 1) . ' 0 R]';
687+
} else {
688+
$annot .= '/FS <</Type /Filespec /F (' . $f . ')';
689+
if ($this->mpdf->PDFA) {
690+
$annot .= ' /UF (' . $f . ')';
691+
}
692+
$annot .= '/EF <</F ' . ($this->mpdf->n + 1) . ' 0 R>>';
693+
$annot .= '>>';
675694
}
676-
$annot .= '/EF <</F ' . ($this->mpdf->n + 1) . ' 0 R>>';
677-
$annot .= '>>';
678695

679696
} else {
680697
$annot .= '/Subtype /Text';
@@ -758,14 +775,24 @@ public function writeAnnotations() // _putannots
758775
throw new \Mpdf\MpdfException('mPDF Error: Cannot access file attachment - ' . $pl['opt']['file']);
759776
}
760777

761-
$filestream = gzcompress($file);
762-
$this->writer->object();
763-
$this->writer->write('<</Type /EmbeddedFile');
764-
$this->writer->write('/Length ' . strlen($filestream));
765-
$this->writer->write('/Filter /FlateDecode');
766-
$this->writer->write('>>');
767-
$this->writer->stream($filestream);
768-
$this->writer->write('endobj');
778+
if ($this->associatesAnnotationFiles()) {
779+
$this->writeAssociatedFile([
780+
'name' => $f,
781+
'description' => '',
782+
'AFRelationship' => 'Unspecified',
783+
'mime' => 'application/octet-stream',
784+
'content' => $file,
785+
]);
786+
} else {
787+
$filestream = gzcompress($file);
788+
$this->writer->object();
789+
$this->writer->write('<</Type /EmbeddedFile');
790+
$this->writer->write('/Length ' . strlen($filestream));
791+
$this->writer->write('/Filter /FlateDecode');
792+
$this->writer->write('>>');
793+
$this->writer->stream($filestream);
794+
$this->writer->write('endobj');
795+
}
769796

770797
} elseif (!empty($pl['opt']['popup'])) {
771798
$this->writer->object();
@@ -851,13 +878,34 @@ public function settleAnnotations()
851878
/**
852879
* How many objects an annotation writes: itself, then its file or popup, then its appearance under PDF/A
853880
*
881+
* An associated file under PDF/A-3 is two objects, its specification and its stream.
882+
*
854883
* @param mixed[] $pl
855884
*
856885
* @return int
857886
*/
858887
public function annotationObjectCount(array $pl)
859888
{
860-
return 1 + (!empty($pl['opt']['popup']) || !empty($pl['opt']['file']) ? 1 : 0) + ($this->mpdf->PDFA ? 1 : 0);
889+
$file = !empty($pl['opt']['file']);
890+
891+
return 1 + (!empty($pl['opt']['popup']) || $file ? 1 : 0) + ($file && $this->associatesAnnotationFiles() ? 1 : 0) + ($this->mpdf->PDFA ? 1 : 0);
892+
}
893+
894+
/**
895+
* Whether an annotation's file is written as a PDF/A-3 associated file, with a MIME type, an AFRelationship and
896+
* the annotation's /AF naming it
897+
*
898+
* @return bool
899+
*/
900+
private function associatesAnnotationFiles()
901+
{
902+
if (!$this->mpdf->PDFA) {
903+
return false;
904+
}
905+
906+
list($part) = $this->mpdf->pdfaConformance();
907+
908+
return $part === '3';
861909
}
862910

863911
/**

‎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

@@ -240,6 +241,28 @@ public function testPdfa2AutoLeavesOutFileItCannotEmbed()
240241
$this->assertStringContainsString('/F (' . $name . ') /UF (' . $name . ')', $pdf);
241242
}
242243

244+
/**
245+
* PDF/A-3 writes an annotation's file as an associated file: a file specification with an AFRelationship, named by
246+
* the annotation's /AF, and a stream with a MIME type
247+
*/
248+
public function testPdfa3AssociatesAnnotationFile()
249+
{
250+
$pdf = $this->render('<p><annotation content="File" file="' . __FILE__ . '" /></p>', ['allowAnnotationFiles' => true] + $this->pdfaConfig('3-B'));
251+
252+
$annotation = $this->annotations($pdf)[0];
253+
$this->assertSame(1, preg_match('/\/FS (\d+) 0 R \/AF \[\1 0 R\]/', $annotation, $spec));
254+
$this->assertSame(1, preg_match('/\/AP <<\/N (\d+) 0 R>>/', $annotation, $appearance));
255+
$this->assertStringContainsString('/Subtype /Form', $this->object($pdf, $appearance[1]));
256+
257+
$filespec = $this->object($pdf, $spec[1]);
258+
$this->assertStringContainsString('/Type /Filespec', $filespec);
259+
$this->assertStringContainsString('/AFRelationship /Unspecified', $filespec);
260+
$this->assertStringContainsString('/UF (PDFA2Test.php)', $filespec);
261+
262+
$this->assertSame(1, preg_match('/\/EF <<\s*\/F (\d+) 0 R/', $filespec, $stream));
263+
$this->assertStringContainsString("/Type /EmbeddedFile\n/Subtype /application#2Foctet-stream", $this->object($pdf, $stream[1]));
264+
}
265+
243266
/**
244267
* An image at 40% opacity and a gradient that fades out
245268
*

‎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

@@ -100,13 +102,14 @@ public function documents()
100102
}
101103

102104
/**
103-
* The PDF/A-2 versions, whose attachments must themselves be PDF/A
105+
* The PDF/A versions that embed files, with how many of the three attachments each embeds: PDF/A-2 only the
106+
* PDF/A document
104107
*
105-
* @return string[][]
108+
* @return mixed[][]
106109
*/
107-
public function pdfa2Versions()
110+
public function attachmentVersions()
108111
{
109-
return [['2-B'], ['2-U']];
112+
return [['2-B', 1], ['2-U', 1], ['3-B', 3], ['3-U', 3]];
110113
}
111114

112115
/**

0 commit comments

Comments
 (0)