Skip to content

Commit

Permalink
Merge pull request #4 from jellehenkens/experimental
Browse files Browse the repository at this point in the history
Improved configuration and linking to engines
  • Loading branch information
ceeram committed May 10, 2012
2 parents 66d8e38 + 74bac40 commit b0aec3b
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 14 deletions.
229 changes: 225 additions & 4 deletions Lib/Pdf/CakePdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,70 @@ class CakePdf {
*/
protected $_engineClass = null;

/**
* Html to be rendered
*
* @var string
*/
protected $_html = null;

/**
* Page size of the pdf
*
* @var string
*/
protected $_pageSize = 'A4';

/**
* Orientation of the pdf
*
* @var string
*/
protected $_orientation = 'portrait';

/**
* Bottom margin in mm
*
* @var number
*/
protected $_marginBottom = null;

/**
* Left margin in mm
*
* @var number
*/
protected $_marginLeft = null;

/**
* Right margin in mm
*
* @var number
*/
protected $_marginRight = null;

/**
* Top margin in mm
*
* @var number
*/
protected $_marginTop = null;

/**
* Constructor
*
* @param array $config Pdf configs to use
*/
public function __construct($config = array()) {
$config = array_merge(array('engine' => null), $config);
$config = array_merge(array('engine' => Configure::read('Pdf.engine')), $config);
$this->engine($config['engine'])->config($config);

$options = array('pageSize', 'orientation', 'margin');
foreach($options as $option) {
if(isset($config[$option])) {
$this->{$option}($config[$option]);
}
}
}

/**
Expand All @@ -73,10 +129,175 @@ public function output($html = null) {
if (!isset($this->_engineClass)) {
throw new Exception(__d('cakepdf', 'No Pdf engine is set!'));
}
if (!$html) {
$html = $this->_render();

if ($html) {
$this->html($html);
} else {
$this->html($this->_render());
}

return $this->engine()->output($this);
}

/**
* Get/Set Html.
*
* @param null|string $html
* @return mixed
*/
public function html($html = null) {
if ($html === null) {
return $this->_html;
}
$this->_html = $html;
return $this;
}

/**
* Get/Set Page size.
*
* @param null|string $pageSize
* @return mixed
*/
public function pageSize($pageSize = null) {
if ($pageSize === null) {
return $this->_pageSize;
}
$this->_pageSize = $pageSize;
return $this;
}

/**
* Get/Set Orientation.
*
* @param null|string $orientation
* @return mixed
*/
public function orientation($orientation = null) {
if ($orientation === null) {
return $this->_orientation;
}
$this->_orientation = $orientation;
return $this;
}

/**
* Get/Set page margins.
*
* Several options are available
*
* Array format
* ------------
* First param can be an array with the following options:
* - bottom
* - left
* - right
* - top
*
* Set margin for all borders
* --------------------------
* $bottom is set to a string
* Leave all other parameters empty
*
* Set margin for horizontal and vertical
* --------------------------------------
* $bottom value will be set to bottom and top
* $left value will be set to left and right
*
* @param null|string|array $bottom
* @param null|string $left
* @param null|string $right
* @param null|string $top
* @return mixed
*/
public function margin($bottom = null, $left = null, $right = null, $top = null) {
if ($bottom === null) {
return array(
'bottom' => $this->_marginBottom,
'left' => $this->_marginLeft,
'right' => $this->_marginRight,
'top' => $this->_marginTop
);
}

if (is_array($bottom)) {
extract($bottom, EXTR_IF_EXISTS);
}

if($bottom && $left === null && $right === null && $top === null) {
$left = $right = $top = $bottom;
}

if($bottom && $top === null) {
$top = $bottom;
}

if($left && $right === null) {
$right = $left;
}

$this->marginBottom($bottom);
$this->marginLeft($left);
$this->marginRight($right);
$this->marginTop($top);

return $this;
}

/**
* Get/Set bottom margin.
*
* @param null|string $margin
* @return mixed
*/
public function marginBottom($margin = null) {
if ($margin === null) {
return $this->_marginBottom;
}
$this->_marginBottom = $margin;
return $this;
}

/**
* Get/Set left margin.
*
* @param null|string $margin
* @return mixed
*/
public function marginLeft($margin = null) {
if ($margin === null) {
return $this->_marginLeft;
}
$this->_marginLeft = $margin;
return $this;
}

/**
* Get/Set right margin.
*
* @param null|string $margin
* @return mixed
*/
public function marginRight($margin = null) {
if ($margin === null) {
return $this->_marginRight;
}
$this->_marginRight = $margin;
return $this;
}

/**
* Get/Set bottom margin.
*
* @param null|string $margin
* @return mixed
*/
public function marginTop($margin = null) {
if ($margin === null) {
return $this->_marginTop;
}
return $this->engine()->output($html);
$this->_marginTop = $margin;
return $this;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Lib/Pdf/Engine/AbstractPdfEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
protected $_config = array();

abstract public function output($html);
abstract public function output(CakePdf $pdf);

/**
* Set the config
Expand Down
7 changes: 4 additions & 3 deletions Lib/Pdf/Engine/DomPdfEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ public function __construct() {
App::import('Vendor', 'CakePdf.DomPDF', array('file' => 'dompdf' . DS . 'dompdf_config.inc.php'));
}

public function output($html) {
$DomPDF = new DOMPDF('A4');
$DomPDF->load_html($html);
public function output(CakePdf $pdf) {
$DomPDF = new DOMPDF();
$DomPDF->set_paper($pdf->pageSize(), $pdf->orientation());
$DomPDF->load_html($pdf->html());
$DomPDF->render();
return $DomPDF->output();
}
Expand Down
4 changes: 2 additions & 2 deletions Lib/Pdf/Engine/MpdfEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ public function __construct() {
App::import('Vendor', 'CakePdf.Mpdf', array('file' => 'mpdf' . DS . 'mpdf.php'));
}

public function output($html) {
public function output(CakePdf $pdf) {
//mPDF often produces a whole bunch of errors, although there is a pdf created when debug = 0
//Configure::write('debug', 0);
$pdf = new mPDF();
$pdf->writeHTML($html);
$pdf->writeHTML($pdf->html());
return $pdf->Output('', 'S');
}

Expand Down
17 changes: 13 additions & 4 deletions Lib/Pdf/Engine/WkHtmlToPdfEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ class WkHtmlToPdfEngine extends AbstractPdfEngine {
*/
protected $binary = '/usr/bin/wkhtmltopdf';


/**
* Instance of CakePdf class
*
* @var CakePdf
*/
protected $_pdf = null;

/**
* @brief the default options for WkHtmlToPdf View class
*
Expand All @@ -34,9 +42,10 @@ public function __construct() {
}
}

public function output($html) {
public function output(CakePdf $pdf) {
$this->_pdf = $pdf;

return $this->_renderPdf($html);
return $this->_renderPdf($pdf->html());
}

/**
Expand Down Expand Up @@ -101,8 +110,8 @@ private function __exec($cmd, $input) {
private function __getCommand() {
$command = $this->binary;

$command .= " --orientation " . $this->options['orientation'];
$command .= " --page-size " . $this->options['pageSize'];
$command .= " --orientation " . $this->_pdf->orientation();
$command .= " --page-size " . $this->_pdf->pageSize();

$command .= " - -";

Expand Down
72 changes: 72 additions & 0 deletions Test/Case/Lib/CakePdfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

App::uses('CakePdf', 'CakePdf.Lib/Pdf');

/**
* CakePdfTest class
*
* @package CakePdf.Test.Case.Lib.CakePdf
*/
class CakePdfTest extends CakeTestCase {

public function testMargin() {
$pdf = new CakePdf();
$pdf->margin(15, 20, 25, 30);
$expected = array(
'bottom' => 15,
'left' => 20,
'right' => 25,
'top' => 30
);
$this->assertEqual($expected, $pdf->margin());

$pdf = new CakePdf();
$pdf->margin(75);
$expected = array(
'bottom' => 75,
'left' => 75,
'right' => 75,
'top' => 75
);
$this->assertEqual($expected, $pdf->margin());

$pdf = new CakePdf();
$pdf->margin(20, 50);
$expected = array(
'bottom' => 20,
'left' => 50,
'right' => 50,
'top' => 20
);
$this->assertEqual($expected, $pdf->margin());

$pdf = new CakePdf();
$pdf->margin(array('left' => 120, 'right' => 30, 'top' => 34, 'bottom' => 15));
$expected = array(
'bottom' => 15,
'left' => 120,
'right' => 30,
'top' => 34
);
$this->assertEqual($expected, $pdf->margin());
}

public function testMarginFromConfig() {
$config = array(
'margin' => array(
'bottom' => 15,
'left' => 50,
'right' => 30,
'top' => 45
)
);
$pdf = new CakePdf($config);
$expected = array(
'bottom' => 15,
'left' => 50,
'right' => 30,
'top' => 45
);
$this->assertEqual($expected, $pdf->margin());
}
}

0 comments on commit b0aec3b

Please sign in to comment.