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