@@ -10,6 +10,16 @@ final class OptionalContentWriter
1010
1111 use Strict;
1212
13+ /**
14+ * The groups the visibility property draws in, by resource name: the bit of Mpdf::$hasOC that marks each one used,
15+ * the property holding its object number, its name, and its print and view states
16+ */
17+ const VISIBILITY_GROUPS = [
18+ 'OC1 ' => [1 , 'n_ocg_print ' , 'Print only ' , 'ON ' , 'OFF ' ],
19+ 'OC2 ' => [2 , 'n_ocg_view ' , 'Screen only ' , 'OFF ' , 'ON ' ],
20+ 'OC3 ' => [4 , 'n_ocg_hidden ' , 'Hidden ' , 'OFF ' , 'OFF ' ],
21+ ];
22+
1323 /**
1424 * @var \Mpdf\Mpdf
1525 */
@@ -28,24 +38,19 @@ public function __construct(Mpdf $mpdf, BaseWriter $writer)
2838
2939 public function writeOptionalContentGroups () // _putocg Optional Content Groups
3040 {
31- if ($ this ->mpdf ->hasOC ) {
41+ foreach (self ::VISIBILITY_GROUPS as $ group ) {
42+ list ($ bit , $ property , $ name , $ print , $ view ) = $ group ;
3243
33- $ this ->writer ->object ();
34- $ this ->mpdf ->n_ocg_print = $ this ->mpdf ->n ;
35- $ this ->writer ->write ('<</Type /OCG /Name ' . $ this ->writer ->string ('Print only ' ));
36- $ this ->writer ->write ('/Usage <</Print <</PrintState /ON>> /View <</ViewState /OFF>>>>>> ' );
37- $ this ->writer ->write ('endobj ' );
38-
39- $ this ->writer ->object ();
40- $ this ->mpdf ->n_ocg_view = $ this ->mpdf ->n ;
41- $ this ->writer ->write ('<</Type /OCG /Name ' . $ this ->writer ->string ('Screen only ' ));
42- $ this ->writer ->write ('/Usage <</Print <</PrintState /OFF>> /View <</ViewState /ON>>>>>> ' );
43- $ this ->writer ->write ('endobj ' );
44+ // Only the groups the document uses are written, as the catalog lists only those
45+ $ this ->mpdf ->$ property = null ;
46+ if (!($ this ->mpdf ->hasOC & $ bit )) {
47+ continue ;
48+ }
4449
4550 $ this ->writer ->object ();
46- $ this ->mpdf ->n_ocg_hidden = $ this ->mpdf ->n ;
47- $ this ->writer ->write ('<</Type /OCG /Name ' . $ this ->writer ->string (' Hidden ' ));
48- $ this ->writer ->write ('/Usage <</Print <</PrintState /OFF >> /View <</ViewState /OFF >>>>>> ' );
51+ $ this ->mpdf ->$ property = $ this ->mpdf ->n ;
52+ $ this ->writer ->write ('<</Type /OCG /Name ' . $ this ->writer ->string ($ name ));
53+ $ this ->writer ->write ('/Usage <</Print <</PrintState / ' . $ print . ' >> /View <</ViewState / ' . $ view . ' >>>>>> ' );
4954 $ this ->writer ->write ('endobj ' );
5055 }
5156
@@ -68,4 +73,21 @@ public function writeOptionalContentGroups() // _putocg Optional Content Groups
6873 }
6974 }
7075
76+ /**
77+ * The page resource entries naming each visibility group written
78+ *
79+ * @return string
80+ */
81+ public function visibilityProperties ()
82+ {
83+ $ properties = '' ;
84+ foreach (self ::VISIBILITY_GROUPS as $ resource => $ group ) {
85+ if ($ this ->mpdf ->{$ group [1 ]}) {
86+ $ properties .= '/ ' . $ resource . ' ' . $ this ->mpdf ->{$ group [1 ]} . ' 0 R ' ;
87+ }
88+ }
89+
90+ return $ properties ;
91+ }
92+
7193}
0 commit comments