1<?php 2 3namespace Mpdf\Writer; 4 5use Mpdf\Strict; 6use Mpdf\Mpdf; 7use Psr\Log\LoggerInterface; 8 9final class ResourceWriter implements \Psr\Log\LoggerAwareInterface 10{ 11 12 use Strict; 13 14 /** 15 * @var \Mpdf\Mpdf 16 */ 17 private $mpdf; 18 19 /** 20 * @var \Mpdf\Writer\BaseWriter 21 */ 22 private $writer; 23 24 /** 25 * @var \Mpdf\Writer\ColorWriter 26 */ 27 private $colorWriter; 28 29 /** 30 * @var \Mpdf\Writer\FontWriter 31 */ 32 private $fontWriter; 33 34 /** 35 * @var \Mpdf\Writer\ImageWriter 36 */ 37 private $imageWriter; 38 39 /** 40 * @var \Mpdf\Writer\FormWriter 41 */ 42 private $formWriter; 43 44 /** 45 * @var \Mpdf\Writer\OptionalContentWriter 46 */ 47 private $optionalContentWriter; 48 49 /** 50 * @var \Mpdf\Writer\BackgroundWriter 51 */ 52 private $backgroundWriter; 53 54 /** 55 * @var \Mpdf\Writer\BookmarkWriter 56 */ 57 private $bookmarkWriter; 58 59 /** 60 * @var \Mpdf\Writer\MetadataWriter 61 */ 62 private $metadataWriter; 63 64 /** 65 * @var \Mpdf\Writer\JavaScriptWriter 66 */ 67 private $javaScriptWriter; 68 69 /** 70 * @var \Psr\Log\LoggerInterface 71 */ 72 private $logger; 73 74 public function __construct( 75 Mpdf $mpdf, 76 BaseWriter $writer, 77 ColorWriter $colorWriter, 78 FontWriter $fontWriter, 79 ImageWriter $imageWriter, 80 FormWriter $formWriter, 81 OptionalContentWriter $optionalContentWriter, 82 BackgroundWriter $backgroundWriter, 83 BookmarkWriter $bookmarkWriter, 84 MetadataWriter $metadataWriter, 85 JavaScriptWriter $javaScriptWriter, 86 LoggerInterface $logger 87 ) { 88 $this->mpdf = $mpdf; 89 $this->writer = $writer; 90 $this->colorWriter = $colorWriter; 91 $this->fontWriter = $fontWriter; 92 $this->imageWriter = $imageWriter; 93 $this->formWriter = $formWriter; 94 $this->optionalContentWriter = $optionalContentWriter; 95 $this->backgroundWriter = $backgroundWriter; 96 $this->bookmarkWriter = $bookmarkWriter; 97 $this->metadataWriter = $metadataWriter; 98 $this->javaScriptWriter = $javaScriptWriter; 99 $this->logger = $logger; 100 } 101 102 public function writeResources() // _putresources 103 { 104 if ($this->mpdf->hasOC || count($this->mpdf->layers)) { 105 $this->optionalContentWriter->writeOptionalContentGroups(); 106 } 107 108 $this->mpdf->_putextgstates(); 109 $this->colorWriter->writeSpotColors(); 110 111 // @log Compiling Fonts 112 113 $this->fontWriter->writeFonts(); 114 115 // @log Compiling Images 116 117 $this->imageWriter->writeImages(); 118 119 $this->formWriter->writeFormObjects(); 120 121 $this->mpdf->writeImportedPagesAndResolvedObjects(); 122 123 $this->backgroundWriter->writeShaders(); 124 $this->backgroundWriter->writePatterns(); 125 126 // Resource dictionary 127 $this->mpdf->offsets[2] = strlen($this->mpdf->buffer); 128 $this->writer->write('2 0 obj'); 129 $this->writer->write('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]'); 130 131 $this->writer->write('/Font <<'); 132 foreach ($this->mpdf->fonts as $font) { 133 if (isset($font['type']) && $font['type'] === 'TTF' && !$font['used']) { 134 continue; 135 } 136 if (isset($font['type']) && $font['type'] === 'TTF' && ($font['sip'] || $font['smp'])) { 137 foreach ($font['n'] as $k => $fid) { 138 $this->writer->write('/F' . $font['subsetfontids'][$k] . ' ' . $font['n'][$k] . ' 0 R'); 139 } 140 } else { 141 $this->writer->write('/F' . $font['i'] . ' ' . $font['n'] . ' 0 R'); 142 } 143 } 144 $this->writer->write('>>'); 145 146 if (count($this->mpdf->spotColors)) { 147 $this->writer->write('/ColorSpace <<'); 148 foreach ($this->mpdf->spotColors as $color) { 149 $this->writer->write('/CS' . $color['i'] . ' ' . $color['n'] . ' 0 R'); 150 } 151 $this->writer->write('>>'); 152 } 153 154 if (count($this->mpdf->extgstates)) { 155 $this->writer->write('/ExtGState <<'); 156 foreach ($this->mpdf->extgstates as $k => $extgstate) { 157 if (isset($extgstate['trans'])) { 158 $this->writer->write('/' . $extgstate['trans'] . ' ' . $extgstate['n'] . ' 0 R'); 159 } else { 160 $this->writer->write('/GS' . $k . ' ' . $extgstate['n'] . ' 0 R'); 161 } 162 } 163 $this->writer->write('>>'); 164 } 165 166 /* -- BACKGROUNDS -- */ 167 if (($this->mpdf->gradients !== null && (count($this->mpdf->gradients) > 0))) { // mPDF 5.7.3 168 169 $this->writer->write('/Shading <<'); 170 171 foreach ($this->mpdf->gradients as $id => $grad) { 172 $this->writer->write('/Sh' . $id . ' ' . $grad['id'] . ' 0 R'); 173 } 174 175 $this->writer->write('>>'); 176 177 /* 178 // ??? Not needed !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 179 $this->writer->write('/Pattern <<'); 180 foreach ($this->mpdf->gradients as $id => $grad) { 181 $this->writer->write('/P'.$id.' '.$grad['pattern'].' 0 R'); 182 } 183 $this->writer->write('>>'); 184 */ 185 } 186 /* -- END BACKGROUNDS -- */ 187 188 if (count($this->mpdf->images) || count($this->mpdf->formobjects) || count($this->mpdf->getImportedPages())) { 189 $this->writer->write('/XObject <<'); 190 foreach ($this->mpdf->images as $image) { 191 $this->writer->write('/I' . $image['i'] . ' ' . $image['n'] . ' 0 R'); 192 } 193 foreach ($this->mpdf->formobjects as $formobject) { 194 $this->writer->write('/FO' . $formobject['i'] . ' ' . $formobject['n'] . ' 0 R'); 195 } 196 /* -- IMPORTS -- */ 197 foreach ($this->mpdf->getImportedPages() as $pageData) { 198 $this->writer->write('/' . $pageData['id'] . ' ' . $pageData['objectNumber'] . ' 0 R'); 199 } 200 /* -- END IMPORTS -- */ 201 $this->writer->write('>>'); 202 } 203 204 /* -- BACKGROUNDS -- */ 205 206 if (count($this->mpdf->patterns)) { 207 $this->writer->write('/Pattern <<'); 208 foreach ($this->mpdf->patterns as $k => $patterns) { 209 $this->writer->write('/P' . $k . ' ' . $patterns['n'] . ' 0 R'); 210 } 211 $this->writer->write('>>'); 212 } 213 /* -- END BACKGROUNDS -- */ 214 215 if ($this->mpdf->hasOC || count($this->mpdf->layers)) { 216 $this->writer->write('/Properties <<'); 217 if ($this->mpdf->hasOC) { 218 $this->writer->write('/OC1 ' . $this->mpdf->n_ocg_print . ' 0 R /OC2 ' . $this->mpdf->n_ocg_view . ' 0 R /OC3 ' . $this->mpdf->n_ocg_hidden . ' 0 R '); 219 } 220 if (count($this->mpdf->layers)) { 221 foreach ($this->mpdf->layers as $id => $layer) { 222 $this->writer->write('/ZI' . $id . ' ' . $layer['n'] . ' 0 R'); 223 } 224 } 225 $this->writer->write('>>'); 226 } 227 228 $this->writer->write('>>'); 229 $this->writer->write('endobj'); // end resource dictionary 230 231 $this->bookmarkWriter->writeBookmarks(); 232 233 if (!empty($this->mpdf->js)) { 234 $this->javaScriptWriter->writeJavascript(); 235 } 236 237 if ($this->mpdf->encrypted) { 238 $this->writer->object(); 239 $this->mpdf->enc_obj_id = $this->mpdf->n; 240 $this->writer->write('<<'); 241 $this->metadataWriter->writeEncryption(); 242 $this->writer->write('>>'); 243 $this->writer->write('endobj'); 244 } 245 } 246 247 /** 248 * @param \Psr\Log\LoggerInterface $logger 249 * 250 * @return void 251 */ 252 public function setLogger(LoggerInterface $logger) 253 { 254 $this->logger = $logger; 255 } 256} 257