1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* This file is part of FPDI
|
|
|
5 |
*
|
|
|
6 |
* @package setasign\Fpdi
|
|
|
7 |
* @copyright Copyright (c) 2023 Setasign GmbH & Co. KG (https://www.setasign.com)
|
|
|
8 |
* @license http://opensource.org/licenses/mit-license The MIT License
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
namespace setasign\Fpdi;
|
|
|
12 |
|
|
|
13 |
use setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException;
|
|
|
14 |
use setasign\Fpdi\PdfParser\Filter\FilterException;
|
|
|
15 |
use setasign\Fpdi\PdfParser\PdfParser;
|
|
|
16 |
use setasign\Fpdi\PdfParser\PdfParserException;
|
|
|
17 |
use setasign\Fpdi\PdfParser\StreamReader;
|
|
|
18 |
use setasign\Fpdi\PdfParser\Type\PdfArray;
|
|
|
19 |
use setasign\Fpdi\PdfParser\Type\PdfBoolean;
|
|
|
20 |
use setasign\Fpdi\PdfParser\Type\PdfDictionary;
|
|
|
21 |
use setasign\Fpdi\PdfParser\Type\PdfHexString;
|
|
|
22 |
use setasign\Fpdi\PdfParser\Type\PdfIndirectObject;
|
|
|
23 |
use setasign\Fpdi\PdfParser\Type\PdfIndirectObjectReference;
|
|
|
24 |
use setasign\Fpdi\PdfParser\Type\PdfName;
|
|
|
25 |
use setasign\Fpdi\PdfParser\Type\PdfNull;
|
|
|
26 |
use setasign\Fpdi\PdfParser\Type\PdfNumeric;
|
|
|
27 |
use setasign\Fpdi\PdfParser\Type\PdfStream;
|
|
|
28 |
use setasign\Fpdi\PdfParser\Type\PdfString;
|
|
|
29 |
use setasign\Fpdi\PdfParser\Type\PdfToken;
|
|
|
30 |
use setasign\Fpdi\PdfParser\Type\PdfType;
|
|
|
31 |
use setasign\Fpdi\PdfParser\Type\PdfTypeException;
|
|
|
32 |
use setasign\Fpdi\PdfReader\DataStructure\Rectangle;
|
|
|
33 |
use setasign\Fpdi\PdfReader\PageBoundaries;
|
|
|
34 |
use setasign\Fpdi\PdfReader\PdfReader;
|
|
|
35 |
use setasign\Fpdi\PdfReader\PdfReaderException;
|
|
|
36 |
use /* This namespace/class is used by the commercial FPDI PDF-Parser add-on. */
|
|
|
37 |
/** @noinspection PhpUndefinedClassInspection */
|
|
|
38 |
/** @noinspection PhpUndefinedNamespaceInspection */
|
|
|
39 |
setasign\FpdiPdfParser\PdfParser\PdfParser as FpdiPdfParser;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* The FpdiTrait
|
|
|
43 |
*
|
|
|
44 |
* This trait offers the core functionalities of FPDI. By passing them to a trait we can reuse it with e.g. TCPDF in a
|
|
|
45 |
* very easy way.
|
|
|
46 |
*/
|
|
|
47 |
trait FpdiTrait
|
|
|
48 |
{
|
|
|
49 |
/**
|
|
|
50 |
* The pdf reader instances.
|
|
|
51 |
*
|
|
|
52 |
* @var PdfReader[]
|
|
|
53 |
*/
|
|
|
54 |
protected $readers = [];
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Instances created internally.
|
|
|
58 |
*
|
|
|
59 |
* @var array
|
|
|
60 |
*/
|
|
|
61 |
protected $createdReaders = [];
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* The current reader id.
|
|
|
65 |
*
|
|
|
66 |
* @var string|null
|
|
|
67 |
*/
|
|
|
68 |
protected $currentReaderId;
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Data of all imported pages.
|
|
|
72 |
*
|
|
|
73 |
* @var array
|
|
|
74 |
*/
|
|
|
75 |
protected $importedPages = [];
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* A map from object numbers of imported objects to new assigned object numbers by FPDF.
|
|
|
79 |
*
|
|
|
80 |
* @var array
|
|
|
81 |
*/
|
|
|
82 |
protected $objectMap = [];
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* An array with information about objects, which needs to be copied to the resulting document.
|
|
|
86 |
*
|
|
|
87 |
* @var array
|
|
|
88 |
*/
|
|
|
89 |
protected $objectsToCopy = [];
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Release resources and file handles.
|
|
|
93 |
*
|
|
|
94 |
* This method is called internally when the document is created successfully. By default it only cleans up
|
|
|
95 |
* stream reader instances which were created internally.
|
|
|
96 |
*
|
|
|
97 |
* @param bool $allReaders
|
|
|
98 |
*/
|
|
|
99 |
public function cleanUp($allReaders = false)
|
|
|
100 |
{
|
|
|
101 |
$readers = $allReaders ? array_keys($this->readers) : $this->createdReaders;
|
|
|
102 |
foreach ($readers as $id) {
|
|
|
103 |
$this->readers[$id]->getParser()->getStreamReader()->cleanUp();
|
|
|
104 |
unset($this->readers[$id]);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
$this->createdReaders = [];
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Set the minimal PDF version.
|
|
|
112 |
*
|
|
|
113 |
* @param string $pdfVersion
|
|
|
114 |
*/
|
|
|
115 |
protected function setMinPdfVersion($pdfVersion)
|
|
|
116 |
{
|
|
|
117 |
if (\version_compare($pdfVersion, $this->PDFVersion, '>')) {
|
|
|
118 |
$this->PDFVersion = $pdfVersion;
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/** @noinspection PhpUndefinedClassInspection */
|
|
|
123 |
/**
|
|
|
124 |
* Get a new pdf parser instance.
|
|
|
125 |
*
|
|
|
126 |
* @param StreamReader $streamReader
|
|
|
127 |
* @param array $parserParams Individual parameters passed to the parser instance.
|
|
|
128 |
* @return PdfParser|FpdiPdfParser
|
|
|
129 |
*/
|
|
|
130 |
protected function getPdfParserInstance(StreamReader $streamReader, array $parserParams = [])
|
|
|
131 |
{
|
|
|
132 |
// note: if you get an exception here - turn off errors/warnings on not found classes for your autoloader.
|
|
|
133 |
// psr-4 (https://www.php-fig.org/psr/psr-4/) says: Autoloader implementations MUST NOT throw
|
|
|
134 |
// exceptions, MUST NOT raise errors of any level, and SHOULD NOT return a value.
|
|
|
135 |
/** @noinspection PhpUndefinedClassInspection */
|
|
|
136 |
if (\class_exists(FpdiPdfParser::class)) {
|
|
|
137 |
/** @noinspection PhpUndefinedClassInspection */
|
|
|
138 |
return new FpdiPdfParser($streamReader, $parserParams);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
return new PdfParser($streamReader);
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/**
|
|
|
145 |
* Get an unique reader id by the $file parameter.
|
|
|
146 |
*
|
|
|
147 |
* @param string|resource|PdfReader|StreamReader $file An open file descriptor, a path to a file, a PdfReader
|
|
|
148 |
* instance or a StreamReader instance.
|
|
|
149 |
* @param array $parserParams Individual parameters passed to the parser instance.
|
|
|
150 |
* @return string
|
|
|
151 |
*/
|
|
|
152 |
protected function getPdfReaderId($file, array $parserParams = [])
|
|
|
153 |
{
|
|
|
154 |
if (\is_resource($file)) {
|
|
|
155 |
$id = (string) $file;
|
|
|
156 |
} elseif (\is_string($file)) {
|
|
|
157 |
$id = \realpath($file);
|
|
|
158 |
if ($id === false) {
|
|
|
159 |
$id = $file;
|
|
|
160 |
}
|
|
|
161 |
} elseif (\is_object($file)) {
|
|
|
162 |
$id = \spl_object_hash($file);
|
|
|
163 |
} else {
|
|
|
164 |
throw new \InvalidArgumentException(
|
|
|
165 |
\sprintf('Invalid type in $file parameter (%s)', \gettype($file))
|
|
|
166 |
);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/** @noinspection OffsetOperationsInspection */
|
|
|
170 |
if (isset($this->readers[$id])) {
|
|
|
171 |
return $id;
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
if (\is_resource($file)) {
|
|
|
175 |
$streamReader = new StreamReader($file);
|
|
|
176 |
} elseif (\is_string($file)) {
|
|
|
177 |
$streamReader = StreamReader::createByFile($file);
|
|
|
178 |
$this->createdReaders[] = $id;
|
|
|
179 |
} else {
|
|
|
180 |
$streamReader = $file;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
$reader = new PdfReader($this->getPdfParserInstance($streamReader, $parserParams));
|
|
|
184 |
/** @noinspection OffsetOperationsInspection */
|
|
|
185 |
$this->readers[$id] = $reader;
|
|
|
186 |
|
|
|
187 |
return $id;
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* Get a pdf reader instance by its id.
|
|
|
192 |
*
|
|
|
193 |
* @param string $id
|
|
|
194 |
* @return PdfReader
|
|
|
195 |
*/
|
|
|
196 |
protected function getPdfReader($id)
|
|
|
197 |
{
|
|
|
198 |
if (isset($this->readers[$id])) {
|
|
|
199 |
return $this->readers[$id];
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
throw new \InvalidArgumentException(
|
|
|
203 |
\sprintf('No pdf reader with the given id (%s) exists.', $id)
|
|
|
204 |
);
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
/**
|
|
|
208 |
* Set the source PDF file.
|
|
|
209 |
*
|
|
|
210 |
* @param string|resource|StreamReader $file Path to the file or a stream resource or a StreamReader instance.
|
|
|
211 |
* @return int The page count of the PDF document.
|
|
|
212 |
* @throws PdfParserException
|
|
|
213 |
*/
|
|
|
214 |
public function setSourceFile($file)
|
|
|
215 |
{
|
|
|
216 |
return $this->setSourceFileWithParserParams($file);
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
/**
|
|
|
220 |
* Set the source PDF file with parameters which are passed to the parser instance.
|
|
|
221 |
*
|
|
|
222 |
* This method allows us to pass e.g. authentication information to the parser instance.
|
|
|
223 |
*
|
|
|
224 |
* @param string|resource|StreamReader $file Path to the file or a stream resource or a StreamReader instance.
|
|
|
225 |
* @param array $parserParams Individual parameters passed to the parser instance.
|
|
|
226 |
* @return int The page count of the PDF document.
|
|
|
227 |
* @throws CrossReferenceException
|
|
|
228 |
* @throws PdfParserException
|
|
|
229 |
* @throws PdfTypeException
|
|
|
230 |
*/
|
|
|
231 |
public function setSourceFileWithParserParams($file, array $parserParams = [])
|
|
|
232 |
{
|
|
|
233 |
$this->currentReaderId = $this->getPdfReaderId($file, $parserParams);
|
|
|
234 |
$this->objectsToCopy[$this->currentReaderId] = [];
|
|
|
235 |
|
|
|
236 |
$reader = $this->getPdfReader($this->currentReaderId);
|
|
|
237 |
$this->setMinPdfVersion($reader->getPdfVersion());
|
|
|
238 |
|
|
|
239 |
return $reader->getPageCount();
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
/**
|
|
|
243 |
* Imports a page.
|
|
|
244 |
*
|
|
|
245 |
* @param int $pageNumber The page number.
|
|
|
246 |
* @param string $box The page boundary to import. Default set to PageBoundaries::CROP_BOX.
|
|
|
247 |
* @param bool $groupXObject Define the form XObject as a group XObject to support transparency (if used).
|
|
|
248 |
* @param bool $importExternalLinks Define whether external links are imported or not.
|
|
|
249 |
* @return string A unique string identifying the imported page.
|
|
|
250 |
* @throws CrossReferenceException
|
|
|
251 |
* @throws FilterException
|
|
|
252 |
* @throws PdfParserException
|
|
|
253 |
* @throws PdfTypeException
|
|
|
254 |
* @throws PdfReaderException
|
|
|
255 |
* @see PageBoundaries
|
|
|
256 |
*/
|
|
|
257 |
public function importPage(
|
|
|
258 |
$pageNumber,
|
|
|
259 |
$box = PageBoundaries::CROP_BOX,
|
|
|
260 |
$groupXObject = true,
|
|
|
261 |
$importExternalLinks = false
|
|
|
262 |
) {
|
|
|
263 |
if ($this->currentReaderId === null) {
|
|
|
264 |
throw new \BadMethodCallException('No reader initiated. Call setSourceFile() first.');
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
$pageId = $this->currentReaderId;
|
|
|
268 |
|
|
|
269 |
$pageNumber = (int)$pageNumber;
|
|
|
270 |
$pageId .= '|' . $pageNumber . '|' . ($groupXObject ? '1' : '0') . '|' . ($importExternalLinks ? '1' : '0');
|
|
|
271 |
|
|
|
272 |
// for backwards compatibility with FPDI 1
|
|
|
273 |
$box = \ltrim($box, '/');
|
|
|
274 |
if (!PageBoundaries::isValidName($box)) {
|
|
|
275 |
throw new \InvalidArgumentException(
|
|
|
276 |
\sprintf('Box name is invalid: "%s"', $box)
|
|
|
277 |
);
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
$pageId .= '|' . $box;
|
|
|
281 |
|
|
|
282 |
if (isset($this->importedPages[$pageId])) {
|
|
|
283 |
return $pageId;
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
$reader = $this->getPdfReader($this->currentReaderId);
|
|
|
287 |
$page = $reader->getPage($pageNumber);
|
|
|
288 |
|
|
|
289 |
$bbox = $page->getBoundary($box);
|
|
|
290 |
if ($bbox === false) {
|
|
|
291 |
throw new PdfReaderException(
|
|
|
292 |
\sprintf("Page doesn't have a boundary box (%s).", $box),
|
|
|
293 |
PdfReaderException::MISSING_DATA
|
|
|
294 |
);
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
$dict = new PdfDictionary();
|
|
|
298 |
$dict->value['Type'] = PdfName::create('XObject');
|
|
|
299 |
$dict->value['Subtype'] = PdfName::create('Form');
|
|
|
300 |
$dict->value['FormType'] = PdfNumeric::create(1);
|
|
|
301 |
$dict->value['BBox'] = $bbox->toPdfArray();
|
|
|
302 |
|
|
|
303 |
if ($groupXObject) {
|
|
|
304 |
$this->setMinPdfVersion('1.4');
|
|
|
305 |
$dict->value['Group'] = PdfDictionary::create([
|
|
|
306 |
'Type' => PdfName::create('Group'),
|
|
|
307 |
'S' => PdfName::create('Transparency')
|
|
|
308 |
]);
|
|
|
309 |
}
|
|
|
310 |
|
|
|
311 |
$resources = $page->getAttribute('Resources');
|
|
|
312 |
if ($resources !== null) {
|
|
|
313 |
$dict->value['Resources'] = $resources;
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
list($width, $height) = $page->getWidthAndHeight($box);
|
|
|
317 |
|
|
|
318 |
$a = 1;
|
|
|
319 |
$b = 0;
|
|
|
320 |
$c = 0;
|
|
|
321 |
$d = 1;
|
|
|
322 |
$e = -$bbox->getLlx();
|
|
|
323 |
$f = -$bbox->getLly();
|
|
|
324 |
|
|
|
325 |
$rotation = $page->getRotation();
|
|
|
326 |
|
|
|
327 |
if ($rotation !== 0) {
|
|
|
328 |
$rotation *= -1;
|
|
|
329 |
$angle = $rotation * M_PI / 180;
|
|
|
330 |
$a = \cos($angle);
|
|
|
331 |
$b = \sin($angle);
|
|
|
332 |
$c = -$b;
|
|
|
333 |
$d = $a;
|
|
|
334 |
|
|
|
335 |
switch ($rotation) {
|
|
|
336 |
case -90:
|
|
|
337 |
$e = -$bbox->getLly();
|
|
|
338 |
$f = $bbox->getUrx();
|
|
|
339 |
break;
|
|
|
340 |
case -180:
|
|
|
341 |
$e = $bbox->getUrx();
|
|
|
342 |
$f = $bbox->getUry();
|
|
|
343 |
break;
|
|
|
344 |
case -270:
|
|
|
345 |
$e = $bbox->getUry();
|
|
|
346 |
$f = -$bbox->getLlx();
|
|
|
347 |
break;
|
|
|
348 |
}
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
// we need to rotate/translate
|
|
|
352 |
if ($a != 1 || $b != 0 || $c != 0 || $d != 1 || $e != 0 || $f != 0) {
|
|
|
353 |
$dict->value['Matrix'] = PdfArray::create([
|
|
|
354 |
PdfNumeric::create($a), PdfNumeric::create($b), PdfNumeric::create($c),
|
|
|
355 |
PdfNumeric::create($d), PdfNumeric::create($e), PdfNumeric::create($f)
|
|
|
356 |
]);
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
// try to use the existing content stream
|
|
|
360 |
$pageDict = $page->getPageDictionary();
|
|
|
361 |
|
|
|
362 |
try {
|
|
|
363 |
$contentsObject = PdfType::resolve(PdfDictionary::get($pageDict, 'Contents'), $reader->getParser(), true);
|
|
|
364 |
$contents = PdfType::resolve($contentsObject, $reader->getParser());
|
|
|
365 |
|
|
|
366 |
// just copy the stream reference if it is only a single stream
|
|
|
367 |
if (
|
|
|
368 |
($contentsIsStream = ($contents instanceof PdfStream))
|
|
|
369 |
|| ($contents instanceof PdfArray && \count($contents->value) === 1)
|
|
|
370 |
) {
|
|
|
371 |
if ($contentsIsStream) {
|
|
|
372 |
/**
|
|
|
373 |
* @var PdfIndirectObject $contentsObject
|
|
|
374 |
*/
|
|
|
375 |
$stream = $contents;
|
|
|
376 |
} else {
|
|
|
377 |
$stream = PdfType::resolve($contents->value[0], $reader->getParser());
|
|
|
378 |
}
|
|
|
379 |
|
|
|
380 |
$filter = PdfDictionary::get($stream->value, 'Filter');
|
|
|
381 |
if (!$filter instanceof PdfNull) {
|
|
|
382 |
$dict->value['Filter'] = $filter;
|
|
|
383 |
}
|
|
|
384 |
$length = PdfType::resolve(PdfDictionary::get($stream->value, 'Length'), $reader->getParser());
|
|
|
385 |
$dict->value['Length'] = $length;
|
|
|
386 |
$stream->value = $dict;
|
|
|
387 |
// otherwise extract it from the array and re-compress the whole stream
|
|
|
388 |
} else {
|
|
|
389 |
$streamContent = $this->compress
|
|
|
390 |
? \gzcompress($page->getContentStream())
|
|
|
391 |
: $page->getContentStream();
|
|
|
392 |
|
|
|
393 |
$dict->value['Length'] = PdfNumeric::create(\strlen($streamContent));
|
|
|
394 |
if ($this->compress) {
|
|
|
395 |
$dict->value['Filter'] = PdfName::create('FlateDecode');
|
|
|
396 |
}
|
|
|
397 |
|
|
|
398 |
$stream = PdfStream::create($dict, $streamContent);
|
|
|
399 |
}
|
|
|
400 |
// Catch faulty pages and use an empty content stream
|
|
|
401 |
} catch (FpdiException $e) {
|
|
|
402 |
$dict->value['Length'] = PdfNumeric::create(0);
|
|
|
403 |
$stream = PdfStream::create($dict, '');
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
$externalLinks = [];
|
|
|
407 |
if ($importExternalLinks) {
|
|
|
408 |
$externalLinks = $page->getExternalLinks($box);
|
|
|
409 |
}
|
|
|
410 |
|
|
|
411 |
$this->importedPages[$pageId] = [
|
|
|
412 |
'objectNumber' => null,
|
|
|
413 |
'readerId' => $this->currentReaderId,
|
|
|
414 |
'id' => 'TPL' . $this->getNextTemplateId(),
|
|
|
415 |
'width' => $width / $this->k,
|
|
|
416 |
'height' => $height / $this->k,
|
|
|
417 |
'stream' => $stream,
|
|
|
418 |
'externalLinks' => $externalLinks
|
|
|
419 |
];
|
|
|
420 |
|
|
|
421 |
return $pageId;
|
|
|
422 |
}
|
|
|
423 |
|
|
|
424 |
/**
|
|
|
425 |
* Draws an imported page onto the page.
|
|
|
426 |
*
|
|
|
427 |
* Give only one of the size parameters (width, height) to calculate the other one automatically in view to the
|
|
|
428 |
* aspect ratio.
|
|
|
429 |
*
|
|
|
430 |
* @param mixed $pageId The page id
|
|
|
431 |
* @param float|int|array $x The abscissa of upper-left corner. Alternatively you could use an assoc array
|
|
|
432 |
* with the keys "x", "y", "width", "height", "adjustPageSize".
|
|
|
433 |
* @param float|int $y The ordinate of upper-left corner.
|
|
|
434 |
* @param float|int|null $width The width.
|
|
|
435 |
* @param float|int|null $height The height.
|
|
|
436 |
* @param bool $adjustPageSize
|
|
|
437 |
* @return array The size.
|
|
|
438 |
* @see Fpdi::getTemplateSize()
|
|
|
439 |
*/
|
|
|
440 |
public function useImportedPage($pageId, $x = 0, $y = 0, $width = null, $height = null, $adjustPageSize = false)
|
|
|
441 |
{
|
|
|
442 |
if (\is_array($x)) {
|
|
|
443 |
/** @noinspection OffsetOperationsInspection */
|
|
|
444 |
unset($x['pageId']);
|
|
|
445 |
\extract($x, EXTR_IF_EXISTS);
|
|
|
446 |
/** @noinspection NotOptimalIfConditionsInspection */
|
|
|
447 |
if (\is_array($x)) {
|
|
|
448 |
$x = 0;
|
|
|
449 |
}
|
|
|
450 |
}
|
|
|
451 |
|
|
|
452 |
if (!isset($this->importedPages[$pageId])) {
|
|
|
453 |
throw new \InvalidArgumentException('Imported page does not exist!');
|
|
|
454 |
}
|
|
|
455 |
|
|
|
456 |
$importedPage = $this->importedPages[$pageId];
|
|
|
457 |
|
|
|
458 |
$originalSize = $this->getTemplateSize($pageId);
|
|
|
459 |
$newSize = $this->getTemplateSize($pageId, $width, $height);
|
|
|
460 |
if ($adjustPageSize) {
|
|
|
461 |
$this->setPageFormat($newSize, $newSize['orientation']);
|
|
|
462 |
}
|
|
|
463 |
|
|
|
464 |
$scaleX = ($newSize['width'] / $originalSize['width']);
|
|
|
465 |
$scaleY = ($newSize['height'] / $originalSize['height']);
|
|
|
466 |
$xPt = $x * $this->k;
|
|
|
467 |
$yPt = $y * $this->k;
|
|
|
468 |
$newHeightPt = $newSize['height'] * $this->k;
|
|
|
469 |
|
|
|
470 |
$this->_out(
|
|
|
471 |
// reset standard values, translate and scale
|
|
|
472 |
\sprintf(
|
|
|
473 |
'q 0 J 1 w 0 j 0 G 0 g %.4F 0 0 %.4F %.4F %.4F cm /%s Do Q',
|
|
|
474 |
$scaleX,
|
|
|
475 |
$scaleY,
|
|
|
476 |
$xPt,
|
|
|
477 |
$this->hPt - $yPt - $newHeightPt,
|
|
|
478 |
$importedPage['id']
|
|
|
479 |
)
|
|
|
480 |
);
|
|
|
481 |
|
|
|
482 |
if (count($importedPage['externalLinks']) > 0) {
|
|
|
483 |
foreach ($importedPage['externalLinks'] as $externalLink) {
|
|
|
484 |
// mPDF uses also 'externalLinks' but doesn't come with a rect-value
|
|
|
485 |
if (!isset($externalLink['rect'])) {
|
|
|
486 |
continue;
|
|
|
487 |
}
|
|
|
488 |
|
|
|
489 |
/** @var Rectangle $rect */
|
|
|
490 |
$rect = $externalLink['rect'];
|
|
|
491 |
$this->Link(
|
|
|
492 |
$x + $rect->getLlx() / $this->k * $scaleX,
|
|
|
493 |
$y + $newSize['height'] - ($rect->getLly() + $rect->getHeight()) / $this->k * $scaleY,
|
|
|
494 |
$rect->getWidth() / $this->k * $scaleX,
|
|
|
495 |
$rect->getHeight() / $this->k * $scaleY,
|
|
|
496 |
$externalLink['uri']
|
|
|
497 |
);
|
|
|
498 |
|
|
|
499 |
$this->adjustLastLink($externalLink, $xPt, $scaleX, $yPt, $newHeightPt, $scaleY, $importedPage);
|
|
|
500 |
}
|
|
|
501 |
}
|
|
|
502 |
|
|
|
503 |
return $newSize;
|
|
|
504 |
}
|
|
|
505 |
|
|
|
506 |
/**
|
|
|
507 |
* This method will add additional data to the last created link/annotation.
|
|
|
508 |
*
|
|
|
509 |
* It is separated because TCPDF uses its own logic to handle link annotations.
|
|
|
510 |
* This method is overwritten in the TCPDF implementation.
|
|
|
511 |
*
|
|
|
512 |
* @param array $externalLink
|
|
|
513 |
* @param float|int $xPt
|
|
|
514 |
* @param float|int $scaleX
|
|
|
515 |
* @param float|int $yPt
|
|
|
516 |
* @param float|int $newHeightPt
|
|
|
517 |
* @param float|int $scaleY
|
|
|
518 |
* @param array $importedPage
|
|
|
519 |
* @return void
|
|
|
520 |
*/
|
|
|
521 |
protected function adjustLastLink($externalLink, $xPt, $scaleX, $yPt, $newHeightPt, $scaleY, $importedPage)
|
|
|
522 |
{
|
|
|
523 |
// let's create a relation of the newly created link to the data of the external link
|
|
|
524 |
$lastLink = count($this->PageLinks[$this->page]);
|
|
|
525 |
$this->PageLinks[$this->page][$lastLink - 1]['importedLink'] = $externalLink;
|
|
|
526 |
if (count($externalLink['quadPoints']) > 0) {
|
|
|
527 |
$quadPoints = [];
|
|
|
528 |
for ($i = 0, $n = count($externalLink['quadPoints']); $i < $n; $i += 2) {
|
|
|
529 |
$quadPoints[] = $xPt + $externalLink['quadPoints'][$i] * $scaleX;
|
|
|
530 |
$quadPoints[] = $this->hPt - $yPt - $newHeightPt + $externalLink['quadPoints'][$i + 1] * $scaleY;
|
|
|
531 |
}
|
|
|
532 |
|
|
|
533 |
$this->PageLinks[$this->page][$lastLink - 1]['quadPoints'] = $quadPoints;
|
|
|
534 |
}
|
|
|
535 |
}
|
|
|
536 |
|
|
|
537 |
/**
|
|
|
538 |
* Get the size of an imported page.
|
|
|
539 |
*
|
|
|
540 |
* Give only one of the size parameters (width, height) to calculate the other one automatically in view to the
|
|
|
541 |
* aspect ratio.
|
|
|
542 |
*
|
|
|
543 |
* @param mixed $tpl The template id
|
|
|
544 |
* @param float|int|null $width The width.
|
|
|
545 |
* @param float|int|null $height The height.
|
|
|
546 |
* @return array|bool An array with following keys: width, height, 0 (=width), 1 (=height), orientation (L or P)
|
|
|
547 |
*/
|
|
|
548 |
public function getImportedPageSize($tpl, $width = null, $height = null)
|
|
|
549 |
{
|
|
|
550 |
if (isset($this->importedPages[$tpl])) {
|
|
|
551 |
$importedPage = $this->importedPages[$tpl];
|
|
|
552 |
|
|
|
553 |
if ($width === null && $height === null) {
|
|
|
554 |
$width = $importedPage['width'];
|
|
|
555 |
$height = $importedPage['height'];
|
|
|
556 |
} elseif ($width === null) {
|
|
|
557 |
$width = $height * $importedPage['width'] / $importedPage['height'];
|
|
|
558 |
}
|
|
|
559 |
|
|
|
560 |
if ($height === null) {
|
|
|
561 |
$height = $width * $importedPage['height'] / $importedPage['width'];
|
|
|
562 |
}
|
|
|
563 |
|
|
|
564 |
if ($height <= 0. || $width <= 0.) {
|
|
|
565 |
throw new \InvalidArgumentException('Width or height parameter needs to be larger than zero.');
|
|
|
566 |
}
|
|
|
567 |
|
|
|
568 |
return [
|
|
|
569 |
'width' => $width,
|
|
|
570 |
'height' => $height,
|
|
|
571 |
|
|
|
572 |
1 => $height,
|
|
|
573 |
'orientation' => $width > $height ? 'L' : 'P'
|
|
|
574 |
];
|
|
|
575 |
}
|
|
|
576 |
|
|
|
577 |
return false;
|
|
|
578 |
}
|
|
|
579 |
|
|
|
580 |
/**
|
|
|
581 |
* Writes a PdfType object to the resulting buffer.
|
|
|
582 |
*
|
|
|
583 |
* @param PdfType $value
|
|
|
584 |
* @throws PdfTypeException
|
|
|
585 |
*/
|
|
|
586 |
protected function writePdfType(PdfType $value)
|
|
|
587 |
{
|
|
|
588 |
if ($value instanceof PdfNumeric) {
|
|
|
589 |
if (\is_int($value->value)) {
|
|
|
590 |
$this->_put($value->value . ' ', false);
|
|
|
591 |
} else {
|
|
|
592 |
$this->_put(\rtrim(\rtrim(\sprintf('%.5F', $value->value), '0'), '.') . ' ', false);
|
|
|
593 |
}
|
|
|
594 |
} elseif ($value instanceof PdfName) {
|
|
|
595 |
$this->_put('/' . $value->value . ' ', false);
|
|
|
596 |
} elseif ($value instanceof PdfString) {
|
|
|
597 |
$this->_put('(' . $value->value . ')', false);
|
|
|
598 |
} elseif ($value instanceof PdfHexString) {
|
|
|
599 |
$this->_put('<' . $value->value . '>', false);
|
|
|
600 |
} elseif ($value instanceof PdfBoolean) {
|
|
|
601 |
$this->_put($value->value ? 'true ' : 'false ', false);
|
|
|
602 |
} elseif ($value instanceof PdfArray) {
|
|
|
603 |
$this->_put('[', false);
|
|
|
604 |
foreach ($value->value as $entry) {
|
|
|
605 |
$this->writePdfType($entry);
|
|
|
606 |
}
|
|
|
607 |
$this->_put(']');
|
|
|
608 |
} elseif ($value instanceof PdfDictionary) {
|
|
|
609 |
$this->_put('<<', false);
|
|
|
610 |
foreach ($value->value as $name => $entry) {
|
|
|
611 |
$this->_put('/' . $name . ' ', false);
|
|
|
612 |
$this->writePdfType($entry);
|
|
|
613 |
}
|
|
|
614 |
$this->_put('>>');
|
|
|
615 |
} elseif ($value instanceof PdfToken) {
|
|
|
616 |
$this->_put($value->value);
|
|
|
617 |
} elseif ($value instanceof PdfNull) {
|
|
|
618 |
$this->_put('null ', false);
|
|
|
619 |
} elseif ($value instanceof PdfStream) {
|
|
|
620 |
$this->writePdfType($value->value);
|
|
|
621 |
$this->_put('stream');
|
|
|
622 |
$this->_put($value->getStream());
|
|
|
623 |
$this->_put('endstream');
|
|
|
624 |
} elseif ($value instanceof PdfIndirectObjectReference) {
|
|
|
625 |
if (!isset($this->objectMap[$this->currentReaderId])) {
|
|
|
626 |
$this->objectMap[$this->currentReaderId] = [];
|
|
|
627 |
}
|
|
|
628 |
|
|
|
629 |
if (!isset($this->objectMap[$this->currentReaderId][$value->value])) {
|
|
|
630 |
$this->objectMap[$this->currentReaderId][$value->value] = ++$this->n;
|
|
|
631 |
$this->objectsToCopy[$this->currentReaderId][] = $value->value;
|
|
|
632 |
}
|
|
|
633 |
|
|
|
634 |
$this->_put($this->objectMap[$this->currentReaderId][$value->value] . ' 0 R ', false);
|
|
|
635 |
} elseif ($value instanceof PdfIndirectObject) {
|
|
|
636 |
$n = $this->objectMap[$this->currentReaderId][$value->objectNumber];
|
|
|
637 |
$this->_newobj($n);
|
|
|
638 |
$this->writePdfType($value->value);
|
|
|
639 |
|
|
|
640 |
// add newline before "endobj" for all objects in view to PDF/A conformance
|
|
|
641 |
if (
|
|
|
642 |
!(
|
|
|
643 |
($value->value instanceof PdfArray) ||
|
|
|
644 |
($value->value instanceof PdfDictionary) ||
|
|
|
645 |
($value->value instanceof PdfToken) ||
|
|
|
646 |
($value->value instanceof PdfStream)
|
|
|
647 |
)
|
|
|
648 |
) {
|
|
|
649 |
$this->_put("\n", false);
|
|
|
650 |
}
|
|
|
651 |
|
|
|
652 |
$this->_put('endobj');
|
|
|
653 |
}
|
|
|
654 |
}
|
|
|
655 |
}
|