Updated ACL help file, minor styling changes (#1590)
[openemr.git] / vendor / setasign / fpdi / fpdi_bridge.php
blob8283876b1710399990bc88ee61db83c9d7c446d9
1 <?php
2 /**
3 * This file is part of FPDI
5 * @package FPDI
6 * @copyright Copyright (c) 2017 Setasign - Jan Slabon (https://www.setasign.com)
7 * @license http://opensource.org/licenses/mit-license The MIT License
8 * @version 1.6.2
9 */
11 /**
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)) {
20 /**
21 * Class fpdi_bridge
23 class fpdi_bridge extends FPDF
25 // empty body
28 } else {
30 /**
31 * Class fpdi_bridge
33 class fpdi_bridge extends TCPDF
35 /**
36 * Array of Tpl-Data
38 * @var array
40 protected $_tpls = array();
42 /**
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";
49 /**
50 * Current Object Id.
52 * @var integer
54 protected $_currentObjId;
56 /**
57 * Return XObjects Dictionary.
59 * Overwritten to add additional XObjects to the resources dictionary of TCPDF
61 * @return string
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']);
70 return $out;
73 /**
74 * Writes a PDF value to the resulting document.
76 * Prepares the value for encryption of imported data by FPDI
78 * @param array $value
80 protected function _prepareValue(&$value)
82 switch ($value[0]) {
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]);
89 break;
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,
96 strlen($value[2][1])
99 break;
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]);
109 break;
114 * Un-escapes a PDF string
116 * @param string $s
117 * @return string
119 protected function _unescape($s)
121 $out = '';
122 for ($count = 0, $n = strlen($s); $count < $n; $count++) {
123 if ($s[$count] != '\\' || $count == $n-1) {
124 $out .= $s[$count];
125 } else {
126 switch ($s[++$count]) {
127 case ')':
128 case '(':
129 case '\\':
130 $out .= $s[$count];
131 break;
132 case 'f':
133 $out .= chr(0x0C);
134 break;
135 case 'b':
136 $out .= chr(0x08);
137 break;
138 case 't':
139 $out .= chr(0x09);
140 break;
141 case 'r':
142 $out .= chr(0x0D);
143 break;
144 case 'n':
145 $out .= chr(0x0A);
146 break;
147 case "\r":
148 if ($count != $n-1 && $s[$count+1] == "\n")
149 $count++;
150 break;
151 case "\n":
152 break;
153 default:
154 // Octal-Values
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));
170 } else {
171 $out .= $s[$count];
176 return $out;
180 * Hexadecimal to string
182 * @param string $data
183 * @return string
185 public function hex2str($data)
187 $data = preg_replace('/[^0-9A-Fa-f]/', '', rtrim($data, '>'));
188 if ((strlen($data) % 2) == 1) {
189 $data .= '0';
192 return pack('H*', $data);
196 * String to hexadecimal
198 * @param string $str
199 * @return string
201 public function str2hex($str)
203 return current(unpack('H*', $str));