From c21155ba0300d112653838a4b3266d2a88ccaddf Mon Sep 17 00:00:00 2001 From: Giancarlos Salas Date: Sat, 20 Jan 2018 19:39:13 -0500 Subject: [PATCH] Init commit --- .gitattributes | 8 + .gitignore | 7 + .php_cs.dist | 23 ++ .travis.yml | 22 ++ LICENSE | 21 ++ README.md | 13 + composer.json | 32 +++ phpunit.xml | 28 ++ src/Report/PdfReport.php | 79 ++++++ tests/Report/PdfReportTest.php | 107 ++++++++ tests/Resources/invoice.html | 418 ++++++++++++++++++++++++++++ tests/Resources/invoiceBreak.html | 439 ++++++++++++++++++++++++++++++ 12 files changed, 1197 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .php_cs.dist create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpunit.xml create mode 100644 src/Report/PdfReport.php create mode 100644 tests/Report/PdfReportTest.php create mode 100644 tests/Resources/invoice.html create mode 100644 tests/Resources/invoiceBreak.html diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..20ea03b --- /dev/null +++ b/.gitattributes @@ -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 \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..71019ad --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +/.idea/ +/build/ +/vendor/ +/composer.lock +.DS_Store +.php_cs.cache +wkhtmltopdf.exe \ No newline at end of file diff --git a/.php_cs.dist b/.php_cs.dist new file mode 100644 index 0000000..0dfd690 --- /dev/null +++ b/.php_cs.dist @@ -0,0 +1,23 @@ +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; \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..089c60a --- /dev/null +++ b/.travis.yml @@ -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 + + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..68fde1a --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..f77056a --- /dev/null +++ b/README.md @@ -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 +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..7897ade --- /dev/null +++ b/composer.json @@ -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/" + } + } +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..f338fc6 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,28 @@ + + + + + tests + + + + + src + + ./tests + ./vendor + + + + + + + + \ No newline at end of file diff --git a/src/Report/PdfReport.php b/src/Report/PdfReport.php new file mode 100644 index 0000000..559e339 --- /dev/null +++ b/src/Report/PdfReport.php @@ -0,0 +1,79 @@ +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(); + } +} diff --git a/tests/Report/PdfReportTest.php b/tests/Report/PdfReportTest.php new file mode 100644 index 0000000..9a25cc7 --- /dev/null +++ b/tests/Report/PdfReportTest.php @@ -0,0 +1,107 @@ +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'; + } +} \ No newline at end of file diff --git a/tests/Resources/invoice.html b/tests/Resources/invoice.html new file mode 100644 index 0000000..832a67e --- /dev/null +++ b/tests/Resources/invoice.html @@ -0,0 +1,418 @@ + + + + + + + + + + +
+ + + + + + + + + +
+ + +
+ + + + + + + + + + + + + + + + +
+ FACTURA +
+ E L E C T R Ó N I C A +
+   +
+ R.U.C.: 20123456789 +
+ No.: F001-123 +
+ Nro. R.I. Emisor: 212321 +
+
+
+
+ + + + + + + + + + +
+ EMPRESA S.A.C +
+ Dirección: AV ITALIA 232 - LIMA - LIMA - PERU +
+ Telf: (056) 123375 +
+
+
+
+ + + + + + + + + + + + + +
+ Razón Social: +  EMPRESA 1 + + RUC: +     + 20000000001 +
Fecha Emisión:     21/01/2018Dirección:     + AV ITALIA 23423 +
Tipo Moneda:     SOLES +
+

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CantidadCódigoDescripciónValor UnitarioValor Total
+ 2.00 + NIU + + C023 + + PRODUCTO 1
+
+ S/ + 50.00 + + S/ + 100.00 +
+ 2.00 + ZZ + + C21 + + PRODUCTO 2
+
+ S/ + 10.00 + + S/ + 100.00 +
+ 2.00 + NIU + + C023 + + PRODUCTO 1
+
+ S/ + 50.00 + + S/ + 100.00 +
+ 2.00 + NIU + + C023 + + PRODUCTO 1
+
+ S/ + 50.00 + + S/ + 100.00 +
+ 2.00 + NIU + + C023 + + PRODUCTO 1
+
+ S/ + 50.00 + + S/ + 100.00 +
+ 2.00 + NIU + + C023 + + PRODUCTO 1
+
+ S/ + 50.00 + + S/ + 100.00 +
+ 2.00 + NIU + + C023 + + PRODUCTO 1
+
+ S/ + 50.00 + + S/ + 100.00 +
+ + + + + +
+ + + + + + +
+
+
+ SON DOSCIENTOS TREINTA Y SEIS CON 00/100 SOLES. +
+
+ Información Adicional +
+ + + + + + + + + + + + + + + + +
+ LEYENDA: + + +
+ CONDICION DE PAGO: + + Efectivo +
+ VENDEDOR: + + GITHUB SELLER +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Op. Gravadas:S/ 200.00
Op. Inafectas:S/ 0.00
Op. Exoneradas:S/ 0.00
IGV:S/ 36.00
ISC:S/ 2.00
Precio Venta:S/ 236
Otros Cargos:S/ 12.00
Otros Tributos:S/ 1.00
+
+
+
+
+
+ + + + +
+
+
+
+

