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