Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
giansalex committed Jan 21, 2018
0 parents commit c21155b
Show file tree
Hide file tree
Showing 12 changed files with 1,197 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/tests export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.php_cs.dist export-ignore
.travis.yml export-ignore
LICENSE export-ignore
phpunit.xml export-ignore
README.md export-ignore
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.idea/
/build/
/vendor/
/composer.lock
.DS_Store
.php_cs.cache
wkhtmltopdf.exe
23 changes: 23 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$config = PhpCsFixer\Config::create()
->setRules([
'@Symfony' => true,
'@PSR1' => true,
'@PSR2' => true,
'yoda_style' => false
]);
// special handling of fabbot.io service if it's using too old PHP CS Fixer version
try {
PhpCsFixer\FixerFactory::create()
->registerBuiltInFixers()
->registerCustomFixers($config->getCustomFixers())
->useRuleSet(new PhpCsFixer\RuleSet($config->getRules()));
} catch (PhpCsFixer\ConfigurationException\InvalidConfigurationException $e) {
$config->setRules([]);
} catch (UnexpectedValueException $e) {
$config->setRules([]);
} catch (InvalidArgumentException $e) {
$config->setRules([]);
}
return $config;
22 changes: 22 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
language: php

php:
- 7.0

before_script:
- composer self-update
- composer install --prefer-source --no-interaction

install:
- composer require php-coveralls/php-coveralls
- composer require jakub-onderka/php-parallel-lint

script:
- mkdir -p build/logs
- vendor/bin/parallel-lint --exclude vendor .
- vendor/bin/phpunit --configuration phpunit.xml --coverage-clover build/logs/clover.xml

after_success:
- travis_retry php vendor/bin/php-coveralls -v


21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Giancarlos Salas

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Html to PDF Report - Greenter

[![Travis-CI](https://img.shields.io/travis/giansalex/greenter-htmltopdf.svg?label=build&branch=master&style=flat-square)](https://travis-ci.org/giansalex/greenter-htmltopdf)
[![Coverage Status](https://img.shields.io/coveralls/giansalex/greenter-htmltopdf.svg?label=coveralls&style=flat-square&branch=master)](https://coveralls.io/github/giansalex/greenter-htmltopdf?branch=master)

Greenter HTML Report to PDF using wkhtmltopdf.

# Install
Using composer.

```bash
composer require greenter/htmltopdf
```
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "greenter/htmtopdf",
"description": "Greenter Html Report to Pdf using wkhtmltopdf",
"keywords": ["greenter", "wkhtmltodf", "representacion-impresa", "facturacion electronica"],
"license": "MIT",
"authors": [
{
"name": "Giancarlos Salas",
"email": "giansalex@gmail.com"
}
],
"homepage": "https://github.com/giansalex/greenter-htmltopdf",
"type": "library",
"require": {
"php": ">=5.5.9",
"greenter/core": "^1.2",
"mikehaertl/phpwkhtmltopdf": "^2.3"
},
"require-dev": {
"phpunit/phpunit": ">=4.8 < 6.0"
},
"autoload": {
"psr-4": {
"Greenter\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\Greenter\\": "tests/"
}
}
}
28 changes: 28 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="true">
<testsuites>
<testsuite name="Unit Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false"/>
</logging>
</phpunit>
79 changes: 79 additions & 0 deletions src/Report/PdfReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Created by PhpStorm.
* User: Giansalex
* Date: 20/01/2018
* Time: 18:36.
*/

namespace Greenter\Report;

use Greenter\Model\DocumentInterface;
use mikehaertl\wkhtmlto\Pdf;

/**
* Class PdfReport.
*/
class PdfReport implements ReportInterface
{
/**
* @var ReportInterface
*/
private $htmlReport;

/**
* @var Pdf
*/
private $pdfRender;

/**
* PdfReport constructor.
*
* @param ReportInterface $htmlReport
*/
public function __construct(ReportInterface $htmlReport)
{
$this->htmlReport = $htmlReport;
$this->pdfRender = new Pdf([
'no-outline', // Make Chrome not complain
'viewport-size' => '1280x1024',
]);
}

/**
* @param DocumentInterface $document
* @param array $parameters
*
* @return mixed
*/
public function render(DocumentInterface $document, $parameters = [])
{
$html = $this->htmlReport->render($document, $parameters);

return $this->buildPdf($html);
}

public function setOptions(array $options)
{
$this->pdfRender->setOptions($options);
}

/**
* @param string $path
*/
public function setBinPath($path)
{
if (!is_executable($path)) {
throw new \RuntimeException('file is not executable');
}

$this->pdfRender->binary = $path;
}

private function buildPdf($html)
{
$this->pdfRender->addPage($html);

return $this->pdfRender->toString();
}
}
107 changes: 107 additions & 0 deletions tests/Report/PdfReportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* Created by PhpStorm.
* User: Giansalex
* Date: 20/01/2018
* Time: 18:57
*/

namespace Tests\Greenter\Report;
use Greenter\Model\Sale\Invoice;
use Greenter\Report\PdfReport;
use Greenter\Report\ReportInterface;

/**
* Class PdfReportTest
* @package Tests\Greenter\Report
*/
class PdfReportTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PdfReport
*/
private $pdf;

protected function setUp()
{
$htmlRender = $this->getHtmlRender();
$this->pdf = new PdfReport($htmlRender);
$this->pdf->setOptions([
'page-width' => '21cm',
'page-height' => '29.7cm',
]);
$this->pdf->setBinPath(__DIR__.'/../../wkhtmltopdf.exe');
}

public function testPdfRender()
{
$invoice = $this->getInvoice();

$content = $this->pdf->render($invoice);

$this->assertNotEmpty($content);

if ($this->isWindows()) {
$path = __DIR__.DIRECTORY_SEPARATOR.'report.pdf';
file_put_contents($path, $content);

exec('start '.$path);
}
}

public function testPdfRenderWithBreak()
{
$invoice = $this->getInvoice();

$content = $this->pdf->render($invoice, ['name' => 'invoiceBreak.html']);

$this->assertNotEmpty($content);

if ($this->isWindows()) {
$path = __DIR__.DIRECTORY_SEPARATOR.'report2Pages.pdf';
file_put_contents($path, $content);

exec('start '.$path);
}
}

/**
* @expectedException \RuntimeException
*/
public function testInvalidBinPath()
{
$this->pdf->setBinPath(__DIR__ . '/../Resources/invoice.html');
}

/**
* @return ReportInterface
*/
private function getHtmlRender()
{
$stub = $this->getMockBuilder(ReportInterface::class)
->getMock();

$stub->method('render')
->willReturnCallback(function ($doc, $params) {
$filename = 'invoice.html';
if (isset($params['name'])) {
$filename = $params['name'];
}

return file_get_contents(__DIR__ . '/../Resources/'.$filename);
});

/**@var $stub ReportInterface*/
return $stub;
}

private function getInvoice()
{
return new Invoice();
}

private function isWindows()
{
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}
}
Loading

0 comments on commit c21155b

Please sign in to comment.