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\PdfParserException;
|
|
|
15 |
use setasign\Fpdi\PdfParser\Type\PdfIndirectObject;
|
|
|
16 |
use setasign\Fpdi\PdfParser\Type\PdfNull;
|
|
|
17 |
use setasign\Fpdi\PdfParser\Type\PdfType;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* This trait is used for the implementation of FPDI in FPDF and tFPDF.
|
|
|
21 |
*/
|
|
|
22 |
trait FpdfTrait
|
|
|
23 |
{
|
|
|
24 |
protected function _enddoc()
|
|
|
25 |
{
|
|
|
26 |
parent::_enddoc();
|
|
|
27 |
$this->cleanUp();
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Draws an imported page or a template onto the page or another template.
|
|
|
32 |
*
|
|
|
33 |
* Give only one of the size parameters (width, height) to calculate the other one automatically in view to the
|
|
|
34 |
* aspect ratio.
|
|
|
35 |
*
|
|
|
36 |
* @param mixed $tpl The template id
|
|
|
37 |
* @param float|int|array $x The abscissa of upper-left corner. Alternatively you could use an assoc array
|
|
|
38 |
* with the keys "x", "y", "width", "height", "adjustPageSize".
|
|
|
39 |
* @param float|int $y The ordinate of upper-left corner.
|
|
|
40 |
* @param float|int|null $width The width.
|
|
|
41 |
* @param float|int|null $height The height.
|
|
|
42 |
* @param bool $adjustPageSize
|
|
|
43 |
* @return array The size
|
|
|
44 |
* @see Fpdi::getTemplateSize()
|
|
|
45 |
*/
|
|
|
46 |
public function useTemplate($tpl, $x = 0, $y = 0, $width = null, $height = null, $adjustPageSize = false)
|
|
|
47 |
{
|
|
|
48 |
if (isset($this->importedPages[$tpl])) {
|
|
|
49 |
$size = $this->useImportedPage($tpl, $x, $y, $width, $height, $adjustPageSize);
|
|
|
50 |
if ($this->currentTemplateId !== null) {
|
|
|
51 |
$this->templates[$this->currentTemplateId]['resources']['templates']['importedPages'][$tpl] = $tpl;
|
|
|
52 |
}
|
|
|
53 |
return $size;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
return parent::useTemplate($tpl, $x, $y, $width, $height, $adjustPageSize);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Get the size of an imported page or template.
|
|
|
61 |
*
|
|
|
62 |
* Give only one of the size parameters (width, height) to calculate the other one automatically in view to the
|
|
|
63 |
* aspect ratio.
|
|
|
64 |
*
|
|
|
65 |
* @param mixed $tpl The template id
|
|
|
66 |
* @param float|int|null $width The width.
|
|
|
67 |
* @param float|int|null $height The height.
|
|
|
68 |
* @return array|bool An array with following keys: width, height, 0 (=width), 1 (=height), orientation (L or P)
|
|
|
69 |
*/
|
|
|
70 |
public function getTemplateSize($tpl, $width = null, $height = null)
|
|
|
71 |
{
|
|
|
72 |
$size = parent::getTemplateSize($tpl, $width, $height);
|
|
|
73 |
if ($size === false) {
|
|
|
74 |
return $this->getImportedPageSize($tpl, $width, $height);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
return $size;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* @throws CrossReferenceException
|
|
|
82 |
* @throws PdfParserException
|
|
|
83 |
*/
|
|
|
84 |
protected function _putimages()
|
|
|
85 |
{
|
|
|
86 |
$this->currentReaderId = null;
|
|
|
87 |
parent::_putimages();
|
|
|
88 |
|
|
|
89 |
foreach ($this->importedPages as $key => $pageData) {
|
|
|
90 |
$this->_newobj();
|
|
|
91 |
$this->importedPages[$key]['objectNumber'] = $this->n;
|
|
|
92 |
$this->currentReaderId = $pageData['readerId'];
|
|
|
93 |
$this->writePdfType($pageData['stream']);
|
|
|
94 |
$this->_put('endobj');
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
foreach (\array_keys($this->readers) as $readerId) {
|
|
|
98 |
$parser = $this->getPdfReader($readerId)->getParser();
|
|
|
99 |
$this->currentReaderId = $readerId;
|
|
|
100 |
|
|
|
101 |
while (($objectNumber = \array_pop($this->objectsToCopy[$readerId])) !== null) {
|
|
|
102 |
try {
|
|
|
103 |
$object = $parser->getIndirectObject($objectNumber);
|
|
|
104 |
} catch (CrossReferenceException $e) {
|
|
|
105 |
if ($e->getCode() === CrossReferenceException::OBJECT_NOT_FOUND) {
|
|
|
106 |
$object = PdfIndirectObject::create($objectNumber, 0, new PdfNull());
|
|
|
107 |
} else {
|
|
|
108 |
throw $e;
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
$this->writePdfType($object);
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
$this->currentReaderId = null;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* @inheritdoc
|
|
|
121 |
*/
|
|
|
122 |
protected function _putxobjectdict()
|
|
|
123 |
{
|
|
|
124 |
foreach ($this->importedPages as $pageData) {
|
|
|
125 |
$this->_put('/' . $pageData['id'] . ' ' . $pageData['objectNumber'] . ' 0 R');
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
parent::_putxobjectdict();
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* @param int $n
|
|
|
133 |
* @return void
|
|
|
134 |
* @throws PdfParser\Type\PdfTypeException
|
|
|
135 |
*/
|
|
|
136 |
protected function _putlinks($n)
|
|
|
137 |
{
|
|
|
138 |
foreach ($this->PageLinks[$n] as $pl) {
|
|
|
139 |
$this->_newobj();
|
|
|
140 |
$rect = sprintf('%.2F %.2F %.2F %.2F', $pl[0], $pl[1], $pl[0] + $pl[2], $pl[1] - $pl[3]);
|
|
|
141 |
$this->_put('<</Type /Annot /Subtype /Link /Rect [' . $rect . ']', false);
|
|
|
142 |
if (is_string($pl[4])) {
|
|
|
143 |
$this->_put('/A <</S /URI /URI ' . $this->_textstring($pl[4]) . '>>');
|
|
|
144 |
if (isset($pl['importedLink'])) {
|
|
|
145 |
$values = $pl['importedLink']['pdfObject']->value;
|
|
|
146 |
|
|
|
147 |
foreach ($values as $name => $entry) {
|
|
|
148 |
$this->_put('/' . $name . ' ', false);
|
|
|
149 |
$this->writePdfType($entry);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
if (isset($pl['quadPoints'])) {
|
|
|
153 |
$s = '/QuadPoints[';
|
|
|
154 |
foreach ($pl['quadPoints'] as $value) {
|
|
|
155 |
$s .= sprintf('%.2F ', $value);
|
|
|
156 |
}
|
|
|
157 |
$s .= ']';
|
|
|
158 |
$this->_put($s);
|
|
|
159 |
}
|
|
|
160 |
} else {
|
|
|
161 |
$this->_put('/Border [0 0 0]', false);
|
|
|
162 |
}
|
|
|
163 |
$this->_put('>>');
|
|
|
164 |
} else {
|
|
|
165 |
$this->_put('/Border [0 0 0] ', false);
|
|
|
166 |
$l = $this->links[$pl[4]];
|
|
|
167 |
if (isset($this->PageInfo[$l[0]]['size'])) {
|
|
|
168 |
$h = $this->PageInfo[$l[0]]['size'][1];
|
|
|
169 |
} else {
|
|
|
170 |
$h = ($this->DefOrientation === 'P')
|
|
|
171 |
? $this->DefPageSize[1] * $this->k
|
|
|
172 |
: $this->DefPageSize[0] * $this->k;
|
|
|
173 |
}
|
|
|
174 |
$this->_put(sprintf(
|
|
|
175 |
'/Dest [%d 0 R /XYZ 0 %.2F null]>>',
|
|
|
176 |
$this->PageInfo[$l[0]]['n'],
|
|
|
177 |
$h - $l[1] * $this->k
|
|
|
178 |
));
|
|
|
179 |
}
|
|
|
180 |
$this->_put('endobj');
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
protected function _put($s, $newLine = true)
|
|
|
185 |
{
|
|
|
186 |
if ($newLine) {
|
|
|
187 |
$this->buffer .= $s . "\n";
|
|
|
188 |
} else {
|
|
|
189 |
$this->buffer .= $s;
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
}
|