| 1 | <?php |
|---|
| 2 | // |
|---|
| 3 | // FPDI - Version 1.4.1 |
|---|
| 4 | // |
|---|
| 5 | // Copyright 2004-2011 Setasign - Jan Slabon |
|---|
| 6 | // |
|---|
| 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 |
|---|
| 10 | // |
|---|
| 11 | // http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 12 | // |
|---|
| 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. |
|---|
| 18 | // |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * This class is used as a bridge between TCPDF and FPDI |
|---|
| 22 | * and will create the possibility to use both FPDF and TCPDF |
|---|
| 23 | * via one FPDI version. |
|---|
| 24 | * |
|---|
| 25 | * We'll simply remap TCPDF to FPDF again. |
|---|
| 26 | * |
|---|
| 27 | * It'll be loaded and extended by FPDF_TPL. |
|---|
| 28 | */ |
|---|
| 29 | class FPDF extends TCPDF { |
|---|
| 30 | |
|---|
| 31 | function _putstream($s) { |
|---|
| 32 | $this->_out($this->_getstream($s)); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | function _getxobjectdict() { |
|---|
| 36 | $out = parent::_getxobjectdict(); |
|---|
| 37 | if (count($this->tpls)) { |
|---|
| 38 | foreach($this->tpls as $tplidx => $tpl) { |
|---|
| 39 | $out .= sprintf('%s%d %d 0 R', $this->tplprefix, $tplidx, $tpl['n']); |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | return $out; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * Encryption of imported data by FPDI |
|---|
| 48 | * |
|---|
| 49 | * @param array $value |
|---|
| 50 | */ |
|---|
| 51 | function pdf_write_value(&$value) { |
|---|
| 52 | switch ($value[0]) { |
|---|
| 53 | case PDF_TYPE_STRING: |
|---|
| 54 | if ($this->encrypted) { |
|---|
| 55 | $value[1] = $this->_unescape($value[1]); |
|---|
| 56 | $value[1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[1]); |
|---|
| 57 | $value[1] = $this->_escape($value[1]); |
|---|
| 58 | } |
|---|
| 59 | break; |
|---|
| 60 | |
|---|
| 61 | case PDF_TYPE_STREAM: |
|---|
| 62 | if ($this->encrypted) { |
|---|
| 63 | $value[2][1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[2][1]); |
|---|
| 64 | } |
|---|
| 65 | break; |
|---|
| 66 | |
|---|
| 67 | case PDF_TYPE_HEX: |
|---|
| 68 | if ($this->encrypted) { |
|---|
| 69 | $value[1] = $this->hex2str($value[1]); |
|---|
| 70 | $value[1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[1]); |
|---|
| 71 | |
|---|
| 72 | // remake hexstring of encrypted string |
|---|
| 73 | $value[1] = $this->str2hex($value[1]); |
|---|
| 74 | } |
|---|
| 75 | break; |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | /** |
|---|
| 80 | * Unescapes a PDF string |
|---|
| 81 | * |
|---|
| 82 | * @param string $s |
|---|
| 83 | * @return string |
|---|
| 84 | */ |
|---|
| 85 | function _unescape($s) { |
|---|
| 86 | $out = ''; |
|---|
| 87 | for ($count = 0, $n = strlen($s); $count < $n; $count++) { |
|---|
| 88 | if ($s[$count] != '\\' || $count == $n-1) { |
|---|
| 89 | $out .= $s[$count]; |
|---|
| 90 | } else { |
|---|
| 91 | switch ($s[++$count]) { |
|---|
| 92 | case ')': |
|---|
| 93 | case '(': |
|---|
| 94 | case '\\': |
|---|
| 95 | $out .= $s[$count]; |
|---|
| 96 | break; |
|---|
| 97 | case 'f': |
|---|
| 98 | $out .= chr(0x0C); |
|---|
| 99 | break; |
|---|
| 100 | case 'b': |
|---|
| 101 | $out .= chr(0x08); |
|---|
| 102 | break; |
|---|
| 103 | case 't': |
|---|
| 104 | $out .= chr(0x09); |
|---|
| 105 | break; |
|---|
| 106 | case 'r': |
|---|
| 107 | $out .= chr(0x0D); |
|---|
| 108 | break; |
|---|
| 109 | case 'n': |
|---|
| 110 | $out .= chr(0x0A); |
|---|
| 111 | break; |
|---|
| 112 | case "\r": |
|---|
| 113 | if ($count != $n-1 && $s[$count+1] == "\n") |
|---|
| 114 | $count++; |
|---|
| 115 | break; |
|---|
| 116 | case "\n": |
|---|
| 117 | break; |
|---|
| 118 | default: |
|---|
| 119 | // Octal-Values |
|---|
| 120 | if (ord($s[$count]) >= ord('0') && |
|---|
| 121 | ord($s[$count]) <= ord('9')) { |
|---|
| 122 | $oct = ''. $s[$count]; |
|---|
| 123 | |
|---|
| 124 | if (ord($s[$count+1]) >= ord('0') && |
|---|
| 125 | ord($s[$count+1]) <= ord('9')) { |
|---|
| 126 | $oct .= $s[++$count]; |
|---|
| 127 | |
|---|
| 128 | if (ord($s[$count+1]) >= ord('0') && |
|---|
| 129 | ord($s[$count+1]) <= ord('9')) { |
|---|
| 130 | $oct .= $s[++$count]; |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | $out .= chr(octdec($oct)); |
|---|
| 135 | } else { |
|---|
| 136 | $out .= $s[$count]; |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | return $out; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | /** |
|---|
| 145 | * Hexadecimal to string |
|---|
| 146 | * |
|---|
| 147 | * @param string $hex |
|---|
| 148 | * @return string |
|---|
| 149 | */ |
|---|
| 150 | function hex2str($hex) { |
|---|
| 151 | return pack('H*', str_replace(array("\r", "\n", ' '), '', $hex)); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | * String to hexadecimal |
|---|
| 156 | * |
|---|
| 157 | * @param string $str |
|---|
| 158 | * @return string |
|---|
| 159 | */ |
|---|
| 160 | function str2hex($str) { |
|---|
| 161 | return current(unpack('H*', $str)); |
|---|
| 162 | } |
|---|
| 163 | } |
|---|