@@ -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 /**
0 commit comments