- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFpdiTrait.php
More file actions
412 lines (352 loc) · 11.5 KB
/
FpdiTrait.php
File metadata and controls
412 lines (352 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
namespace Mpdf;
use setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException;
use setasign\Fpdi\PdfParser\Filter\AsciiHex;
use setasign\Fpdi\PdfParser\Type\PdfArray;
use setasign\Fpdi\PdfParser\Type\PdfIndirectObjectReference;
use setasign\Fpdi\PdfReader\PageBoundaries;
use setasign\Fpdi\PdfParser\Type\PdfHexString;
use setasign\Fpdi\PdfParser\Type\PdfIndirectObject;
use setasign\Fpdi\PdfParser\Type\PdfNull;
use setasign\Fpdi\PdfParser\Type\PdfNumeric;
use setasign\Fpdi\PdfParser\Type\PdfStream;
use setasign\Fpdi\PdfParser\Type\PdfString;
use setasign\Fpdi\PdfParser\Type\PdfType;
use setasign\Fpdi\PdfParser\Type\PdfTypeException;
/**
* @mixin Mpdf
*/
trait FpdiTrait
{
use \setasign\Fpdi\FpdiTrait {
writePdfType as fpdiWritePdfType;
useImportedPage as fpdiUseImportedPage;
importPage as fpdiImportPage;
}
protected $k = Mpdf::SCALE;
/**
* The currently used object number.
*
* @var int
*/
public $currentObjectNumber;
/**
* A counter for template ids.
*
* @var int
*/
protected $templateId = 0;
protected function setPageFormat($format, $orientation)
{
// in mPDF this needs to be "P" (why ever)
$orientation = 'P';
$this->_setPageSize([$format['width'], $format['height']], $orientation);
if ($orientation != $this->DefOrientation) {
$this->OrientationChanges[$this->page] = true;
}
$this->wPt = $this->fwPt;
$this->hPt = $this->fhPt;
$this->w = $this->fw;
$this->h = $this->fh;
$this->CurOrientation = $orientation;
$this->ResetMargins();
$this->pgwidth = $this->w - $this->lMargin - $this->rMargin;
$this->PageBreakTrigger = $this->h - $this->bMargin;
$this->pageDim[$this->page]['w'] = $this->w;
$this->pageDim[$this->page]['h'] = $this->h;
}
/**
* Set the minimal PDF version.
*
* @param string $pdfVersion
*/
protected function setMinPdfVersion($pdfVersion)
{
if (\version_compare($pdfVersion, $this->pdf_version, '>')) {
$this->pdf_version = $pdfVersion;
}
}
/**
* Get the next template id.
*
* @return int
*/
protected function getNextTemplateId()
{
return $this->templateId++;
}
/**
* Draws an imported page or a template onto the page or another template.
*
* Omit one of the size parameters (width, height) to calculate the other one automatically in view to the aspect
* ratio.
*
* @param mixed $tpl The template id
* @param float|int|array $x The abscissa of upper-left corner. Alternatively you could use an assoc array
* with the keys "x", "y", "width", "height", "adjustPageSize".
* @param float|int $y The ordinate of upper-left corner.
* @param float|int|null $width The width.
* @param float|int|null $height The height.
* @param bool $adjustPageSize
* @return array The size
* @see Fpdi::getTemplateSize()
*/
public function useTemplate($tpl, $x = 0, $y = 0, $width = null, $height = null, $adjustPageSize = false)
{
return $this->useImportedPage($tpl, $x, $y, $width, $height, $adjustPageSize);
}
/**
* Draws an imported page onto the page.
*
* Omit one of the size parameters (width, height) to calculate the other one automatically in view to the aspect
* ratio.
*
* @param mixed $pageId The page id
* @param float|int|array $x The abscissa of upper-left corner. Alternatively you could use an assoc array
* with the keys "x", "y", "width", "height", "adjustPageSize".
* @param float|int $y The ordinate of upper-left corner.
* @param float|int|null $width The width.
* @param float|int|null $height The height.
* @param bool $adjustPageSize
* @return array The size.
* @see Fpdi::getTemplateSize()
*/
public function useImportedPage($pageId, $x = 0, $y = 0, $width = null, $height = null, $adjustPageSize = false)
{
if ($this->state == 0) {
$this->AddPage();
}
/* Extract $x if an array */
if (is_array($x)) {
unset($x['pageId']);
extract($x, EXTR_IF_EXISTS);
if (is_array($x)) {
$x = 0;
}
}
$newSize = $this->fpdiUseImportedPage($pageId, $x, $y, $width, $height, $adjustPageSize);
$this->setImportedPageLinks($pageId, $x, $y, $newSize);
return $newSize;
}
/**
* Imports a page.
*
* @param int $pageNumber The page number.
* @param string $box The page boundary to import. Default set to PageBoundaries::CROP_BOX.
* @param bool $groupXObject Define the form XObject as a group XObject to support transparency (if used).
* @return string A unique string identifying the imported page.
* @throws CrossReferenceException
* @throws FilterException
* @throws PdfParserException
* @throws PdfTypeException
* @throws PdfReaderException
* @see PageBoundaries
*/
public function importPage($pageNumber, $box = PageBoundaries::CROP_BOX, $groupXObject = true)
{
$pageId = $this->fpdiImportPage($pageNumber, $box, $groupXObject);
$this->importedPages[$pageId]['externalLinks'] = $this->getImportedExternalPageLinks($pageNumber);
return $pageId;
}
/**
* Imports the external page links
*
* @param int $pageNumber The page number.
* @return array
* @throws CrossReferenceException
* @throws PdfTypeException
* @throws \setasign\Fpdi\PdfParser\PdfParserException
*/
public function getImportedExternalPageLinks($pageNumber)
{
$links = [];
$reader = $this->getPdfReader($this->currentReaderId);
$parser = $reader->getParser();
$page = $reader->getPage($pageNumber);
$page->getPageDictionary();
$annotations = $page->getAttribute('Annots');
if ($annotations instanceof PdfIndirectObjectReference) {
$annotations = PdfType::resolve($parser->getIndirectObject($annotations->value), $parser);
}
if ($annotations instanceof PdfArray) {
$getAttribute = function ($array, $key) {
if (isset($array[$key]->value)) {
return $array[$key]->value;
}
return '';
};
foreach ($annotations->value as $annotation) {
$annotation = PdfType::resolve($annotation, $parser)->value;
/* Skip over any annotations that aren't links */
$type = $getAttribute($annotation, 'Type');
$subtype = $getAttribute($annotation, 'Subtype');
if ($type !== 'Annot' || $subtype !== 'Link' || !isset($annotation['A'])) {
continue;
}
/* Calculate the link positioning */
$position = $getAttribute($annotation, 'Rect');
if (count($position) !== 4) {
continue;
}
$x1 = $getAttribute($position, 0) / Mpdf::SCALE;
$y1 = $getAttribute($position, 1) / Mpdf::SCALE;
$x2 = $getAttribute($position, 2) / Mpdf::SCALE;
$y2 = $getAttribute($position, 3) / Mpdf::SCALE;
$width = $x2 - $x1;
$height = $y2 - $y1;
$link = $annotation['A'] instanceof PdfIndirectObjectReference ? PdfType::resolve($annotation['A'], $parser)->value : $getAttribute($annotation, 'A');
if (isset($link['URI'])) {
$links[] = [
'x' => $x1,
'y' => $y1,
'width' => $width,
'height' => $height,
'url' => $getAttribute($link, 'URI')
];
}
}
}
return $links;
}
/**
* @param mixed $pageId The page id
* @param int|float $x The abscissa of upper-left corner.
* @param int|float $y The ordinate of upper-right corner.
* @param array $newSize The size.
*/
public function setImportedPageLinks($pageId, $x, $y, $newSize)
{
$originalSize = $this->getTemplateSize($pageId);
$pageHeightDifference = $this->h - $newSize['height'];
/* Handle different aspect ratio */
$widthRatio = $newSize['width'] / $originalSize['width'];
$heightRatio = $newSize['height'] / $originalSize['height'];
foreach ($this->importedPages[$pageId]['externalLinks'] as $item) {
$item['x'] *= $widthRatio;
$item['width'] *= $widthRatio;
$item['y'] *= $heightRatio;
$item['height'] *= $heightRatio;
$this->Link(
$item['x'] + $x,
/* convert Y to be measured from the top of the page */
$this->h - $item['y'] - $item['height'] - $pageHeightDifference + $y,
$item['width'],
$item['height'],
$item['url']
);
}
}
/**
* Get the size of an imported page or template.
*
* Omit one of the size parameters (width, height) to calculate the other one automatically in view to the aspect
* ratio.
*
* @param mixed $tpl The template id
* @param float|int|null $width The width.
* @param float|int|null $height The height.
* @return array|bool An array with following keys: width, height, 0 (=width), 1 (=height), orientation (L or P)
*/
public function getTemplateSize($tpl, $width = null, $height = null)
{
return $this->getImportedPageSize($tpl, $width, $height);
}
/**
* @throws CrossReferenceException
* @throws PdfTypeException
* @throws \setasign\Fpdi\PdfParser\PdfParserException
*/
public function writeImportedPagesAndResolvedObjects()
{
$this->currentReaderId = null;
foreach ($this->importedPages as $key => $pageData) {
$this->writer->object();
$this->importedPages[$key]['objectNumber'] = $this->n;
$this->currentReaderId = $pageData['readerId'];
$this->writePdfType($pageData['stream']);
$this->_put('endobj');
}
foreach (\array_keys($this->readers) as $readerId) {
$parser = $this->getPdfReader($readerId)->getParser();
$this->currentReaderId = $readerId;
while (($objectNumber = \array_pop($this->objectsToCopy[$readerId])) !== null) {
try {
$object = $parser->getIndirectObject($objectNumber);
} catch (CrossReferenceException $e) {
if ($e->getCode() === CrossReferenceException::OBJECT_NOT_FOUND) {
$object = PdfIndirectObject::create($objectNumber, 0, new PdfNull());
} else {
throw $e;
}
}
$this->writePdfType($object);
}
}
$this->currentReaderId = null;
}
public function getImportedPages()
{
return $this->importedPages;
}
protected function _put($s, $newLine = true)
{
if ($newLine) {
$this->buffer .= $s . "\n";
} else {
$this->buffer .= $s;
}
}
/**
* Writes a PdfType object to the resulting buffer.
*
* @param PdfType $value
* @throws PdfTypeException
*/
public function writePdfType(PdfType $value)
{
if (!$this->encrypted) {
if ($value instanceof PdfIndirectObject) {
/**
* @var $value PdfIndirectObject
*/
$n = $this->objectMap[$this->currentReaderId][$value->objectNumber];
$this->writer->object($n);
$this->writePdfType($value->value);
$this->_put('endobj');
return;
}
$this->fpdiWritePdfType($value);
return;
}
if ($value instanceof PdfString) {
$string = PdfString::unescape($value->value);
$string = $this->protection->rc4($this->protection->objectKey($this->currentObjectNumber), $string);
$value->value = $this->writer->escape($string);
} elseif ($value instanceof PdfHexString) {
$filter = new AsciiHex();
$string = $filter->decode($value->value);
$string = $this->protection->rc4($this->protection->objectKey($this->currentObjectNumber), $string);
$value->value = $filter->encode($string, true);
} elseif ($value instanceof PdfStream) {
$stream = $value->getStream();
$stream = $this->protection->rc4($this->protection->objectKey($this->currentObjectNumber), $stream);
$dictionary = $value->value;
$dictionary->value['Length'] = PdfNumeric::create(\strlen($stream));
$value = PdfStream::create($dictionary, $stream);
} elseif ($value instanceof PdfIndirectObject) {
/**
* @var $value PdfIndirectObject
*/
$this->currentObjectNumber = $this->objectMap[$this->currentReaderId][$value->objectNumber];
/**
* @var $value PdfIndirectObject
*/
$n = $this->objectMap[$this->currentReaderId][$value->objectNumber];
$this->writer->object($n);
$this->writePdfType($value->value);
$this->_put('endobj');
return;
}
$this->fpdiWritePdfType($value);
}
}