Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 113 additions & 66 deletions src/Writer/MetadataWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,56 +261,9 @@ public function writeAssociatedFiles() // _putAssociatedFiles
throw new \Mpdf\MpdfException('ext-zlib is required for compression of associated files');
}

// for each file, we create the spec object + the stream object
foreach ($this->mpdf->associatedFiles as $k => $file) {
// spec
$this->writer->object();
$this->mpdf->associatedFiles[$k]['_root'] = $this->mpdf->n; // we store the root ref of object for future reference (e.g. /EmbeddedFiles catalog)
$this->writer->write('<</F ' . $this->writer->string($file['name']));
if (!empty($file['description'])) {
$this->writer->write('/Desc ' . $this->writer->string($file['description']));
}
$this->writer->write('/Type /Filespec');
$this->writer->write('/EF <<');
$this->writer->write('/F ' . ($this->mpdf->n + 1) . ' 0 R');
$this->writer->write('/UF ' . ($this->mpdf->n + 1) . ' 0 R');
$this->writer->write('>>');
if (!empty($file['AFRelationship'])) {
$this->writer->write('/AFRelationship /' . $file['AFRelationship']);
}
$this->writer->write('/UF ' . $this->writer->string($file['name']));
$this->writer->write('>>');
$this->writer->write('endobj');

$fileContent = null;
if (isset($file['path'])) {
$fileContent = @file_get_contents($file['path']);
} elseif (isset($file['content'])) {
$fileContent = $file['content'];
}

if (!$fileContent) {
throw new \Mpdf\MpdfException(sprintf('Cannot access associated file - %s', isset($file['path']) ? $file['path'] : $file['name']));
}

$filestream = gzcompress($fileContent);
$this->writer->object();
$this->writer->write('<</Type /EmbeddedFile');
if (!empty($file['mime'])) {
$this->writer->write('/Subtype /' . $this->writer->escapeSlashes($file['mime']));
}
$this->writer->write('/Length ' . $this->writer->streamLength($filestream));
$this->writer->write('/Filter /FlateDecode');
if (isset($file['path'])) {
// The file's own date, not the document's
$this->writer->write('/Params <</ModDate '.$this->writer->string('D:' . PdfDate::format(filemtime($file['path']))).' >>');
} else {
$this->writer->write('/Params <</ModDate ' . $this->writer->dateString() . ' >>');
}

$this->writer->write('>>');
$this->writer->stream($filestream);
$this->writer->write('endobj');
// we store the root ref of object for future reference (e.g. /EmbeddedFiles catalog)
$this->mpdf->associatedFiles[$k]['_root'] = $this->writeAssociatedFile($file, $this->mpdf->n + 1);
}

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

/**
* Writes an associated file's specification as object $specId, then its embedded file stream as the one after
*
* @param mixed[] $file name, and optionally description, AFRelationship and mime, with the file's path or content
* @param int $specId
*
* @return int the file specification's object number
*/
private function writeAssociatedFile(array $file, $specId)
{
$this->beginObject($specId);
$this->writer->write('<</F ' . $this->writer->string($file['name']));
if (!empty($file['description'])) {
$this->writer->write('/Desc ' . $this->writer->string($file['description']));
}
$this->writer->write('/Type /Filespec');
$this->writer->write('/EF <<');
$this->writer->write('/F ' . ($specId + 1) . ' 0 R');
$this->writer->write('/UF ' . ($specId + 1) . ' 0 R');
$this->writer->write('>>');
if (!empty($file['AFRelationship'])) {
$this->writer->write('/AFRelationship /' . $file['AFRelationship']);
}
$this->writer->write('/UF ' . $this->writer->string($file['name']));
$this->writer->write('>>');
$this->writer->write('endobj');

$fileContent = null;
if (isset($file['path'])) {
$fileContent = @file_get_contents($file['path']);
} elseif (isset($file['content'])) {
$fileContent = $file['content'];
}

if (!$fileContent) {
throw new \Mpdf\MpdfException(sprintf('Cannot access associated file - %s', isset($file['path']) ? $file['path'] : $file['name']));
}

$filestream = gzcompress($fileContent);
$this->beginObject($specId + 1);
$this->writer->write('<</Type /EmbeddedFile');
if (!empty($file['mime'])) {
$this->writer->write('/Subtype /' . $this->writer->escapeSlashes($file['mime']));
}
$this->writer->write('/Length ' . $this->writer->streamLength($filestream));
$this->writer->write('/Filter /FlateDecode');
if (isset($file['path'])) {
// The file's own date, not the document's
$this->writer->write('/Params <</ModDate '.$this->writer->string('D:' . PdfDate::format(filemtime($file['path']))).' >>');
} else {
$this->writer->write('/Params <</ModDate ' . $this->writer->dateString() . ' >>');
}

$this->writer->write('>>');
$this->writer->stream($filestream);
$this->writer->write('endobj');

return $specId;
}

