Author: rande Date: 2010-04-16 01:52:16 +0200 (Fri, 16 Apr 2010) New Revision: 29172
Added: plugins/swBaseApplicationPlugin/trunk/lib/base/ plugins/swBaseApplicationPlugin/trunk/lib/base/BaseswWkhtmlPdf.class.php plugins/swBaseApplicationPlugin/trunk/lib/filter/ plugins/swBaseApplicationPlugin/trunk/lib/filter/swWkhtmlToPdfFilter.class.php plugins/swBaseApplicationPlugin/trunk/lib/pdf/ plugins/swBaseApplicationPlugin/trunk/lib/pdf/swWkhtmlPdf.class.php plugins/swBaseApplicationPlugin/trunk/templates/sw_base_application_layout.pdf.php Modified: plugins/swBaseApplicationPlugin/trunk/README plugins/swBaseApplicationPlugin/trunk/templates/sw_base_application_layout.php Log: [swBaseApplicationPlugin] add pdf generation of the fly Modified: plugins/swBaseApplicationPlugin/trunk/README =================================================================== --- plugins/swBaseApplicationPlugin/trunk/README 2010-04-15 22:54:26 UTC (rev 29171) +++ plugins/swBaseApplicationPlugin/trunk/README 2010-04-15 23:52:16 UTC (rev 29172) @@ -8,7 +8,69 @@ - a theme - a menu manager via the event dispatche +## PDF output +The plugin has a PDF generator filter to handle sf_format with pdf as a value. In order +to make it work fine you need to create a layout.pdf.php file and create a showSuccess.pdf.php. + +To get a better understanding, you should check : + + * [wkhtmltopdf project](http://code.google.com/p/wkhtmltopdf) + * [jobeet and sf_format](http://www.symfony-project.org/jobeet/1_4/Doctrine/en/15) + * [iphone and sf_format](http://www.symfony-project.org/blog/2008/06/09/how-to-create-an-optimized-version-of-your-website-for-the-iphone-in-symfony-1-1) + +### how to set up + + * install wkhtmltopdf => http://code.google.com/p/wkhtmltopdf + + * edit your app.yml file and make sure wkhtmltopdf can be started by your webserver + + [yml] + swToolbox: + wkhtml: + command: /opt/local/bin/wkhtmltopdf --page-size A4 + + * edit your filters.yml file and add the swPdfFilter reference + + [yml] + rendering: ~ + security: ~ + swPdfFilter: + enabled: true + class: swWkhtmlToPdfFilter + + * add custom stylesheet by adding a special event listener + + [php] + class backendConfiguration extends sfApplicationConfiguration + { + public function configure() + { + + } + + public function initialize() + { + $this->dispatcher->connect('wkhtml.initialize', array($this, 'initializedPdfAssets')); + } + + public function initializedPdfAssets(sfEvent $event) + { + $context = $event->getSubject()->getContext(); + $context->getResponse()->addStylesheet('/css/pdf.css', 'last'); + } + } + + * now you can create a link like this : + + [php] + echo link_to('PDF version', 'show_order', array('sf_subject' => $order, 'sf_format' => 'pdf'); + // => http://yourecommerce.com/order/show/0907190001.pdf + +### WARNING + +This solution creates PDF file on the fly, so this can not be suitable on heavy load webserver. + ## Write a menu manager Added: plugins/swBaseApplicationPlugin/trunk/lib/base/BaseswWkhtmlPdf.class.php =================================================================== --- plugins/swBaseApplicationPlugin/trunk/lib/base/BaseswWkhtmlPdf.class.php (rev 0) +++ plugins/swBaseApplicationPlugin/trunk/lib/base/BaseswWkhtmlPdf.class.php 2010-04-15 23:52:16 UTC (rev 29172) @@ -0,0 +1,92 @@ +<?php + +class BaseswWkhtmlPdf +{ + protected + $content = null, + $host = null, + $is_secure = false, + $options = array(); + + + public function __construct($options = array()) + { + $this->options = $options; + + $this->host = isset($options['host']) ? $options['host'] : ''; + $this->is_secure = isset($options['is_secure']) ? $options['is_secure'] : false; + } + + public function setContent($content) + { + $this->content = $content; + } + + public function convert() + { + $content = $this->appendHostInformation($this->content); + + $basename = tempnam(sys_get_temp_dir(), 'sw_wkhtml_'); + $input = $basename.'.html'; + $output = $basename.'.pdf'; + + file_put_contents($input, $content); + + $config = sfConfig::get('app_swToolbox_wkhtml', array('command' => 'wkhtmltopdf')); + + $cmd = sprintf('%s %s %s', + $config['command'], + $input, + $output + ); + + $pipes = array(); + $proc = proc_open($cmd, array( + 0 => array('pipe','r'), + 1 => array('pipe','w'), + 2 => array('pipe','w') + ), $pipes); + + fwrite($pipes[0], $input); + fclose($pipes[0]); + + $stdout = stream_get_contents($pipes[1]); + fclose($pipes[1]); + + $stderr = stream_get_contents($pipes[2]); + fclose($pipes[2]); + + $rtn = proc_close($proc); + + return $output; + } + + public function appendHostInformation($content) + { + $pattern = '/(href|src)=(\'|")([^"\']+)(\'|")/'; + + $content = preg_replace_callback($pattern, array($this, 'callbackAppendHostInformation'), $content); + + return $content; + } + + public function callbackAppendHostInformation($matches) + { + // nothing to do + if(substr($matches[3], 0, 4) == 'http') + { + + return; + } + + $uri = substr($matches[3], 0, 1) == '/' ? substr($matches[3], 1) : $matches[3]; + $protocol = $this->is_secure ? 'https' : 'http'; + + return sprintf('%s=%s%s%s', + $matches[1], + $matches[2], + $protocol.'://'.$this->host.'/'.$uri, + $matches[4] + ); + } +} \ No newline at end of file Added: plugins/swBaseApplicationPlugin/trunk/lib/filter/swWkhtmlToPdfFilter.class.php =================================================================== --- plugins/swBaseApplicationPlugin/trunk/lib/filter/swWkhtmlToPdfFilter.class.php (rev 0) +++ plugins/swBaseApplicationPlugin/trunk/lib/filter/swWkhtmlToPdfFilter.class.php 2010-04-15 23:52:16 UTC (rev 29172) @@ -0,0 +1,40 @@ +<?php + + +class swWkhtmlToPdfFilter extends sfFilter +{ + /** + * Executes this filter. + * + * @param sfFilterChain $filterChain A sfFilterChain instance + */ + public function execute($filterChain) + { + // transform the PDF version of the content into PDF + $sf_format = $this->context->getRequest()->getParameter('force_format', $this->context->getRequest()->getParameter('sf_format')); + + if($sf_format == 'pdf') + { + // you can use this event to setup specific asset for the pdf output + $this->context->getEventDispatcher()->notify(new sfEvent($this, 'wkhtml.initialize')); + } + + // execute the filter chain + $filterChain->execute(); + + if($sf_format == 'pdf') + { + + $driver = new swWkhtmlPdf(array( + 'host' => $this->context->getRequest()->getHost(), + 'is_secure' => $this->context->getRequest()->isSecure() + )); + + $driver->setContent($this->context->getResponse()->getContent()); + $pdf_file = $driver->convert(); + + $this->context->getResponse()->setContentType('application/pdf'); + $this->context->getResponse()->setContent(file_get_contents($pdf_file)); + } + } +} \ No newline at end of file Added: plugins/swBaseApplicationPlugin/trunk/lib/pdf/swWkhtmlPdf.class.php =================================================================== --- plugins/swBaseApplicationPlugin/trunk/lib/pdf/swWkhtmlPdf.class.php (rev 0) +++ plugins/swBaseApplicationPlugin/trunk/lib/pdf/swWkhtmlPdf.class.php 2010-04-15 23:52:16 UTC (rev 29172) @@ -0,0 +1,7 @@ +<?php + + +class swWkhtmlPdf extends BaseswWkhtmlPdf +{ + +} \ No newline at end of file Added: plugins/swBaseApplicationPlugin/trunk/templates/sw_base_application_layout.pdf.php =================================================================== --- plugins/swBaseApplicationPlugin/trunk/templates/sw_base_application_layout.pdf.php (rev 0) +++ plugins/swBaseApplicationPlugin/trunk/templates/sw_base_application_layout.pdf.php 2010-04-15 23:52:16 UTC (rev 29172) @@ -0,0 +1,20 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml"> +<head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <?php include_javascripts() ?> + <?php include_stylesheets() ?> + <?php include_title() ?> +</head> + +<body> + <div id="wrapper"> + <h1 class="logo"><span><?php echo link_to('homepage', '@homepage') ?></span></h1> + + <div id="sw-base-container-column"> + <?php echo $sf_content ?> + </div> + </div> +</body> +</html> + Modified: plugins/swBaseApplicationPlugin/trunk/templates/sw_base_application_layout.php =================================================================== --- plugins/swBaseApplicationPlugin/trunk/templates/sw_base_application_layout.php 2010-04-15 22:54:26 UTC (rev 29171) +++ plugins/swBaseApplicationPlugin/trunk/templates/sw_base_application_layout.php 2010-04-15 23:52:16 UTC (rev 29172) @@ -12,6 +12,9 @@ <?php include_http_metas() ?> <?php include_metas() ?> + <?php include_stylesheets() ?> + <?php include_javascripts() ?> + <?php include_title() ?> </head> -- You received this message because you are subscribed to the Google Groups "symfony SVN" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/symfony-svn?hl=en.
