3 * This file is part of FPDI
6 * @copyright Copyright (c) 2017 Setasign - Jan Slabon (https://www.setasign.com)
7 * @license http://opensource.org/licenses/mit-license The MIT License
12 * This file is used as a bridge between TCPDF or FPDF
13 * It will dynamically create the class extending the available
14 * class FPDF or TCPDF.
16 * This way it is possible to use FPDI for both FPDF and TCPDF with one FPDI version.
19 if (!class_exists('TCPDF', false)) {
23 class fpdi_bridge
extends FPDF
33 class fpdi_bridge
extends TCPDF
40 protected $_tpls = array();
43 * Name-prefix of Templates used in Resources-Dictionary
45 * @var string A String defining the Prefix used as Template-Object-Names. Have to begin with an /
47 public $tplPrefix = "/TPL";
54 protected $_currentObjId;
57 * Return XObjects Dictionary.
59 * Overwritten to add additional XObjects to the resources dictionary of TCPDF
63 protected function _getxobjectdict()
65 $out = parent
::_getxobjectdict();
66 foreach ($this->_tpls
as $tplIdx => $tpl) {
67 $out .= sprintf('%s%d %d 0 R', $this->tplPrefix
, $tplIdx, $tpl['n']);
74 * Writes a PDF value to the resulting document.
76 * Prepares the value for encryption of imported data by FPDI
80 protected function _prepareValue(&$value)
83 case pdf_parser
::TYPE_STRING
:
84 if ($this->encrypted
) {
85 $value[1] = $this->_unescape($value[1]);
86 $value[1] = $this->_encrypt_data($this->_currentObjId
, $value[1]);
87 $value[1] = TCPDF_STATIC
::_escape($value[1]);
91 case pdf_parser
::TYPE_STREAM
:
92 if ($this->encrypted
) {
93 $value[2][1] = $this->_encrypt_data($this->_currentObjId
, $value[2][1]);
94 $value[1][1]['/Length'] = array(
95 pdf_parser
::TYPE_NUMERIC
,
101 case pdf_parser
::TYPE_HEX
:
102 if ($this->encrypted
) {
103 $value[1] = $this->hex2str($value[1]);
104 $value[1] = $this->_encrypt_data($this->_currentObjId
, $value[1]);
106 // remake hexstring of encrypted string
107 $value[1] = $this->str2hex($value[1]);
114 * Un-escapes a PDF string
119 protected function _unescape($s)
122 for ($count = 0, $n = strlen($s); $count < $n; $count++
) {
123 if ($s[$count] != '\\' ||
$count == $n-1) {
126 switch ($s[++
$count]) {
148 if ($count != $n-1 && $s[$count+
1] == "\n")
155 if (ord($s[$count]) >= ord('0') &&
156 ord($s[$count]) <= ord('9')) {
157 $oct = ''. $s[$count];
159 if (ord($s[$count+
1]) >= ord('0') &&
160 ord($s[$count+
1]) <= ord('9')) {
161 $oct .= $s[++
$count];
163 if (ord($s[$count+
1]) >= ord('0') &&
164 ord($s[$count+
1]) <= ord('9')) {
165 $oct .= $s[++
$count];
169 $out .= chr(octdec($oct));
180 * Hexadecimal to string
182 * @param string $data
185 public function hex2str($data)
187 $data = preg_replace('/[^0-9A-Fa-f]/', '', rtrim($data, '>'));
188 if ((strlen($data) %
2) == 1) {
192 return pack('H*', $data);
196 * String to hexadecimal
201 public function str2hex($str)
203 return current(unpack('H*', $str));