public function writeCatalog() //_putcatalog
{
$this->writer->write('/Type /Catalog');
Expand Down Expand Up @@ -707,15 +720,19 @@ public function writeAnnotations() // _putannots

// PushPin
$f = $pl['opt']['file'];
$f = preg_replace('/^.*\//', '', $f);
$f = preg_replace('/^.*[\/\\\\]/', '', $f); // a Windows path separates with backslashes
$f = preg_replace('/[^a-zA-Z0-9._]/', '', $f);

$annot .= '/FS <</Type /Filespec /F ' . $this->writer->string($f);
if ($this->mpdf->PDFA) {
$annot .= ' /UF ' . $this->writer->string($f);
if (isset($ids['filespec'])) {
$annot .= '/FS ' . $ids['filespec'] . ' 0 R /AF [' . $ids['filespec'] . ' 0 R]';
} else {
$annot .= '/FS <</Type /Filespec /F ' . $this->writer->string($f);
if ($this->mpdf->PDFA) {
$annot .= ' /UF ' . $this->writer->string($f);
}
$annot .= '/EF <</F ' . $ids['file'] . ' 0 R>>';
$annot .= '>>';
}
$annot .= '/EF <</F ' . $ids['file'] . ' 0 R>>';
$annot .= '>>';

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

$filestream = gzcompress($file);
$this->beginObject($ids['file']);
$this->writer->write('<</Type /EmbeddedFile');
$this->writer->write('/Length ' . $this->writer->streamLength($filestream));
$this->writer->write('/Filter /FlateDecode');
$this->writer->write('>>');
$this->writer->stream($filestream);
$this->writer->write('endobj');
if (isset($ids['filespec'])) {
$this->writeAssociatedFile([
'name' => $f,
'AFRelationship' => 'Unspecified',
'mime' => 'application/octet-stream',
'content' => $file,
], $ids['filespec']);
} else {
$filestream = gzcompress($file);
$this->beginObject($ids['file']);
$this->writer->write('<</Type /EmbeddedFile');
$this->writer->write('/Length ' . $this->writer->streamLength($filestream));
$this->writer->write('/Filter /FlateDecode');
$this->writer->write('>>');
$this->writer->stream($filestream);
$this->writer->write('endobj');
}

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

if ($pl['opt']['file']) {
if ($pl['opt']['file'] && $this->associatesAnnotationFiles()) {
$ids['filespec'] = $id++;
$ids['file'] = $id++;
} elseif ($pl['opt']['file']) {
$ids['file'] = $id++;
} elseif (!empty($pl['opt']['popup'])) {
$ids['popup'] = $annots[$n][] = $id++;
Expand Down Expand Up @@ -943,7 +973,7 @@ private function settleAnnotations()
}

/**
* Begins the object numberAnnotations() gave $id, which is the next after the objects already written
* Begins object $id, numbered by numberAnnotations() or next after the objects already written
*
* @param int $id
*/
Expand All @@ -953,6 +983,23 @@ private function beginObject($id)
$this->mpdf->n = $id;
}

/**
* Whether an annotation's file is written as a PDF/A-3 associated file, with a MIME type, an AFRelationship and
* the annotation's /AF naming it
*
* @return bool
*/
private function associatesAnnotationFiles()
{
if (!$this->mpdf->PDFA) {
return false;
}

list($part) = $this->mpdf->pdfaConformance();

return $part === '3';
}

/**
* Whether the PDF/A part lets the document embed a file: PDF/A-1 embeds none, PDF/A-2 only PDF/A documents
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Mpdf/PDFA2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ public function annotationConfigs()
'files not allowed' => [[]],
'files allowed' => [['allowAnnotationFiles' => true]],
'PDF/A-2 appearances' => [['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => '2-B', 'allowAnnotationFiles' => true]],
'PDF/A-3 associated files' => [['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => '3-B', 'allowAnnotationFiles' => true]],
];
}

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

/**
* PDF/A-3 writes an annotation's file as an associated file: a file specification with an AFRelationship, named by
* the annotation's /AF, and a stream with a MIME type
*/
public function testPdfa3AssociatesAnnotationFile()
{
$pdf = $this->render('<p><annotation content="File" file="' . __FILE__ . '" /></p>', ['allowAnnotationFiles' => true] + $this->pdfaConfig('3-B'));

$annotation = $this->annotations($pdf)[0];
$this->assertSame(1, preg_match('/\/FS (\d+) 0 R \/AF \[\1 0 R\]/', $annotation, $spec));
$this->assertSame(1, preg_match('/\/AP <<\/N (\d+) 0 R>>/', $annotation, $appearance));
$this->assertStringContainsString('/Subtype /Form', $this->object($pdf, $appearance[1]));

$filespec = $this->object($pdf, $spec[1]);
$this->assertStringContainsString('/Type /Filespec', $filespec);
$this->assertStringContainsString('/AFRelationship /Unspecified', $filespec);
$this->assertStringContainsString('/UF (PDFA2Test.php)', $filespec);

$this->assertSame(1, preg_match('/\/EF <<\s*\/F (\d+) 0 R/', $filespec, $stream));
$this->assertStringContainsString("/Type /EmbeddedFile\n/Subtype /application#2Foctet-stream", $this->object($pdf, $stream[1]));
}

/**
* An image at 40% opacity and a gradient that fades out
*
Expand Down
19 changes: 11 additions & 8 deletions tests/Mpdf/PdfaConformanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,24 @@ public function testDocumentConforms($version, $config)
}

/**
* A document that attaches a PDF/A document and a plain PDF conforms, keeping only the PDF/A one
* A document that attaches a PDF/A document, a plain PDF and a PHP file conforms: PDF/A-2 keeps only the PDF/A
* one, PDF/A-3 all three
*
* @dataProvider pdfa2Versions
* @dataProvider attachmentVersions
*/
public function testAttachmentsConform($version)
public function testAttachmentsConform($version, $embedded)
{
$attachment = $this->write($this->pdfa('2-B'));

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

$this->assertSame(1, substr_count(file_get_contents($file), '/Type /EmbeddedFile'));
$this->assertSame($embedded, substr_count(file_get_contents($file), '/Type /EmbeddedFile'));
$this->assertConforms($file, $this->flavour($mpdf));
}

Expand Down Expand Up @@ -118,13 +120,14 @@ public function documents()
}

/**
* The PDF/A-2 versions, whose attachments must themselves be PDF/A
* The PDF/A versions that embed files, with how many of the three attachments each embeds: PDF/A-2 only the
* PDF/A document
*
* @return string[][]
* @return mixed[][]
*/
public function pdfa2Versions()
public function attachmentVersions()
{
return [['2-B'], ['2-U']];
return [['2-B', 1], ['2-U', 1], ['3-B', 3], ['3-U', 3]];
}

/**
Expand Down
Loading