PageRenderTime 624ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/includes/fpdfi/fpdi2tcpdf_bridge.php

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