You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
6.2KB

  1. <?php
  2. /**
  3. * This file is part of FPDI
  4. *
  5. * @package FPDI
  6. * @copyright Copyright (c) 2015 Setasign - Jan Slabon (http://www.setasign.com)
  7. * @license http://opensource.org/licenses/mit-license The MIT License
  8. * @version 1.6.1
  9. */
  10. /**
  11. * This file is used as a bridge between TCPDF or FPDF
  12. * It will dynamically create the class extending the available
  13. * class FPDF or TCPDF.
  14. *
  15. * This way it is possible to use FPDI for both FPDF and TCPDF with one FPDI version.
  16. */
  17. if (!class_exists('TCPDF', false)) {
  18. /**
  19. * Class fpdi_bridge
  20. */
  21. class fpdi_bridge extends FPDF
  22. {
  23. // empty body
  24. }
  25. } else {
  26. /**
  27. * Class fpdi_bridge
  28. */
  29. class fpdi_bridge extends TCPDF
  30. {
  31. /**
  32. * Array of Tpl-Data
  33. *
  34. * @var array
  35. */
  36. protected $_tpls = array();
  37. /**
  38. * Name-prefix of Templates used in Resources-Dictionary
  39. *
  40. * @var string A String defining the Prefix used as Template-Object-Names. Have to begin with an /
  41. */
  42. public $tplPrefix = "/TPL";
  43. /**
  44. * Current Object Id.
  45. *
  46. * @var integer
  47. */
  48. protected $_currentObjId;
  49. /**
  50. * Return XObjects Dictionary.
  51. *
  52. * Overwritten to add additional XObjects to the resources dictionary of TCPDF
  53. *
  54. * @return string
  55. */
  56. protected function _getxobjectdict()
  57. {
  58. $out = parent::_getxobjectdict();
  59. foreach ($this->_tpls as $tplIdx => $tpl) {
  60. $out .= sprintf('%s%d %d 0 R', $this->tplPrefix, $tplIdx, $tpl['n']);
  61. }
  62. return $out;
  63. }
  64. /**
  65. * Writes a PDF value to the resulting document.
  66. *
  67. * Prepares the value for encryption of imported data by FPDI
  68. *
  69. * @param array $value
  70. */
  71. protected function _prepareValue(&$value)
  72. {
  73. switch ($value[0]) {
  74. case pdf_parser::TYPE_STRING:
  75. if ($this->encrypted) {
  76. $value[1] = $this->_unescape($value[1]);
  77. $value[1] = $this->_encrypt_data($this->_currentObjId, $value[1]);
  78. $value[1] = TCPDF_STATIC::_escape($value[1]);
  79. }
  80. break;
  81. case pdf_parser::TYPE_STREAM:
  82. if ($this->encrypted) {
  83. $value[2][1] = $this->_encrypt_data($this->_currentObjId, $value[2][1]);
  84. $value[1][1]['/Length'] = array(
  85. pdf_parser::TYPE_NUMERIC,
  86. strlen($value[2][1])
  87. );
  88. }
  89. break;
  90. case pdf_parser::TYPE_HEX:
  91. if ($this->encrypted) {
  92. $value[1] = $this->hex2str($value[1]);
  93. $value[1] = $this->_encrypt_data($this->_currentObjId, $value[1]);
  94. // remake hexstring of encrypted string
  95. $value[1] = $this->str2hex($value[1]);
  96. }
  97. break;
  98. }
  99. }
  100. /**
  101. * Un-escapes a PDF string
  102. *
  103. * @param string $s
  104. * @return string
  105. */
  106. protected function _unescape($s)
  107. {
  108. $out = '';
  109. for ($count = 0, $n = strlen($s); $count < $n; $count++) {
  110. if ($s[$count] != '\\' || $count == $n-1) {
  111. $out .= $s[$count];
  112. } else {
  113. switch ($s[++$count]) {
  114. case ')':
  115. case '(':
  116. case '\\':
  117. $out .= $s[$count];
  118. break;
  119. case 'f':
  120. $out .= chr(0x0C);
  121. break;
  122. case 'b':
  123. $out .= chr(0x08);
  124. break;
  125. case 't':
  126. $out .= chr(0x09);
  127. break;
  128. case 'r':
  129. $out .= chr(0x0D);
  130. break;
  131. case 'n':
  132. $out .= chr(0x0A);
  133. break;
  134. case "\r":
  135. if ($count != $n-1 && $s[$count+1] == "\n")
  136. $count++;
  137. break;
  138. case "\n":
  139. break;
  140. default:
  141. // Octal-Values
  142. if (ord($s[$count]) >= ord('0') &&
  143. ord($s[$count]) <= ord('9')) {
  144. $oct = ''. $s[$count];
  145. if (ord($s[$count+1]) >= ord('0') &&
  146. ord($s[$count+1]) <= ord('9')) {
  147. $oct .= $s[++$count];
  148. if (ord($s[$count+1]) >= ord('0') &&
  149. ord($s[$count+1]) <= ord('9')) {
  150. $oct .= $s[++$count];
  151. }
  152. }
  153. $out .= chr(octdec($oct));
  154. } else {
  155. $out .= $s[$count];
  156. }
  157. }
  158. }
  159. }
  160. return $out;
  161. }
  162. /**
  163. * Hexadecimal to string
  164. *
  165. * @param string $data
  166. * @return string
  167. */
  168. public function hex2str($data)
  169. {
  170. $data = preg_replace('/[^0-9A-Fa-f]/', '', rtrim($data, '>'));
  171. if ((strlen($data) % 2) == 1) {
  172. $data .= '0';
  173. }
  174. return pack('H*', $data);
  175. }
  176. /**
  177. * String to hexadecimal
  178. *
  179. * @param string $str
  180. * @return string
  181. */
  182. public function str2hex($str)
  183. {
  184. return current(unpack('H*', $str));
  185. }
  186. }
  187. }