"FACTURA SUJETA A DETRACCIÓN"

+

CUENTAS DEL BCP:

+

+ + + Cuenta Dólares:194-1742647-1-03 + + + +

+

+ + CCI: $ BCP:002-194-001742647103-97 + + +

+

+ +   + +

+

+ + + Cuenta Soles:194-1735919-0-33 + + + +

+

+ CCI: S/ BCP:002-194-001735919033-91 + + + +

+

+ BANCO DE LA NACION: + + + + + +

+

+ CTA DE DETRACCIONES EN SOLES: + + 00068125014 + + +

+ Agradecemos enviar su comprobante de pago alcorreo: cobranzas-pe@bumeran.com
+
+

+ Consulte su documento electrónico en : http://bumeran.stupendo.pe +
+ Representación Impresa de la FACTURA ELECTRÓNICA. +
+
+ Qr Image +
+
+
+ \ No newline at end of file diff --git a/tests/Resources/invoiceBreak.html b/tests/Resources/invoiceBreak.html new file mode 100644 index 0000000..f49d739 --- /dev/null +++ b/tests/Resources/invoiceBreak.html @@ -0,0 +1,439 @@ + + + + + + + + + + +
+ + + + + + + + + +
+ + +
+ + + + + + + + + + + + + + + + +
+ FACTURA +
+ E L E C T R Ó N I C A +
+   +
+ R.U.C.: 20123456789 +
+ No.: F001-123 +
+ Nro. R.I. Emisor: 212321 +
+
+
+
+ + + + + + + + + + +
+ EMPRESA S.A.C +
+ Dirección: AV ITALIA 232 - LIMA - LIMA - PERU +
+ Telf: (056) 123375 +
+
+
+
+ + + + + + + + + + + + + +
+ Razón Social: +  EMPRESA 1 + + RUC: +     + 20000000001 +
Fecha Emisión:     20/01/2018Dirección:     + AV ITALIA 23423 +
Tipo Moneda:     SOLES +
+

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CantidadCódigoDescripciónValor UnitarioValor Total
+ 2.00 + NIU + + C023 + + PRODUCTO 1
+
+ S/ + 50.00 + + S/ + 100.00 +
+ 2.00 + ZZ + + C21 + + PRODUCTO 2
+
+ S/ + 10.00 + + S/ + 100.00 +
+ 2.00 + NIU + + C023 + + PRODUCTO 1
+
+ S/ + 50.00 + + S/ + 100.00 +
+ 2.00 + NIU + + C023 + + PRODUCTO 1
+
+ S/ + 50.00 + + S/ + 100.00 +
+ 2.00 + NIU + + C023 + + PRODUCTO 1
+
+ S/ + 50.00 + + S/ + 100.00 +
+ 2.00 + NIU + + C023 + + PRODUCTO 1
+
+ S/ + 50.00 + + S/ + 100.00 +
+ 2.00 + NIU + + C023 + + PRODUCTO 1
+
+ S/ + 50.00 + + S/ + 100.00 +
+ 2.00 + NIU + + C023 + + PRODUCTO 1
+
+ S/ + 50.00 + + S/ + 100.00 +
+ + + + + +
+ + + + + + +
+
+
+ SON DOSCIENTOS TREINTA Y SEIS CON 00/100 SOLES. +
+
+ Información Adicional +
+ + + + + + + + + + + + + + + + +
+ LEYENDA: + + +
+ CONDICION DE PAGO: + + Efectivo +
+ VENDEDOR: + + GITHUB SELLER +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Op. Gravadas:S/ 200.00
Op. Inafectas:S/ 0.00
Op. Exoneradas:S/ 0.00
IGV:S/ 36.00
ISC:S/ 2.00
Precio Venta:S/ 236
Otros Cargos:S/ 12.00
Otros Tributos:S/ 1.00
+
+
+
+
+
+
+ + + + +
+
+
+
+

"FACTURA SUJETA A DETRACCIÓN"

+

CUENTAS DEL BCP:

+

+ + + Cuenta Dólares:194-1742647-1-03 + + + +

+

+ + CCI: $ BCP:002-194-001742647103-97 + + +

+

+ +   + +

+

+ + + Cuenta Soles:194-1735919-0-33 + + + +

+

+ CCI: S/ BCP:002-194-001735919033-91 + + + +

+

+ BANCO DE LA NACION: + + + + + +

+

+ CTA DE DETRACCIONES EN SOLES: + + 00068125014 + + +

+ Agradecemos enviar su comprobante de pago alcorreo: cobranzas-pe@bumeran.com
+
+

+ Consulte su documento electrónico en : http://bumeran.stupendo.pe +
+ Representación Impresa de la FACTURA ELECTRÓNICA. +
+
+ Qr Image +
+
+
+ \ No newline at end of file