5 // Copyright 2004-2014 Setasign - Jan Slabon
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
21 * This file is used as a bridge between TCPDF or FPDF
22 * It will dynamically create the class extending the available
23 * class FPDF or TCPDF.
25 * This way it is possible to use FPDI for both FPDF and TCPDF with one FPDI version.
28 if (!class_exists('TCPDF', false)) {
32 class fpdi_bridge
extends FPDF
42 class fpdi_bridge
extends TCPDF
49 protected $_tpls = array();
52 * Name-prefix of Templates used in Resources-Dictionary
54 * @var string A String defining the Prefix used as Template-Object-Names. Have to begin with an /
56 public $tplPrefix = "/TPL";
63 protected $_currentObjId;
66 * Return XObjects Dictionary.
68 * Overwritten to add additional XObjects to the resources dictionary of TCPDF
72 protected function _getxobjectdict()
74 $out = parent
::_getxobjectdict();
75 foreach ($this->_tpls
as $tplIdx => $tpl) {
76 $out .= sprintf('%s%d %d 0 R', $this->tplPrefix
, $tplIdx, $tpl['n']);
83 * Writes a PDF value to the resulting document.
85 * Prepares the value for encryption of imported data by FPDI
89 protected function _prepareValue(&$value)
92 case pdf_parser
::TYPE_STRING
:
93 if ($this->encrypted
) {
94 $value[1] = $this->_unescape($value[1]);
95 $value[1] = $this->_encrypt_data($this->_currentObjId
, $value[1]);
96 $value[1] = TCPDF_STATIC
::_escape($value[1]);
100 case pdf_parser
::TYPE_STREAM
:
101 if ($this->encrypted
) {
102 $value[2][1] = $this->_encrypt_data($this->_currentObjId
, $value[2][1]);
103 $value[1][1]['/Length'] = array(
104 pdf_parser
::TYPE_NUMERIC
,
110 case pdf_parser
::TYPE_HEX
:
111 if ($this->encrypted
) {
112 $value[1] = $this->hex2str($value[1]);
113 $value[1] = $this->_encrypt_data($this->_currentObjId
, $value[1]);
115 // remake hexstring of encrypted string
116 $value[1] = $this->str2hex($value[1]);
123 * Un-escapes a PDF string
128 protected function _unescape($s)
131 for ($count = 0, $n = strlen($s); $count < $n; $count++
) {
132 if ($s[$count] != '\\' ||
$count == $n-1) {
135 switch ($s[++
$count]) {
157 if ($count != $n-1 && $s[$count+
1] == "\n")
164 if (ord($s[$count]) >= ord('0') &&
165 ord($s[$count]) <= ord('9')) {
166 $oct = ''. $s[$count];
168 if (ord($s[$count+
1]) >= ord('0') &&
169 ord($s[$count+
1]) <= ord('9')) {
170 $oct .= $s[++
$count];
172 if (ord($s[$count+
1]) >= ord('0') &&
173 ord($s[$count+
1]) <= ord('9')) {
174 $oct .= $s[++
$count];
178 $out .= chr(octdec($oct));
189 * Hexadecimal to string
191 * @param string $data
194 public function hex2str($data)
196 $data = preg_replace('/[^0-9A-Fa-f]/', '', rtrim($data, '>'));
197 if ((strlen($data) %
2) == 1) {
201 return pack('H*', $data);
205 * String to hexadecimal
210 public function str2hex($str)
212 return current(unpack('H*', $str));