Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 0cf63721280e4c325528f59350530685d925fe91 @greut committed May 12, 2011
@@ -0,0 +1 @@
+*.sw[op]
@@ -0,0 +1,31 @@
+<?php
+
+use \lithium\core\Libraries;
+
+if (!defined('DOMPDF_LIBRARY_PATH')) {
+ define('DOMPDF_LIBRARY_PATH', LITHIUM_LIBRARY_PATH . '/dompdf/dompdf');
+}
+
+define('DOMPDF_INC_DIR', DOMPDF_LIBRARY_PATH . '/include', true);
+define('DOMPDF_LIB_DIR', DOMPDF_LIBRARY_PATH . '/lib', true);
+
+require DOMPDF_INC_DIR . '/functions.inc.php';
+
+
+$name = 'dompdf';
+$library = Libraries::get($name);
+
+if (empty($library)) {
+ Libraries::add($name, array(
+ 'bootstrap' => false,
+ 'path' => DOMPDF_LIBRARY_PATH
+ ));
+}
+
+
+/**
+ * Configure media to output PDF
+ */
+//include __DIR__ . '/bootstrap/media.php';
+
+?>
@@ -0,0 +1,19 @@
+<?php
+
+use lithium\net\http\Media;
+
+use li3_pdf\extensions\Pdf;
+
+Media::type('pdf', 'application/pdf', array(
+ 'view' => 'lithium\\template\\View',
+ 'encode' => function($data, $handler, &$response) {
+ $htmlhandler = array(
+ 'encode' => false
+ ) + $handler;
+ $view = Media::view($htmlhandler, $handler['data'], $response);
+ $html = $view->render('all', $handler['data'], $handler);
+
+ $pdf = Pdf::adapter('default');
+ return $pdf->generate($html);
+ }
+));
@@ -0,0 +1,28 @@
+<?php
+
+namespace li3_pdf\extensions;
+
+/**
+ * PDF configuration object
+ *
+ * {{{
+ * use li3_pdf\extensions\Pdf;
+ * // the default values
+ * Pdf::config(array('default' => array(
+ * 'adapter' => 'Wkhtmltopdf'
+ * ));
+ * }}}
+ *
+ * Usage:
+ *
+ * {{{
+ * $pdf = Pdf::get('default');
+ *
+ * header('Content-Type: application/pdf');
+ * echo $pdf->generate($html);
+ * }}}
+ */
+class Pdf extends \lithium\core\Adaptable
+{
+ protected static $_adapters = 'adapter.pdf';
+}
@@ -0,0 +1,71 @@
+<?php
+
+namespace li3_pdf\extensions;
+
+/**
+ * This object is intended to produce PDF without being into a Controller
+ * but is still in a work in progress state.
+ */
+abstract class Writer extends \lithium\core\Object
+{
+ protected $_config = 'default';
+
+ protected $_classes = array(
+ 'view' => 'lithium\\template\\View',
+ 'message' => 'lithium\\g11n\\Message'
+ );
+
+ protected $_autoConfig = array('classes' => 'merge', 'config');
+
+ /**
+ * Request data
+ */
+ public $data = null;
+
+ function render(array $options=array())
+ {
+ $class = get_class($this);
+ $name = preg_replace('/Pdf$/', '', substr($class, strrpos($class, '\\') + 1));
+ $name = strtolower($name);
+
+ if (isset($options['data'])) {
+ $this->data = $options['data'];
+ unset($options['data']);
+ } else {
+ $this->data = $options;
+ $options = array();
+ }
+
+ $data = $this->data;
+
+ $options += array(
+ 'controller' => $name,
+ 'template' => 'index',
+ 'layout' => 'default',
+ 'type' => 'pdf'
+ );
+
+ return $this->_render($data, $options);
+ }
+
+ protected function _render(array $data, array $options=array()) {
+ $pdf = Pdf::adapter($this->_config);
+ $view = $this->_view();
+ $html = $view->render('all', $data, $options);
+ return $pdf->generate($html);
+ }
+
+ protected function _view()
+ {
+ $view = $this->_classes['view'];
+ $message = $this->_classes['message'];
+
+ return new $view(array(
+ 'paths' => array(
+ 'template' => '{:library}/views/{:controller}/{:template}.{:type}.php',
+ 'layout' => '{:library}/views/layouts/{:layout}.{:type}.php'
+ ),
+ 'outputFilters' => $message::aliases()
+ ));
+ }
+}
@@ -0,0 +1,111 @@
+<?php
+
+namespace li3_pdf\extensions\adapter\pdf;
+
+class Dompdf extends \lithium\core\Object
+{
+ protected $_backend = 'CPDF';
+ protected $_pdflib;
+ protected $_unicode = true;
+ protected $_media = 'screen';
+ protected $_paper = 'paper';
+ protected $_orientation = 'portrait';
+ protected $_dpi = 96;
+ protected $_font = 'serif';
+ protected $_font_height_ratio = 1.1;
+ protected $_php = false;
+ protected $_css_float = true;
+ protected $_javascript = true;
+ protected $_remote = false;
+ protected $_warnings = false;
+ protected $_debug = false;
+
+ // Singleton (must be bad)
+ protected static $_dompdf;
+
+ protected $_autoConfig = array(
+ 'backend',
+ 'pdflib',
+ 'unicode',
+ 'media',
+ 'paper',
+ 'orientation',
+ 'dpi',
+ 'font',
+ 'font_height_ratio',
+ 'php',
+ 'css_float',
+ 'javascript',
+ 'remote',
+ 'warnings',
+ 'debug',
+ );
+
+ static function auto_load($class) {
+ $filename = DOMPDF_INC_DIR.'/'.strtolower($class).'.cls.php';
+ if (is_file($filename)) {
+ require_once($filename);
+ }
+ }
+
+ public function _init() {
+ parent::_init();
+
+ if (!isset(static::$_dompdf)) {
+ // boiler plate
+ define('DOMPDF_FONT_DIR', DOMPDF_LIB_DIR . '/fonts/', true);
+ define('DOMPDF_FONT_CACHE', DOMPDF_FONT_DIR, true);
+ define('DOMPDF_TEMP_DIR', sys_get_temp_dir(), true);
+ define('DOMPDF_CHROOT', dirname(LITHIUM_APP_PATH), true);
+ define('DOMPDF_UNICODE_ENABLED', $this->_unicode, true);
+
+ define('DOMPDF_PDF_BACKEND', $this->_backend, true);
+ if (isset($this->_pdflib)) {
+ define('DOMPDF_PDFLIB_LICENSE', $this->_pdflib, true);
+ }
+ define('DOMPDF_DEFAULT_MEDIA_TYPE', $this->_media, true);
+ define('DOMPDF_DEFAULT_PAPER_SIZE', $this->_paper, true);
+ define('DOMPDF_DEFAULT_FONT', $this->_font, true);
+ define('DOMPDF_FONT_HEIGHT_RATIO', $this->_font_height_ratio, true);
+ define('DOMPDF_DPI', $this->_dpi, true);
+ define('DOMPDF_ENABLE_PHP', $this->_php, true);
+ define('DOMPDF_ENABLE_REMOTE', $this->_remote, true);
+ define('DOMPDF_ENABLE_CSS_FLOAT', $this->_css_float, true);
+ define('DOMPDF_ENABLE_JAVASCRIPT', $this->_javascript, true);
+
+ spl_autoload_register(__NAMESPACE__.'\Dompdf::auto_load');
+
+ global $_dompdf_warnings;
+ $_dompdf_warnings = array();
+ global $_dompdf_show_warnings;
+ $_dompdf_show_warnings = $this->_warnings;
+ global $_dompdf_debug;
+ $_dompdf_debug = $this->_debug;
+ global $_DOMPDF_DEBUG_TYPES;
+ $_DOMPDF_DEBUG_TYPES = array();
+
+ define('DEBUGPNG', $this->_debug, true);
+ define('DEBUGKEEPTEMP', $this->_debug, true);
+ define('DEBUGCSS', $this->_debug, true);
+ define('DEBUG_LAYOUT', $this->_debug, true);
+ define('DEBUG_LAYOUT_LINES', $this->_debug, true);
+ define('DEBUG_LAYOUT_BLOCKS', $this->_debug, true);
+ define('DEBUG_LAYOUT_INLINE', $this->_debug, true);
+ define('DEBUG_LAYOUT_PADDINGBOX', $this->_debug, true);
+ define('DOMPDF_LOG_OUTPUT_FILE', DOMPDF_FONT_DIR.'log.htm');
+
+ $dompdf = new \DOMPDF();
+ $dompdf->set_paper($this->_paper, $this->_orientation);
+
+ static::$_dompdf = $dompdf;
+ }
+ }
+
+ function generate($html) {
+ $dompdf = static::$_dompdf;
+
+ $dompdf->load_html($html);
+ $dompdf->render();
+ return $dompdf->output();
+ }
+}
@@ -0,0 +1,118 @@
+<?php
+
+namespace li3_pdf\extensions\adapter\pdf;
+
+class Wkhtmltopdf extends \lithium\core\Object
+{
+ /**
+ * Which command to call
+ *
+ * @var string
+ */
+ protected $_command = 'wkhtmltopdf';
+ /**
+ * Page orientation
+ *
+ * @var string
+ */
+ protected $_orientation;
+ /**
+ * Page size
+ *
+ * @var array
+ */
+ protected $_page_size;
+ /**
+ * margin-top
+ *
+ * @var integer
+ */
+ protected $_margin_top;
+
+ /**
+ * margin-right
+ *
+ * @var integer
+ */
+ protected $_margin_right;
+ /**
+ * margin-Bottom
+ *
+ * @var integer
+ */
+ protected $_margin_bottom;
+ /**
+ * margin-left
+ *
+ * @var integer
+ */
+ protected $_margin_left;
+
+ protected $_autoConfig = array(
+ 'orientation',
+ 'page_size',
+ 'margin_top',
+ 'margin_right',
+ 'margin_bottom',
+ 'margin_left',
+ 'command',
+ );
+
+ function generate($html, $input='') {
+ $filename = LITHIUM_APP_PATH . '/resources/tmp/wk'.time().'.html';
+ file_put_contents($filename, $html);
+
+ $proc = proc_open(
+ $this->command($filename).' -',
+ array(
+ array('pipe', 'r'), // stdin
+ array('pipe', 'w'), // stdout
+ array('pipe', 'w'), // stderr
+ ),
+ $pipes
+ );
+
+ fwrite($pipes[0], $input);
+ fclose($pipes[0]);
+
+ $result = array(
+ 'stdout' => stream_get_contents($pipes[1]),
+ 'stderr' => stream_get_contents($pipes[2]),
+ );
+ fclose($pipes[1]);
+ fclose($pipes[2]);
+
+ $result['return'] = proc_close($proc);
+ unlink($filename);
+
+ if ($result['return'] == 0) {
+ return $result['stdout'];
+ } else {
+ throw new \Exception($result['stderr']);
+ }
+ }
+
+ function command($filename) {
+ $opts = array(
+ 'orientation' => '--orientation',
+ 'page_size' => '--page-size',
+ 'margin_top' => '-T',
+ 'margin_right' => '-R',
+ 'margin_bottom' => '-B',
+ 'margin_left' => '-L',
+ );
+ $cmd = $this->_command;
+
+ foreach ($opts as $config => $opt) {
+ if (!is_null($this->{"_$config"})) {
+ $cmd .= ' '.$opt.' '.$this->{"_$config"};
+ }
+ }
+
+ if ($filename) {
+ $cmd .= ' "'.addslashes($filename).'"';
+ }
+
+ return $cmd;
+ }
+}
Oops, something went wrong.

0 comments on commit 0cf6372

Please sign in to comment.