Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
guenbakku committed Jan 10, 2018
2 parents 9c4c518 + 481c715 commit aa80a4d
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 48 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Cakepdf

Convert HTML to PDF plugin for CakePHP 2.x.
CakePHP plugin for converting HTML to PDF.
This plugin is compatible with CakePHP 2.x and CakePHP 3.x.
This plugin could not be made without awesome library [Snappy](https://github.com/KnpLabs/snappy).
Thankful to the authors of library Snappy.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "guenbakku/cakepdf",
"description": "Convert HTML to PDF plugin for CakePHP 2.x",
"description": "CakePHP plugin for converting HTML to PDF",
"type": "library",
"license": "MIT",
"authors": [
Expand Down
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>

<logging>
<log type="coverage-html" target="./tests/Coverage" lowUpperBound="35"
highLowerBound="70"/>
</logging>

</phpunit>
81 changes: 60 additions & 21 deletions src/Pdf.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Convert HTML to PDF plugin for CakePHP 2.x
* CakePHP plugin for converting HTML to PDF.
* This plugin could not be made without awesome library "Knp\Snappy\Pdf".
* Thankful to the authors of library Snappy.
*
Expand All @@ -9,11 +9,11 @@
*/
namespace Guenbakku\Cakepdf;

use Exception;
use RuntimeException;

class Pdf {

protected $vendor = 'vendors';
// Path to vendor's folder
protected $vendor;

// Snappy object
protected $Snappy;
Expand Down Expand Up @@ -41,22 +41,33 @@ class Pdf {

protected $pageContainer = '<div class="page-container">%s</div>';

public function __construct() {
public function __construct($vendor = array('vendor', 'vendors')) {
$this->_setVendor($vendor);
$this->_setBinary();
$this->Snappy = new \Knp\Snappy\Pdf($this->binary);
}

/**
* Set output path for pdf file
* Set output path of pdf file
*
* @param string: output path
* @return object: this object
*/
public function output($path) {
public function setOutput($path) {
$this->output = $path;
return $this;
}

/**
* Get output path of pdf file
*
* @param void
* @return string
*/
public function getOutput() {
return $this->output;
}

/**
* Add html as seperated pdf page
*
Expand Down Expand Up @@ -100,12 +111,12 @@ public function add($html, $breakPage = false) {
*/
public function render($output = null, $options = array(), $overwrite = false) {
if ($output !== null) {
$this->output($output);
$this->setOutput($output);
}
if (empty($this->output)) {
if (empty($this->getOutput())) {
return $this->Snappy->getOutputFromHtml($this->html, $options);
} else {
return $this->Snappy->generateFromHtml($this->html, $this->output, $options, $overwrite);
return $this->Snappy->generateFromHtml($this->html, $this->getOutput(), $options, $overwrite);
}
}

Expand Down Expand Up @@ -133,16 +144,44 @@ public function __call($name, $arguments) {
* @param string: path of current script
* @return string: path of root
*/
protected function _findRoot($root) {
do {
$lastRoot = $root;
$root = dirname($root);
if (is_dir($root . DIRECTORY_SEPARATOR . $this->vendor)) {
return $root;
protected function _findVendorFullPath() {
$findRoot = function ($vendor) {
$root = __FILE__;
do {
$lastRoot = $root;
$root = dirname($root);
if (is_dir($root . DIRECTORY_SEPARATOR . $vendor)) {
return $root;
}
} while ($root !== $lastRoot);

return null;
};

foreach ($this->vendor as $vendor) {
$root = $findRoot($vendor);
if (!empty($root)) {
return implode(DIRECTORY_SEPARATOR, array(
$root, $vendor
));
}
} while ($root !== $lastRoot);

throw new Exception('Cannot find the root of the application');
}

throw new RuntimeException('Cannot find the root of the application');
}

/**
* Set path to vendor's folder
*
* @param string|array: path(s) to vendor's folder
* @return object: this object
*/
protected function _setVendor($vendor) {
if (!is_array($vendor)) {
$vendor = array($vendor);
}
$this->vendor = $vendor;
return $this;
}

/**
Expand All @@ -161,9 +200,9 @@ protected function _setBinary() {
$binary = 'wkhtmltopdf-amd64';
}

$root = $this->_findRoot(__FILE__);
$vendorFullPath = $this->_findVendorFullPath();
$binary = implode(DIRECTORY_SEPARATOR, array(
$root, $this->vendor, 'bin', $binary
$vendorFullPath, 'bin', $binary
));

if (is_file($binary) || is_link($binary)) {
Expand Down
65 changes: 45 additions & 20 deletions tests/TestCase/PdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
class PdfTest extends TestCase {

public function setUp() {
$this->Pdf = new Pdf();
$this->output = dirname(__FILE__)."/pdfTest.pdf";
if (is_file($this->output)) {
unlink($this->output);
Expand All @@ -20,6 +19,8 @@ public function tearDown() {
}

public function testInstanceSnappy() {
$Pdf = new Pdf();

$is32bit = PHP_INT_SIZE === 4;
if ($is32bit) {
$binary = 'wkhtmltopdf-i386';
Expand All @@ -28,51 +29,75 @@ public function testInstanceSnappy() {
}

$binary = implode(DS, array(
ROOT, 'vendors', 'bin', $binary
VENDOR, 'bin', $binary
));

$Snappy = new Knp\Snappy\Pdf($binary);

$this->assertEquals($Snappy, $this->Pdf->getSnappy());
$this->assertEquals($Snappy, $Pdf->getSnappy());
}

public function testSetVendorManually() {
$Pdf = new Pdf('vendors');
$this->assertEquals(true, $Pdf instanceof Pdf);
}

/**
* @expectedException RuntimeException
*/
public function testNotFoundVendorFullPath() {
$Pdf = new Pdf('xxx');
}

public function testOuput() {
$this->Pdf->add('<p>Test</p>');
$this->Pdf->output($this->output);
$this->Pdf->render();
public function testSetOuput() {
$Pdf = new Pdf();
$Pdf->add('<p>Test</p>');
$Pdf->setOutput($this->output);
$Pdf->render();
$this->assertEquals(true, is_file($this->output));
}

public function testGetOutput() {
$Pdf = new Pdf();
$Pdf->setOutput($this->output);
$this->assertEquals($this->output, $Pdf->getOutput());
}

public function testGenerateFileByAdd() {
$this->Pdf->add('<p>Test</p>');
$this->Pdf->render($this->output);
$Pdf = new Pdf();
$Pdf->add('<p>Test</p>');
$Pdf->render($this->output);
$this->assertEquals(true, is_file($this->output));
}

public function testGenerateFileByAddPage() {
$this->Pdf->add('<p>Test1</p>', true);
$this->Pdf->addPage('<p>Test2</p>');
$this->Pdf->render($this->output);
$Pdf = new Pdf();
$Pdf->add('<p>Test1</p>', true);
$Pdf->addPage('<p>Test2</p>');
$Pdf->render($this->output);
$this->assertEquals(true, is_file($this->output));
}

public function testOutputByAdd() {
$this->Pdf->add('<p>Test</p>');
$result = $this->Pdf->render();
$Pdf = new Pdf();
$Pdf->add('<p>Test</p>');
$result = $Pdf->render();
$this->assertEquals(true, !is_null($result));
}

public function testOutputByAddPage() {
$this->Pdf->addPage('<p>Test1</p>');
$this->Pdf->addPage('<p>Test2</p>');
$result = $this->Pdf->render();
$Pdf = new Pdf();
$Pdf->addPage('<p>Test1</p>');
$Pdf->addPage('<p>Test2</p>');
$result = $Pdf->render();
$this->assertEquals(true, !is_null($result));
}

public function testMagicCall() {
$this->Pdf->setOption('orientation', 'Landscape');
$this->Pdf->add('<p>Test</p>');
$this->Pdf->render($this->output);
$Pdf = new Pdf();
$Pdf->setOption('orientation', 'Landscape');
$Pdf->add('<p>Test</p>');
$Pdf->render($this->output);
$this->assertEquals(true, is_file($this->output));
}
}
21 changes: 16 additions & 5 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,35 @@
* has been installed as a dependency of the plugin, or the plugin is itself
* installed as a dependency of an application.
*/
$findRoot = function ($root) {
$findRoot = function ($root, $vendor) {
do {
$lastRoot = $root;
$root = dirname($root);
if (is_dir($root . '/vendors')) {
if (is_dir($root.'/'.$vendor)) {
return $root;
}
} while ($root !== $lastRoot);

throw new Exception("Cannot find the root of the application, unable to run tests");
return null;
};
$root = $findRoot(__FILE__);

foreach (array('vendor', 'vendors') as $vendor) {
$root = $findRoot(__FILE__, $vendor);
if (!empty($root)) {
break;
}
}
if (empty($root)) {
throw new Exception("Cannot find the root of the application, unable to run tests");
}
unset($findRoot);

chdir($root);
require $root . '/vendors/autoload.php';

if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
define('ROOT', $root);
define('VENDOR', ROOT.DS.$vendor);

require VENDOR.DS.'autoload.php';

0 comments on commit aa80a4d

Please sign in to comment.