1<?php
2
3namespace Mpdf\Writer;
4
5use Mpdf\Strict;
6use Mpdf\Form;
7use Mpdf\Mpdf;
8use Mpdf\Pdf\Protection;
9use Mpdf\Utils\PdfDate;
10
11use Psr\Log\LoggerInterface;
12
13class MetadataWriter implements \Psr\Log\LoggerAwareInterface
14{
15
16	use Strict;
17
18	/**
19	 * @var \Mpdf\Mpdf
20	 */
21	private $mpdf;
22
23	/**
24	 * @var \Mpdf\Writer\BaseWriter
25	 */
26	private $writer;
27
28	/**
29	 * @var \Mpdf\Form
30	 */
31	private $form;
32
33	/**
34	 * @var \Mpdf\Pdf\Protection
35	 */
36	private $protection;
37
38	/**
39	 * @var \Psr\Log\LoggerInterface
40	 */
41	private $logger;
42
43	public function __construct(Mpdf $mpdf, BaseWriter $writer, Form $form, Protection $protection, LoggerInterface $logger)
44	{
45		$this->mpdf = $mpdf;
46		$this->writer = $writer;
47		$this->form = $form;
48		$this->protection = $protection;
49		$this->logger = $logger;
50	}
51
52	public function writeMetadata() // _putmetadata
53	{
54		$this->writer->object();
55		$this->mpdf->MetadataRoot = $this->mpdf->n;
56		$Producer = 'mPDF' . ($this->mpdf->exposeVersion ? (' ' . Mpdf::VERSION) : '');
57		$z = date('O'); // +0200
58		$offset = substr($z, 0, 3) . ':' . substr($z, 3, 2);
59		$CreationDate = date('Y-m-d\TH:i:s') . $offset; // 2006-03-10T10:47:26-05:00 2006-06-19T09:05:17Z
60		$uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', random_int(0, 0xffff), random_int(0, 0xffff), random_int(0, 0xffff), random_int(0, 0x0fff) | 0x4000, random_int(0, 0x3fff) | 0x8000, random_int(0, 0xffff), random_int(0, 0xffff), random_int(0, 0xffff));
61
62
63		$m = '<?xpacket begin="' . chr(239) . chr(187) . chr(191) . '" id="W5M0MpCehiHzreSzNTczkc9d"?>' . "\n"; // begin = FEFF BOM
64		$m .= ' <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="3.1-701">' . "\n";
65		$m .= '  <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">' . "\n";
66		$m .= '   <rdf:Description rdf:about="uuid:' . $uuid . '" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">' . "\n";
67		$m .= '    <pdf:Producer>' . $Producer . '</pdf:Producer>' . "\n";
68		if (!empty($this->mpdf->keywords)) {
69			$m .= '    <pdf:Keywords>' . $this->mpdf->keywords . '</pdf:Keywords>' . "\n";
70		}
71		$m .= '   </rdf:Description>' . "\n";
72
73		$m .= '   <rdf:Description rdf:about="uuid:' . $uuid . '" xmlns:xmp="http://ns.adobe.com/xap/1.0/">' . "\n";
74		$m .= '    <xmp:CreateDate>' . $CreationDate . '</xmp:CreateDate>' . "\n";
75		$m .= '    <xmp:ModifyDate>' . $CreationDate . '</xmp:ModifyDate>' . "\n";
76		$m .= '    <xmp:MetadataDate>' . $CreationDate . '</xmp:MetadataDate>' . "\n";
77		if (!empty($this->mpdf->creator)) {
78			$m .= '    <xmp:CreatorTool>' . $this->mpdf->creator . '</xmp:CreatorTool>' . "\n";
79		}
80		$m .= '   </rdf:Description>' . "\n";
81
82		// DC elements
83		$m .= '   <rdf:Description rdf:about="uuid:' . $uuid . '" xmlns:dc="http://purl.org/dc/elements/1.1/">' . "\n";
84		$m .= '    <dc:format>application/pdf</dc:format>' . "\n";
85		if (!empty($this->mpdf->title)) {
86			$m .= '    <dc:title>
87	 <rdf:Alt>
88	  <rdf:li xml:lang="x-default">' . $this->mpdf->title . '</rdf:li>
89	 </rdf:Alt>
90	</dc:title>' . "\n";
91		}
92		if (!empty($this->mpdf->keywords)) {
93			$m .= '    <dc:subject>
94	 <rdf:Bag>
95	  <rdf:li>' . $this->mpdf->keywords . '</rdf:li>
96	 </rdf:Bag>
97	</dc:subject>' . "\n";
98		}
99		if (!empty($this->mpdf->subject)) {
100			$m .= '    <dc:description>
101	 <rdf:Alt>
102	  <rdf:li xml:lang="x-default">' . $this->mpdf->subject . '</rdf:li>
103	 </rdf:Alt>
104	</dc:description>' . "\n";
105		}
106		if (!empty($this->mpdf->author)) {
107			$m .= '    <dc:creator>
108	 <rdf:Seq>
109	  <rdf:li>' . $this->mpdf->author . '</rdf:li>
110	 </rdf:Seq>
111	</dc:creator>' . "\n";
112		}
113		$m .= '   </rdf:Description>' . "\n";
114
115		if (!empty($this->mpdf->additionalXmpRdf)) {
116			$m .= $this->mpdf->additionalXmpRdf;
117		}
118
119		// This bit is specific to PDFX-1a
120		if ($this->mpdf->PDFX) {
121			$m .= '   <rdf:Description rdf:about="uuid:' . $uuid . '" xmlns:pdfx="http://ns.adobe.com/pdfx/1.3/" pdfx:Apag_PDFX_Checkup="1.3" pdfx:GTS_PDFXConformance="PDF/X-1a:2003" pdfx:GTS_PDFXVersion="PDF/X-1:2003"/>' . "\n";
122		} // This bit is specific to PDFA-1b
123		elseif ($this->mpdf->PDFA) {
124
125			if (strpos($this->mpdf->PDFAversion, '-') === false) {
126				throw new \Mpdf\MpdfException(sprintf('PDFA version (%s) is not valid. (Use: 1-B, 3-B, etc.)', $this->mpdf->PDFAversion));
127			}
128
129			list($part, $conformance) = explode('-', strtoupper($this->mpdf->PDFAversion));
130			$m .= '   <rdf:Description rdf:about="uuid:' . $uuid . '" xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/" >' . "\n";
131			$m .= '    <pdfaid:part>' . $part . '</pdfaid:part>' . "\n";
132			$m .= '    <pdfaid:conformance>' . $conformance . '</pdfaid:conformance>' . "\n";
133			if ($part === '1' && $conformance === 'B') {
134				$m .= '    <pdfaid:amd>2005</pdfaid:amd>' . "\n";
135			}
136			$m .= '   </rdf:Description>' . "\n";
137		}
138
139		$m .= '   <rdf:Description rdf:about="uuid:' . $uuid . '" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">' . "\n";
140		$m .= '    <xmpMM:DocumentID>uuid:' . $uuid . '</xmpMM:DocumentID>' . "\n";
141		$m .= '   </rdf:Description>' . "\n";
142		$m .= '  </rdf:RDF>' . "\n";
143		$m .= ' </x:xmpmeta>' . "\n";
144		$m .= str_repeat(str_repeat(' ', 100) . "\n", 20); // 2-4kB whitespace padding required
145		$m .= '<?xpacket end="w"?>'; // "r" read only
146		$this->writer->write('<</Type/Metadata/Subtype/XML/Length ' . strlen($m) . '>>');
147		$this->writer->stream($m);
148		$this->writer->write('endobj');
149	}
150
151	public function writeInfo() // _putinfo
152	{
153		$this->writer->write('/Producer ' . $this->writer->utf16BigEndianTextString('mPDF' . ($this->mpdf->exposeVersion ? (' ' . $this->getVersionString()) : '')));
154
155		if (!empty($this->mpdf->title)) {
156			$this->writer->write('/Title ' . $this->writer->utf16BigEndianTextString($this->mpdf->title));
157		}
158
159		if (!empty($this->mpdf->subject)) {
160			$this->writer->write('/Subject ' . $this->writer->utf16BigEndianTextString($this->mpdf->subject));
161		}
162
163		if (!empty($this->mpdf->author)) {
164			$this->writer->write('/Author ' . $this->writer->utf16BigEndianTextString($this->mpdf->author));
165		}
166
167		if (!empty($this->mpdf->keywords)) {
168			$this->writer->write('/Keywords ' . $this->writer->utf16BigEndianTextString($this->mpdf->keywords));
169		}
170
171		if (!empty($this->mpdf->creator)) {
172			$this->writer->write('/Creator ' . $this->writer->utf16BigEndianTextString($this->mpdf->creator));
173		}
174
175		foreach ($this->mpdf->customProperties as $key => $value) {
176			$this->writer->write('/' . $key . ' ' . $this->writer->utf16BigEndianTextString($value));
177		}
178
179		$now = PdfDate::format(time());
180		$this->writer->write('/CreationDate ' . $this->writer->string('D:' . $now));
181		$this->writer->write('/ModDate ' . $this->writer->string('D:' . $now));
182		if ($this->mpdf->PDFX) {
183			$this->writer->write('/Trapped/False');
184			$this->writer->write('/GTS_PDFXVersion(PDF/X-1a:2003)');
185		}
186	}
187
188	public function writeOutputIntent() // _putoutputintent
189	{
190		$this->writer->object();
191		$this->mpdf->OutputIntentRoot = $this->mpdf->n;
192		$this->writer->write('<</Type /OutputIntent');
193
194		$ICCProfile = str_replace('_', ' ', basename($this->mpdf->ICCProfile, '.icc'));
195
196		if ($this->mpdf->PDFA) {
197			$this->writer->write('/S /GTS_PDFA1');
198			if ($this->mpdf->ICCProfile) {
199				$this->writer->write('/Info (' . $ICCProfile . ')');
200				$this->writer->write('/OutputConditionIdentifier (Custom)');
201				$this->writer->write('/OutputCondition ()');
202			} else {
203				$this->writer->write('/Info (sRGB IEC61966-2.1)');
204				$this->writer->write('/OutputConditionIdentifier (sRGB IEC61966-2.1)');
205				$this->writer->write('/OutputCondition ()');
206			}
207			$this->writer->write('/DestOutputProfile ' . ($this->mpdf->n + 1) . ' 0 R');
208		} elseif ($this->mpdf->PDFX) { // always a CMYK profile
209			$this->writer->write('/S /GTS_PDFX');
210			if ($this->mpdf->ICCProfile) {
211				$this->writer->write('/Info (' . $ICCProfile . ')');
212				$this->writer->write('/OutputConditionIdentifier (Custom)');
213				$this->writer->write('/OutputCondition ()');
214				$this->writer->write('/DestOutputProfile ' . ($this->mpdf->n + 1) . ' 0 R');
215			} else {
216				$this->writer->write('/Info (CGATS TR 001)');
217				$this->writer->write('/OutputConditionIdentifier (CGATS TR 001)');
218				$this->writer->write('/OutputCondition (CGATS TR 001 (SWOP))');
219				$this->writer->write('/RegistryName (http://www.color.org)');
220			}
221		}
222		$this->writer->write('>>');
223		$this->writer->write('endobj');
224
225		if ($this->mpdf->PDFX && !$this->mpdf->ICCProfile) {
226			return;
227		}
228
229		$this->writer->object();
230
231		if ($this->mpdf->ICCProfile) {
232			if (!file_exists($this->mpdf->ICCProfile)) {
233				throw new \Mpdf\MpdfException(sprintf('Unable to find ICC profile "%s"', $this->mpdf->ICCProfile));
234			}
235			$s = file_get_contents($this->mpdf->ICCProfile);
236		} else {
237			$s = file_get_contents(__DIR__ . '/../../data/iccprofiles/sRGB_IEC61966-2-1.icc');
238		}
239
240		if ($this->mpdf->compress) {
241			$s = gzcompress($s);
242		}
243
244		$this->writer->write('<<');
245
246		if ($this->mpdf->PDFX || ($this->mpdf->PDFA && $this->mpdf->restrictColorSpace === 3)) {
247			$this->writer->write('/N 4');
248		} else {
249			$this->writer->write('/N 3');
250		}
251
252		if ($this->mpdf->compress) {
253			$this->writer->write('/Filter /FlateDecode ');
254		}
255
256		$this->writer->write('/Length ' . strlen($s) . '>>');
257		$this->writer->stream($s);
258		$this->writer->write('endobj');
259	}
260
261	public function writeAssociatedFiles() // _putAssociatedFiles
262	{
263		if (!function_exists('gzcompress')) {
264			throw new \Mpdf\MpdfException('ext-zlib is required for compression of associated files');
265		}
266
267		// for each file, we create the spec object + the stream object
268		foreach ($this->mpdf->associatedFiles as $k => $file) {
269			// spec
270			$this->writer->object();
271			$this->mpdf->associatedFiles[$k]['_root'] = $this->mpdf->n; // we store the root ref of object for future reference (e.g. /EmbeddedFiles catalog)
272			$this->writer->write('<</F ' . $this->writer->string($file['name']));
273			if ($file['description']) {
274				$this->writer->write('/Desc ' . $this->writer->string($file['description']));
275			}
276			$this->writer->write('/Type /Filespec');
277			$this->writer->write('/EF <<');
278			$this->writer->write('/F ' . ($this->mpdf->n + 1) . ' 0 R');
279			$this->writer->write('/UF ' . ($this->mpdf->n + 1) . ' 0 R');
280			$this->writer->write('>>');
281			if ($file['AFRelationship']) {
282				$this->writer->write('/AFRelationship /' . $file['AFRelationship']);
283			}
284			$this->writer->write('/UF ' . $this->writer->string($file['name']));
285			$this->writer->write('>>');
286			$this->writer->write('endobj');
287
288			$fileContent = null;
289			if (isset($file['path'])) {
290				$fileContent = @file_get_contents($file['path']);
291			} elseif (isset($file['content'])) {
292				$fileContent = $file['content'];
293			}
294
295			if (!$fileContent) {
296				throw new \Mpdf\MpdfException(sprintf('Cannot access associated file - %s', $file['path']));
297			}
298
299			$filestream = gzcompress($fileContent);
300			$this->writer->object();
301			$this->writer->write('<</Type /EmbeddedFile');
302			if ($file['mime']) {
303				$this->writer->write('/Subtype /' . $this->writer->escapeSlashes($file['mime']));
304			}
305			$this->writer->write('/Length '.strlen($filestream));
306			$this->writer->write('/Filter /FlateDecode');
307			if (isset($file['path'])) {
308				$this->writer->write('/Params <</ModDate '.$this->writer->string('D:' . PdfDate::format(filemtime($file['path']))).' >>');
309			} else {
310				$this->writer->write('/Params <</ModDate '.$this->writer->string('D:' . PdfDate::format(time())).' >>');
311			}
312
313			$this->writer->write('>>');
314			$this->writer->stream($filestream);
315			$this->writer->write('endobj');
316		}
317
318		// AF array
319		$this->writer->object();
320		$refs = [];
321		foreach ($this->mpdf->associatedFiles as $file) {
322			$refs[] = '' . $file['_root'] . ' 0 R';
323		}
324		$this->writer->write('[' . implode(' ', $refs) . ']');
325		$this->writer->write('endobj');
326
327		$this->mpdf->associatedFilesRoot = $this->mpdf->n;
328	}
329
330	public function writeCatalog() //_putcatalog
331	{
332		$this->writer->write('/Type /Catalog');
333		$this->writer->write('/Pages 1 0 R');
334
335		if ($this->mpdf->ZoomMode === 'fullpage') {
336			$this->writer->write('/OpenAction [3 0 R /Fit]');
337		} elseif ($this->mpdf->ZoomMode === 'fullwidth') {
338			$this->writer->write('/OpenAction [3 0 R /FitH null]');
339		} elseif ($this->mpdf->ZoomMode === 'real') {
340			$this->writer->write('/OpenAction [3 0 R /XYZ null null 1]');
341		} elseif (!is_string($this->mpdf->ZoomMode)) {
342			$this->writer->write('/OpenAction [3 0 R /XYZ null null ' . ($this->mpdf->ZoomMode / 100) . ']');
343		} elseif ($this->mpdf->ZoomMode === 'none') {
344			// do not write any zoom mode / OpenAction
345		} else {
346			$this->writer->write('/OpenAction [3 0 R /XYZ null null null]');
347		}
348
349		if ($this->mpdf->LayoutMode === 'single') {
350			$this->writer->write('/PageLayout /SinglePage');
351		} elseif ($this->mpdf->LayoutMode === 'continuous') {
352			$this->writer->write('/PageLayout /OneColumn');
353		} elseif ($this->mpdf->LayoutMode === 'twoleft') {
354			$this->writer->write('/PageLayout /TwoColumnLeft');
355		} elseif ($this->mpdf->LayoutMode === 'tworight') {
356			$this->writer->write('/PageLayout /TwoColumnRight');
357		} elseif ($this->mpdf->LayoutMode === 'two') {
358			if ($this->mpdf->mirrorMargins) {
359				$this->writer->write('/PageLayout /TwoColumnRight');
360			} else {
361				$this->writer->write('/PageLayout /TwoColumnLeft');
362			}
363		}
364
365		// Bookmarks
366		if (count($this->mpdf->BMoutlines) > 0) {
367			$this->writer->write('/Outlines ' . $this->mpdf->OutlineRoot . ' 0 R');
368			$this->writer->write('/PageMode /UseOutlines');
369		}
370
371		// Fullscreen
372		if (is_int(strpos($this->mpdf->DisplayPreferences, 'FullScreen'))) {
373			$this->writer->write('/PageMode /FullScreen');
374		}
375
376		// Metadata
377		if ($this->mpdf->PDFA || $this->mpdf->PDFX) {
378			$this->writer->write('/Metadata ' . $this->mpdf->MetadataRoot . ' 0 R');
379		}
380
381		// OutputIntents
382		if ($this->mpdf->PDFA || $this->mpdf->PDFX || $this->mpdf->ICCProfile) {
383			$this->writer->write('/OutputIntents [' . $this->mpdf->OutputIntentRoot . ' 0 R]');
384		}
385
386		// Associated files
387		if ($this->mpdf->associatedFilesRoot) {
388			$this->writer->write('/AF '. $this->mpdf->associatedFilesRoot .' 0 R');
389
390			$names = [];
391			foreach ($this->mpdf->associatedFiles as $file) {
392				$names[] = $this->writer->string($file['name']) . ' ' . $file['_root'] . ' 0 R';
393			}
394			$this->writer->write('/Names << /EmbeddedFiles << /Names [' . implode(' ', $names) .  '] >> >>');
395		}
396
397		// Forms
398		if (count($this->form->forms) > 0) {
399			$this->form->_putFormsCatalog();
400		}
401
402		if ($this->mpdf->js !== null) {
403			$this->writer->write('/Names << /JavaScript ' . $this->mpdf->n_js . ' 0 R >> ');
404		}
405
406		if ($this->mpdf->DisplayPreferences || $this->mpdf->directionality === 'rtl' || $this->mpdf->mirrorMargins) {
407
408			$this->writer->write('/ViewerPreferences<<');
409
410			if (is_int(strpos($this->mpdf->DisplayPreferences, 'HideMenubar'))) {
411				$this->writer->write('/HideMenubar true');
412			}
413
414			if (is_int(strpos($this->mpdf->DisplayPreferences, 'HideToolbar'))) {
415				$this->writer->write('/HideToolbar true');
416			}
417
418			if (is_int(strpos($this->mpdf->DisplayPreferences, 'HideWindowUI'))) {
419				$this->writer->write('/HideWindowUI true');
420			}
421
422			if (is_int(strpos($this->mpdf->DisplayPreferences, 'DisplayDocTitle'))) {
423				$this->writer->write('/DisplayDocTitle true');
424			}
425
426			if (is_int(strpos($this->mpdf->DisplayPreferences, 'CenterWindow'))) {
427				$this->writer->write('/CenterWindow true');
428			}
429
430			if (is_int(strpos($this->mpdf->DisplayPreferences, 'FitWindow'))) {
431				$this->writer->write('/FitWindow true');
432			}
433
434			// PrintScaling is PDF 1.6 spec.
435			if (!$this->mpdf->PDFA && !$this->mpdf->PDFX && is_int(strpos($this->mpdf->DisplayPreferences, 'NoPrintScaling'))) {
436				$this->writer->write('/PrintScaling /None');
437			}
438
439			if ($this->mpdf->directionality === 'rtl') {
440				$this->writer->write('/Direction /R2L');
441			}
442
443			// Duplex is PDF 1.7 spec.
444			if ($this->mpdf->mirrorMargins && !$this->mpdf->PDFA && !$this->mpdf->PDFX) {
445				// if ($this->mpdf->DefOrientation=='P') $this->writer->write('/Duplex /DuplexFlipShortEdge');
446				$this->writer->write('/Duplex /DuplexFlipLongEdge'); // PDF v1.7+
447			}
448
449			$this->writer->write('>>');
450		}
451
452		if ($this->mpdf->open_layer_pane && ($this->mpdf->hasOC || count($this->mpdf->layers))) {
453			$this->writer->write('/PageMode /UseOC');
454		}
455
456		if ($this->mpdf->hasOC || count($this->mpdf->layers)) {
457
458			$p = $v = $h = $l = $loff = $lall = $as = '';
459
460			if ($this->mpdf->hasOC) {
461
462				if (($this->mpdf->hasOC & 1) === 1) {
463					$p = $this->mpdf->n_ocg_print . ' 0 R';
464				}
465
466				if (($this->mpdf->hasOC & 2) === 2) {
467					$v = $this->mpdf->n_ocg_view . ' 0 R';
468				}
469
470				if (($this->mpdf->hasOC & 4) === 4) {
471					$h = $this->mpdf->n_ocg_hidden . ' 0 R';
472				}
473
474				$as = "<</Event /Print /OCGs [$p $v $h] /Category [/Print]>> <</Event /View /OCGs [$p $v $h] /Category [/View]>>";
475			}
476
477			if (count($this->mpdf->layers)) {
478				foreach ($this->mpdf->layers as $k => $layer) {
479					if (isset($this->mpdf->layerDetails[$k]) && strtolower($this->mpdf->layerDetails[$k]['state']) === 'hidden') {
480						$loff .= $layer['n'] . ' 0 R ';
481					} else {
482						$l .= $layer['n'] . ' 0 R ';
483					}
484					$lall .= $layer['n'] . ' 0 R ';
485				}
486			}
487
488			$this->writer->write("/OCProperties <</OCGs [$p $v $h $lall] /D <</ON [$p $l] /OFF [$v $h $loff] ");
489			$this->writer->write("/Order [$v $p $h $lall] ");
490
491			if ($as) {
492				$this->writer->write("/AS [$as] ");
493			}
494
495			$this->writer->write('>>>>');
496		}
497	}
498
499	/**
500	 * @since 5.7.2
501	 */
502	public function writeAnnotations() // _putannots
503	{
504		$nb = $this->mpdf->page;
505
506		for ($n = 1; $n <= $nb; $n++) {
507
508			if (isset($this->mpdf->PageLinks[$n]) || isset($this->mpdf->PageAnnots[$n]) || count($this->form->forms) > 0) {
509
510				$wPt = $this->mpdf->pageDim[$n]['w'] * Mpdf::SCALE;
511				$hPt = $this->mpdf->pageDim[$n]['h'] * Mpdf::SCALE;
512
513				// Links
514				if (isset($this->mpdf->PageLinks[$n])) {
515
516					foreach ($this->mpdf->PageLinks[$n] as $key => $pl) {
517
518						$this->writer->object();
519						$annot = '';
520
521						$rect = sprintf('%.3F %.3F %.3F %.3F', $pl[0], $pl[1], $pl[0] + $pl[2], $pl[1] - $pl[3]);
522
523						$annot .= '<</Type /Annot /Subtype /Link /Rect [' . $rect . ']';
524						// Removed as causing undesired effects in Chrome PDF viewer https://github.com/mpdf/mpdf/issues/283
525						// $annot .= ' /Contents ' . $this->writer->utf16BigEndianTextString($pl[4]);
526						$annot .= ' /NM ' . $this->writer->string(sprintf('%04u-%04u', $n, $key));
527						$annot .= ' /M ' . $this->writer->string('D:' . date('YmdHis'));
528
529						$annot .= ' /Border [0 0 0]';
530
531						// Use this (instead of /Border) to specify border around link
532
533						// $annot .= ' /BS <</W 1';	// Width on points; 0 = no line
534						// $annot .= ' /S /D';		// style - [S]olid, [D]ashed, [B]eveled, [I]nset, [U]nderline
535						// $annot .= ' /D [3 2]';		// Dash array - if dashed
536						// $annot .= ' >>';
537						// $annot .= ' /C [1 0 0]';	// Color RGB
538
539						if ($this->mpdf->PDFA || $this->mpdf->PDFX) {
540							$annot .= ' /F 28';
541						}
542
543						if (strpos($pl[4], '@') === 0) {
544
545							$p = substr($pl[4], 1);
546							// $h=isset($this->mpdf->OrientationChanges[$p]) ? $wPt : $hPt;
547							$htarg = $this->mpdf->pageDim[$p]['h'] * Mpdf::SCALE;
548							$annot .= sprintf(' /Dest [%d 0 R /XYZ 0 %.3F null]>>', 1 + 2 * $p, $htarg);
549
550						} elseif (is_string($pl[4])) {
551
552							$annot .= ' /A <</S /URI /URI ' . $this->writer->string($pl[4]) . '>> >>';
553
554						} else {
555
556							$l = $this->mpdf->links[$pl[4]];
557							// may not be set if #link points to non-existent target
558							if (isset($this->mpdf->pageDim[$l[0]]['h'])) {
559								$htarg = $this->mpdf->pageDim[$l[0]]['h'] * Mpdf::SCALE;
560							} else {
561								$htarg = $this->mpdf->h * Mpdf::SCALE;
562							} // doesn't really matter
563
564							$annot .= sprintf(' /Dest [%d 0 R /XYZ 0 %.3F null]>>', 1 + 2 * $l[0], $htarg - $l[1] * Mpdf::SCALE);
565						}
566
567						$this->writer->write($annot);
568						$this->writer->write('endobj');
569
570					}
571				}
572
573				/* -- ANNOTATIONS -- */
574				if (isset($this->mpdf->PageAnnots[$n])) {
575
576					foreach ($this->mpdf->PageAnnots[$n] as $key => $pl) {
577
578						$fileAttachment = (bool) $pl['opt']['file'];
579
580						if ($fileAttachment && !$this->mpdf->allowAnnotationFiles) {
581							$this->logger->warning('Embedded files for annotations have to be allowed explicitly with "allowAnnotationFiles" config key');
582							$fileAttachment = false;
583						}
584
585						$this->writer->object();
586
587						$annot = '';
588						$pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
589						$x = $pl['x'];
590
591						if ($this->mpdf->annotMargin != 0 || $x == 0 || $x < 0) { // Odd page, intentional non-strict comparison
592							$x = ($wPt / Mpdf::SCALE) - $this->mpdf->annotMargin;
593						}
594
595						$w = $h = 0;
596						$a = $x * Mpdf::SCALE;
597						$b = $hPt - ($pl['y'] * Mpdf::SCALE);
598
599						$annot .= '<</Type /Annot ';
600
601						if ($fileAttachment) {
602							$annot .= '/Subtype /FileAttachment ';
603							// Need to set a size for FileAttachment icons
604							if ($pl['opt']['icon'] === 'Paperclip') {
605								$w = 8.235;
606								$h = 20;
607							} elseif ($pl['opt']['icon'] === 'Tag') {
608								$w = 20;
609								$h = 16;
610							} elseif ($pl['opt']['icon'] === 'Graph') {
611								$w = 20;
612								$h = 20;
613							} else {
614								$w = 14;
615								$h = 20;
616							}
617
618							// PushPin
619							$f = $pl['opt']['file'];
620							$f = preg_replace('/^.*\//', '', $f);
621							$f = preg_replace('/[^a-zA-Z0-9._]/', '', $f);
622
623							$annot .= '/FS <</Type /Filespec /F (' . $f . ')';
624							$annot .= '/EF <</F ' . ($this->mpdf->n + 1) . ' 0 R>>';
625							$annot .= '>>';
626
627						} else {
628							$annot .= '/Subtype /Text';
629							$w = 20;
630							$h = 20;  // mPDF 6
631						}
632
633						$rect = sprintf('%.3F %.3F %.3F %.3F', $a, $b - $h, $a + $w, $b);
634						$annot .= ' /Rect [' . $rect . ']';
635
636						// contents = description of file in free text
637						$annot .= ' /Contents ' . $this->writer->utf16BigEndianTextString($pl['txt']);
638
639						$annot .= ' /NM ' . $this->writer->string(sprintf('%04u-%04u', $n, 2000 + $key));
640						$annot .= ' /M ' . $this->writer->string('D:' . date('YmdHis'));
641						$annot .= ' /CreationDate ' . $this->writer->string('D:' . date('YmdHis'));
642						$annot .= ' /Border [0 0 0]';
643
644						if ($this->mpdf->PDFA || $this->mpdf->PDFX) {
645							$annot .= ' /F 28';
646							$annot .= ' /CA 1';
647						} elseif ($pl['opt']['ca'] > 0) {
648							$annot .= ' /CA ' . $pl['opt']['ca'];
649						}
650
651						$annotcolor = ' /C [';
652						if (isset($pl['opt']['c']) && $pl['opt']['c']) {
653							$col = $pl['opt']['c'];
654							if ($col[0] == 3 || $col[0] == 5) {
655								$annotcolor .= sprintf('%.3F %.3F %.3F', ord($col[1]) / 255, ord($col[2]) / 255, ord($col[3]) / 255);
656							} elseif ($col[0] == 1) {
657								$annotcolor .= sprintf('%.3F', ord($col[1]) / 255);
658							} elseif ($col[0] == 4 || $col[0] == 6) {
659								$annotcolor .= sprintf('%.3F %.3F %.3F %.3F', ord($col[1]) / 100, ord($col[2]) / 100, ord($col[3]) / 100, ord($col[4]) / 100);
660							} else {
661								$annotcolor .= '1 1 0';
662							}
663						} else {
664							$annotcolor .= '1 1 0';
665						}
666						$annotcolor .= ']';
667						$annot .= $annotcolor;
668
669						// Usually Author
670						// Use as Title for fileattachment
671						if (isset($pl['opt']['t']) && is_string($pl['opt']['t'])) {
672							$annot .= ' /T ' . $this->writer->utf16BigEndianTextString($pl['opt']['t']);
673						}
674
675						if ($fileAttachment) {
676							$iconsapp = ['Paperclip', 'Graph', 'PushPin', 'Tag'];
677						} else {
678							$iconsapp = ['Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph'];
679						}
680
681						if (isset($pl['opt']['icon']) && in_array($pl['opt']['icon'], $iconsapp)) {
682							$annot .= ' /Name /' . $pl['opt']['icon'];
683						} elseif ($fileAttachment) {
684							$annot .= ' /Name /PushPin';
685						} else {
686							$annot .= ' /Name /Note';
687						}
688
689						if (!$fileAttachment) {
690							// Subj is PDF 1.5 spec.
691							if (!$this->mpdf->PDFA && !$this->mpdf->PDFX && isset($pl['opt']['subj'])) {
692								$annot .= ' /Subj ' . $this->writer->utf16BigEndianTextString($pl['opt']['subj']);
693							}
694							if (!empty($pl['opt']['popup'])) {
695								$annot .= ' /Open true';
696								$annot .= ' /Popup ' . ($this->mpdf->n + 1) . ' 0 R';
697							} else {
698								$annot .= ' /Open false';
699							}
700						}
701
702						$annot .= ' /P ' . $pl['pageobj'] . ' 0 R';
703						$annot .= '>>';
704						$this->writer->write($annot);
705						$this->writer->write('endobj');
706
707						if ($fileAttachment) {
708
709							$file = @file_get_contents($pl['opt']['file']);
710							if (!$file) {
711								throw new \Mpdf\MpdfException('mPDF Error: Cannot access file attachment - ' . $pl['opt']['file']);
712							}
713
714							$filestream = gzcompress($file);
715							$this->writer->object();
716							$this->writer->write('<</Type /EmbeddedFile');
717							$this->writer->write('/Length ' . strlen($filestream));
718							$this->writer->write('/Filter /FlateDecode');
719							$this->writer->write('>>');
720							$this->writer->stream($filestream);
721							$this->writer->write('endobj');
722
723						} elseif (!empty($pl['opt']['popup'])) {
724							$this->writer->object();
725							$annot = '';
726							if (is_array($pl['opt']['popup']) && isset($pl['opt']['popup'][0])) {
727								$x = $pl['opt']['popup'][0] * Mpdf::SCALE;
728							} else {
729								$x = $pl['x'] * Mpdf::SCALE;
730							}
731							if (is_array($pl['opt']['popup']) && isset($pl['opt']['popup'][1])) {
732								$y = $hPt - ($pl['opt']['popup'][1] * Mpdf::SCALE);
733							} else {
734								$y = $hPt - ($pl['y'] * Mpdf::SCALE);
735							}
736							if (is_array($pl['opt']['popup']) && isset($pl['opt']['popup'][2])) {
737								$w = $pl['opt']['popup'][2] * Mpdf::SCALE;
738							} else {
739								$w = 180;
740							}
741							if (is_array($pl['opt']['popup']) && isset($pl['opt']['popup'][3])) {
742								$h = $pl['opt']['popup'][3] * Mpdf::SCALE;
743							} else {
744								$h = 120;
745							}
746							$rect = sprintf('%.3F %.3F %.3F %.3F', $x, $y - $h, $x + $w, $y);
747							$annot .= '<</Type /Annot /Subtype /Popup /Rect [' . $rect . ']';
748							$annot .= ' /M ' . $this->writer->string('D:' . date('YmdHis'));
749							if ($this->mpdf->PDFA || $this->mpdf->PDFX) {
750								$annot .= ' /F 28';
751							}
752							$annot .= ' /Parent ' . ($this->mpdf->n - 1) . ' 0 R';
753							$annot .= '>>';
754							$this->writer->write($annot);
755							$this->writer->write('endobj');
756						}
757					}
758				}
759
760				// Active Forms
761				if (count($this->form->forms) > 0) {
762					$this->form->_putFormItems($n, $hPt);
763				}
764			}
765		}
766
767		// Active Forms - Radio Button Group entries
768		// Output Radio Button Group form entries (radio_on_obj_id already determined)
769		if (count($this->form->form_radio_groups)) {
770			$this->form->_putRadioItems($n);
771		}
772	}
773
774	public function writeEncryption() // _putencryption
775	{
776		$this->writer->write('/Filter /Standard');
777		if ($this->protection->getUseRC128Encryption()) {
778			$this->writer->write('/V 2');
779			$this->writer->write('/R 3');
780			$this->writer->write('/Length 128');
781		} else {
782			$this->writer->write('/V 1');
783			$this->writer->write('/R 2');
784		}
785		$this->writer->write('/O (' . $this->writer->escape($this->protection->getOValue()) . ')');
786		$this->writer->write('/U (' . $this->writer->escape($this->protection->getUValue()) . ')');
787		$this->writer->write('/P ' . $this->protection->getPValue());
788	}
789
790	public function writeTrailer() // _puttrailer
791	{
792		$this->writer->write('/Size ' . ($this->mpdf->n + 1));
793		$this->writer->write('/Root ' . $this->mpdf->n . ' 0 R');
794		$this->writer->write('/Info ' . $this->mpdf->InfoRoot . ' 0 R');
795
796		if ($this->mpdf->encrypted) {
797			$this->writer->write('/Encrypt ' . $this->mpdf->enc_obj_id . ' 0 R');
798			$this->writer->write('/ID [<' . $this->protection->getUniqid() . '> <' . $this->protection->getUniqid() . '>]');
799		} else {
800			$uniqid = md5(time() . $this->mpdf->buffer);
801			$this->writer->write('/ID [<' . $uniqid . '> <' . $uniqid . '>]');
802		}
803	}
804
805	public function setLogger(LoggerInterface $logger)
806	{
807		$this->logger = $logger;
808	}
809
810	private function getVersionString()
811	{
812		$return = Mpdf::VERSION;
813		$headFile = __DIR__ . '/../../.git/HEAD';
814		if (file_exists($headFile)) {
815			$ref = file($headFile);
816			$path = explode('/', $ref[0], 3);
817			$branch = isset($path[2]) ? trim($path[2]) : '';
818			$revFile = __DIR__ . '/../../.git/refs/heads/' . $branch;
819			if ($branch && file_exists($revFile)) {
820				$rev = file($revFile);
821				$rev = substr($rev[0], 0, 7);
822				$return .= ' (' . $rev . ')';
823			}
824		}
825
826		return $return;
827	}
828
829}
830