PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/DocBlox/Transformer/Writer/Pdf.php

https://github.com/androa/Docblox
PHP | 86 lines | 32 code | 9 blank | 45 comment | 7 complexity | 8c3a3eb608d23f31427b6b98e7d6ec87 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * DocBlox
  4. *
  5. * PHP Version 5
  6. *
  7. * @category DocBlox
  8. * @package Transformer
  9. * @subpackage Writers
  10. * @author Mike van Riel <mike.vanriel@naenius.com>
  11. * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT
  13. * @link http://docblox-project.org
  14. */
  15. /**
  16. * A PDF writer which uses wkhtmltopdf to convert a single HTML file to PDF.
  17. *
  18. * It is advised to have your own version of wkhtmltopdf installed on the machine where DocBlox runs.
  19. * wkhtmltopdf relies on the presence of xserver (not necessarily running; in case of linux) to invoke webkit to
  20. * generate the PDF.
  21. *
  22. * @category DocBlox
  23. * @package Transformer
  24. * @subpackage Writers
  25. * @author Mike van Riel <mike.vanriel@naenius.com>
  26. * @license http://www.opensource.org/licenses/mit-license.php MIT
  27. * @link http://docblox-project.org
  28. * @link http://code.google.com/p/wkhtmltopdf/
  29. * @link http://blog.structuralartistry.com/post/2327213260/installing-wkhtmltopdf-on-ubuntu-server
  30. */
  31. class DocBlox_Transformer_Writer_Pdf extends DocBlox_Transformer_Writer_Abstract
  32. {
  33. /**
  34. * Calls the wkhtmltopdf executable to generate a PDF.
  35. *
  36. * @param DOMDocument $structure
  37. * @param DocBlox_Transformer_Transformation $transformation
  38. *
  39. * @return void
  40. */
  41. public function transform(DOMDocument $structure, DocBlox_Transformer_Transformation $transformation)
  42. {
  43. $artifact = $transformation->getTransformer()->getTarget() . DIRECTORY_SEPARATOR . $transformation->getArtifact();
  44. $transformation->setArtifact($artifact);
  45. $source = substr($transformation->getSource(), 0, 1) != DIRECTORY_SEPARATOR
  46. ? $transformation->getTransformer()->getTarget() . DIRECTORY_SEPARATOR . $transformation->getSource()
  47. : $transformation->getSource();
  48. $transformation->setSource($source);
  49. $options = '';
  50. if ($transformation->getParameter('toc', 'false') == 'true')
  51. {
  52. $options = ' toc ';
  53. }
  54. // TODO: add parameter to provide a cover HTML
  55. // TODO: add a parameter to provide a header HTML
  56. // TODO: add a parameter to provide a footer HTML
  57. // first try if there is a wkhtmltopdf in the global path, this helps windows users
  58. exec('wkhtmltopdf ' . $options . ' ' . $transformation->getSource() . ' ' . $transformation->getArtifact() . ' 2>&1', $output, $error);
  59. $output = implode(PHP_EOL, $output);
  60. // this notice is linux specific; if it is found no global wkhtmltopdf was installed; try the one which is included
  61. // with docblox
  62. if (strpos($output, 'wkhtmltopdf: not found') !== false)
  63. {
  64. exec($this->getConfig()->paths->application . '/src/wkhtmltopdf/wkhtmltopdf-i386 ' . $options . ' '
  65. . $transformation->getSource() . ' ' . $transformation->getArtifact() . ' 2>&1', $output, $error);
  66. $output = implode(PHP_EOL, $output).PHP_EOL;
  67. }
  68. // log message and output
  69. $this->log('Generating PDF file ' . $transformation->getArtifact() . ' from ' . $transformation->getSource());
  70. $this->log($output, $error == 0 ? DocBlox_Core_Log::INFO : DocBlox_Core_Log::CRIT);
  71. // CRASH!
  72. if ($error != 0)
  73. {
  74. throw new Exception('Conversion to PDF failed, see output for details');
  75. }
  76. }
  77. }