|
From: <gem...@li...> - 2012-07-19 13:04:16
|
Revision: 859
http://gemstracker.svn.sourceforge.net/gemstracker/?rev=859&view=rev
Author: michieltcs
Date: 2012-07-19 13:04:06 +0000 (Thu, 19 Jul 2012)
Log Message:
-----------
Refactoring: move html to pdf conversion to Gems_Pdf
Modified Paths:
--------------
trunk/library/classes/Gems/Export/RespondentExport.php
trunk/library/classes/Gems/Pdf.php
Modified: trunk/library/classes/Gems/Export/RespondentExport.php
===================================================================
--- trunk/library/classes/Gems/Export/RespondentExport.php 2012-07-19 11:12:13 UTC (rev 858)
+++ trunk/library/classes/Gems/Export/RespondentExport.php 2012-07-19 13:04:06 UTC (rev 859)
@@ -43,8 +43,6 @@
*/
class Gems_Export_RespondentExport extends Gems_Registry_TargetAbstract
{
- public $_pdfExportCommand = "";
-
protected $_reportFooter = 'Export_ReportFooterSnippet';
protected $_reportHeader = 'Export_ReportHeaderSnippet';
protected $_respondentSnippet = 'Export_RespondentSnippet';
@@ -84,58 +82,22 @@
public $util;
public $view;
+
+ /**
+ * @var Gems_Pdf
+ */
+ protected $_pdf;
- public function _($messageid, $locale = null)
- {
- return $this->translate->_($messageid, $locale);
- }
-
public function afterRegistry()
{
parent::afterRegistry();
-
- // Load the pdf class from the project settings if available
- if (isset($this->project->export) && isset($this->project->export['pdfExportCommand'])) {
- $this->_pdfExportCommand = $this->project->export['pdfExportCommand'];
- }
+
+ $this->_pdf = $this->loader->getPdf();
}
-
- /**
- * Calls 'wkhtmltopdf' to convert HTML to PDF
- *
- * @param string $content The HTML source
- * @return string The converted PDF file
- * @throws Exception
- */
- protected function _convertToPdf($content)
+
+ public function _($messageid, $locale = null)
{
- $tempInputFilename = GEMS_ROOT_DIR . '/var/tmp/export-' . md5(time() . rand()) . '.html';
- $tempOutputFilename = GEMS_ROOT_DIR . '/var/tmp/export-' . md5(time() . rand()) . '.pdf';
-
- file_put_contents($tempInputFilename, $content);
-
- if (!file_exists($tempInputFilename)) {
- throw new Exception("Unable to create temporary file '{$tempInputFilename}'");
- }
-
- $command = sprintf($this->_pdfExportCommand, escapeshellarg($tempInputFilename),
- escapeshellarg($tempOutputFilename));
-
- $lastLine = exec($command, $outputLines, $return);
-
- if ($return > 0) {
- @unlink($tempInputFilename);
- @unlink($tempOutputFilename);
-
- throw new Exception(sprintf($this->_('Unable to run PDF conversion (%s): "%s"'), $command, $lastLine));
- }
-
- $pdfContents = file_get_contents($tempOutputFilename);
-
- @unlink($tempInputFilename);
- @unlink($tempOutputFilename);
-
- return $pdfContents;
+ return $this->translate->_($messageid, $locale);
}
/**
@@ -328,7 +290,7 @@
$element = new Zend_Form_Element_Select('format');
$element->setLabel($this->_('Output format'));
$outputFormats = array('html' => 'HTML');
- if (!empty($this->_pdfExportCommand)) {
+ if ($this->_pdf->hasPdfExport()) {
$outputFormats['pdf'] = 'PDF';
$element->setValue('pdf');
}
@@ -386,14 +348,9 @@
$content = $this->view->layout->render();
if ($this->_format == 'pdf') {
- $content = $this->_convertToPdf($content);
$filename = 'respondent-export-' . strtolower($respondentId) . '.pdf';
-
- header('Content-Type: application/x-download');
- header('Content-Length: '.strlen($content));
- header('Content-Disposition: inline; filename="'.$filename.'"');
- header('Cache-Control: private, max-age=0, must-revalidate');
- header('Pragma: public');
+ $content = $this->_pdf->convertFromHtml($content);
+ $this->_pdf->echoPdfContent($content, $filename, true);
}
echo $content;
Modified: trunk/library/classes/Gems/Pdf.php
===================================================================
--- trunk/library/classes/Gems/Pdf.php 2012-07-19 11:12:13 UTC (rev 858)
+++ trunk/library/classes/Gems/Pdf.php 2012-07-19 13:04:06 UTC (rev 859)
@@ -52,6 +52,12 @@
*/
protected $db;
+ /**
+ *
+ * @var string
+ */
+ protected $_pdfExportCommand = "";
+
protected $pageFont = Zend_Pdf_Font::FONT_COURIER;
protected $pageFontSize = 12;
protected $pageX = 10;
@@ -71,6 +77,16 @@
*/
protected $translate;
+ public function afterRegistry()
+ {
+ parent::afterRegistry();
+
+ // Load the pdf class from the project settings if available
+ if (isset($this->project->export) && isset($this->project->export['pdfExportCommand'])) {
+ $this->_pdfExportCommand = $this->project->export['pdfExportCommand'];
+ }
+ }
+
protected function addTokenToDocument(Zend_Pdf $pdf, $tokenId, $surveyId)
{
$token = strtoupper($tokenId);
@@ -123,7 +139,17 @@
protected function echoPdf(Zend_Pdf $pdf, $filename, $download = false, $exit = true)
{
$content = $pdf->render();
+
+ $this->echoPdfContent($content, $filename, $download);
+ if ($exit) {
+ // No further output
+ exit;
+ }
+ }
+
+ public function echoPdfContent($content, $filename, $download = false)
+ {
// MUtil_Echo::track($filename);
if ($download) {
// Download & save
@@ -138,11 +164,6 @@
header('Pragma: public');
echo $content;
-
- if ($exit) {
- // No further output
- exit;
- }
}
/**
@@ -284,4 +305,50 @@
}
throw new Gems_Exception_Coding($msg);
}
+
+ /**
+ * Calls the PDF convertor (wkhtmltopdf / phantom.js) to convert HTML to PDF
+ *
+ * @param string $content The HTML source
+ * @return string The converted PDF file
+ * @throws Exception
+ */
+ public function convertFromHtml($content)
+ {
+ $tempInputFilename = GEMS_ROOT_DIR . '/var/tmp/export-' . md5(time() . rand()) . '.html';
+ $tempOutputFilename = GEMS_ROOT_DIR . '/var/tmp/export-' . md5(time() . rand()) . '.pdf';
+
+ file_put_contents($tempInputFilename, $content);
+
+ if (!file_exists($tempInputFilename)) {
+ throw new Exception("Unable to create temporary file '{$tempInputFilename}'");
+ }
+
+ $command = sprintf($this->_pdfExportCommand, escapeshellarg($tempInputFilename),
+ escapeshellarg($tempOutputFilename));
+
+ $lastLine = exec($command, $outputLines, $return);
+
+ if ($return > 0) {
+ @unlink($tempInputFilename);
+ @unlink($tempOutputFilename);
+
+ throw new Exception(sprintf($this->_('Unable to run PDF conversion (%s): "%s"'), $command, $lastLine));
+ }
+
+ $pdfContents = file_get_contents($tempOutputFilename);
+
+ @unlink($tempInputFilename);
+ @unlink($tempOutputFilename);
+
+ return $pdfContents;
+ }
+
+ /**
+ * @return boolean
+ */
+ public function hasPdfExport()
+ {
+ return !empty($this->_pdfExportCommand);
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|