PageRenderTime 36ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/proveedores/XML/fo2pdf.php

https://gitlab.com/marioalvarez/ferrominio
PHP | 339 lines | 120 code | 29 blank | 190 comment | 12 complexity | 20e3da825d90d433c860ebc7fac6483b MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license, |
  8. // | that is bundled with this package in the file LICENSE, and is |
  9. // | available at through the world-wide-web at |
  10. // | http://www.php.net/license/2_02.txt. |
  11. // | If you did not receive a copy of the PHP license and are unable to |
  12. // | obtain it through the world-wide-web, please send a note to |
  13. // | license@php.net so we can mail you a copy immediately. |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Christian Stocker <chregu@phant.ch> |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: fo2pdf.php,v 1.10 2003/03/20 15:32:16 arnaud Exp $
  19. /**
  20. * Required files
  21. */
  22. require_once 'PEAR.php';
  23. /**
  24. * FO to pdf converter.
  25. *
  26. * with fo (formating objects) it's quite easy to convert xml-documents into
  27. * pdf-docs (and not only pdf, but also ps, pcl, txt and more)
  28. *
  29. * see README.fo2pdf for details
  30. *
  31. * @author Christian Stocker <chregu@nomad.ch>
  32. * @version $Id: fo2pdf.php,v 1.10 2003/03/20 15:32:16 arnaud Exp $
  33. * @package XML
  34. */
  35. class XML_fo2pdf
  36. {
  37. /**
  38. * fo-file used in this class
  39. *
  40. * @var string
  41. */
  42. var $fo = '';
  43. /**
  44. * pdf-file used in this class
  45. *
  46. * @var string
  47. */
  48. var $pdf = '';
  49. /**
  50. * Where the temporary fo and pdf files should be stored
  51. *
  52. * @var string
  53. */
  54. var $tmpdir = '/tmp';
  55. /**
  56. * A prefix for the temporary files
  57. *
  58. * @var string
  59. */
  60. var $tmppdfprefix = 'pdffo';
  61. /**
  62. * the render Type. At the moment (fop 0.20.1), possible values are
  63. * - awt
  64. * - mif
  65. * - pcl
  66. * - pdf
  67. * - ps
  68. * - txt
  69. * - xml
  70. *
  71. * @var string
  72. */
  73. var $renderer = 'pdf';
  74. /**
  75. * the content-type to be sent if printPDF is called.
  76. *
  77. * @var contenttype
  78. * @see printPDF()
  79. */
  80. var $contenttype = 'application/pdf';
  81. /**
  82. * If you need more Fonts or have some other stuff, which needs a
  83. * Fop-Configfile, you can assign one
  84. *
  85. * See http://xml.apache.org/fop/fonts.html for Details about
  86. * embedding fonts.
  87. *
  88. * @var configFile
  89. */
  90. var $configFile = null;
  91. /**
  92. * constructor
  93. * ATTENTION (you've been warned!):
  94. * You should not pass the values here, 'cause then you don't have
  95. * Error-Reporting. This variables are only here for Backwards Compatibilty..
  96. * Use $fop->run("input.fo","output.pdf") instead.
  97. *
  98. * @param string $fo file input fo-file (do not use it anymore)
  99. * @param string $pdf file output pdf-file (do not use it anymore)
  100. * @see run(), runFromString(), runFromFile()
  101. * @access public
  102. */
  103. function xml_fo2pdf ($fo = null, $pdf = '')
  104. {
  105. if (!(is_null($fo))) {
  106. $this->run($fo, $pdf);
  107. }
  108. }
  109. /**
  110. * Calls the Main Fop-Java-Programm
  111. *
  112. * One has to pass an input fo-file
  113. * and if the pdf should be stored permanently, a filename/path for
  114. * the pdf.
  115. * if the pdf is not passed or empty/false, a temporary pdf-file
  116. * will be created
  117. *
  118. * @param string $fo file input fo-file
  119. * @param string $pdf file output pdf-file
  120. * @param boolean $DelFo if the fo should be deleted after execution
  121. * @see runFromString()
  122. */
  123. function run($fo, $pdf = '', $DelFo = false)
  124. {
  125. $returnuri = false;
  126. if (!$pdf) {
  127. $pdf = tempnam($this->tmpdir, $this->tmppdfprefix);
  128. $returnuri = true;
  129. }
  130. $this->pdf = $pdf;
  131. $this->fo = $fo;
  132. $options = array();
  133. //$options = array('-d');
  134. if ($this->configFile) {
  135. $options = array('-c', $this->configFile);
  136. //array_push($options, '-c', $this->configFile);
  137. }
  138. array_push($options, $this->fo, '-' . $this->renderer, $this->pdf);
  139. if (!$options = @new Java("org.apache.fop.apps.CommandLineOptions", $options)) {
  140. return PEAR::raiseError('Unable to create Java Virtual Machine in ' . __FILE__ . ':' . __LINE__, 11, PEAR_ERROR_RETURN, null, null);
  141. } // if
  142. $starter = $options->getStarter();
  143. $starter->run();
  144. if ($DelFo) {
  145. $this->deleteFo($fo);
  146. }
  147. if ($returnuri) {
  148. return $pdf;
  149. } else {
  150. return true;
  151. } // if
  152. }
  153. /**
  154. * If the fo is a string, not a file, use this.
  155. *
  156. * If you generate the fo dynamically (for example with a
  157. * xsl-stylesheet), you can use this method
  158. *
  159. * The Fop-Java program needs a file as an input, so a
  160. * temporary fo-file is created here (and will be deleted
  161. * in the run() function.)
  162. *
  163. * @param string $fostring fo input fo-string
  164. * @param string $pdf file output pdf-file
  165. * @see run()
  166. */
  167. function runFromString($fostring, $pdf = '')
  168. {
  169. $fo = tempnam($this->tmpdir, $this->tmppdfprefix);
  170. $fp = fopen($fo, 'w+');
  171. fwrite($fp, $fostring);
  172. fclose($fp);
  173. return $this->run($fo, $pdf, true);
  174. }
  175. /**
  176. * A wrapper to run for better readabilty
  177. *
  178. * This method just calls run....
  179. *
  180. * @param string $fo fo input fo-string
  181. * @param string $pdf file output pdf-file
  182. * @see run()
  183. */
  184. function runFromFile($fo, $pdf = '')
  185. {
  186. return $this->run($fo, $pdf);
  187. }
  188. /**
  189. * Deletes the created pdf
  190. *
  191. * If you dynamically create pdfs and you store them
  192. * for example in a Cache, you don't need it afterwards.
  193. * If no pdf is given, the one generated in run() is deleted
  194. *
  195. * @param string $pdf file output pdf-file
  196. * @access public
  197. */
  198. function deletePDF($pdf = '')
  199. {
  200. if (!$pdf) {
  201. $pdf = $this->pdf;
  202. }
  203. @unlink($pdf);
  204. }
  205. /**
  206. * Deletes the created fo
  207. *
  208. * If you dynamically create fos, you don't need it afterwards.
  209. * If no fo-file is given, the one generated in run() is deleted
  210. *
  211. * @param string $fo file input fo-file
  212. */
  213. function deleteFo($fo = '')
  214. {
  215. if (!$fo) {
  216. $fo = $this->fo;
  217. }
  218. @unlink($fo);
  219. }
  220. /**
  221. * Prints the content header and the generated pdf to the output
  222. *
  223. * If you want to dynamically generate pdfs and return them directly
  224. * to the browser, use this.
  225. * If no pdf-file is given, the generated from run() is taken.
  226. *
  227. * @param string $pdf file output pdf-file
  228. * @see returnPDF()
  229. * @access public
  230. */
  231. function printPDF($pdf = '')
  232. {
  233. $pdf = $this->returnPDF($pdf);
  234. Header('Content-type: ' . $this->contenttype . "\r\nContent-Length: " . strlen($pdf));
  235. print $pdf;
  236. }
  237. /**
  238. * Returns the pdf
  239. *
  240. * If no pdf-file is given, the generated from run() is taken.
  241. *
  242. * @param string $pdf file output pdf-file
  243. * @return string pdf
  244. * @see run()
  245. */
  246. function returnPDF($pdf = '')
  247. {
  248. if (!$pdf) {
  249. $pdf = $this->pdf;
  250. }
  251. $fd = fopen($pdf, "r");
  252. $content = fread( $fd, filesize($pdf) );
  253. fclose($fd);
  254. return $content;
  255. }
  256. /**
  257. * sets the rendertype
  258. *
  259. * @param string $renderer the type of renderer which should be used
  260. * @param string $overwriteContentType if the contentType should be set to a approptiate one
  261. * @see $renderer
  262. * @access public
  263. */
  264. function setRenderer($renderer = 'pdf', $overwriteContentType = true)
  265. {
  266. $this->renderer = $renderer;
  267. if ($overwriteContentType) {
  268. switch ($renderer) {
  269. case 'pdf':
  270. $this->contenttype = 'application/pdf';
  271. break;
  272. case 'ps':
  273. $this->contenttype = 'application/ps';
  274. break;
  275. case 'pcl':
  276. $this->contenttype = 'application/pcl';
  277. break;
  278. case 'txt':
  279. $this->contenttype = 'text/plain';
  280. break;
  281. case 'xml':
  282. $this->contenttype = 'text/xml';
  283. break;
  284. }
  285. }
  286. }
  287. /**
  288. * sets the content-type
  289. *
  290. * @param string $contenttype the content-type for the http-header
  291. * @see $contenttype
  292. * @access public
  293. */
  294. function setContentType($contenttype = 'application/pdf')
  295. {
  296. $this->contenttype = $contenttype;
  297. }
  298. /**
  299. * Sets the configfile-type.
  300. *
  301. * @param string $configFile the config file for fop
  302. * @access public
  303. * @see $configFile
  304. */
  305. function setConfigFile($configFile)
  306. {
  307. $this->configFile = $configFile;
  308. }
  309. }
  310. ?>