commit 26942cdbde44c89c8067bb1f4ad979cb16768bb5 Author: Christopher Vagnetoft Date: Sat Dec 30 00:49:06 2017 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fbb073 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor/ +/composer.lock diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ae8ff5f --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "noccylabs/pdf-bundle", + "description": "Generate PDFs in Symfony", + "type": "bundle", + "require": { + "dompdf/dompdf": "^0.8.1" + }, + "license": "GPL-3.0", + "authors": [ + { + "name": "Christopher Vagnetoft", + "email": "cvagnetoft@gmail.com" + } + ], + "repositories": [ + { + "type": "composer", + "url": "https://packages.noccylabs.info/composer/" + } + ], + "autoload": { + "psr-4": { + "NoccyLabs\\Bundle\\PdfBundle\\": "src/" + } + } +} diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php new file mode 100644 index 0000000..2823f38 --- /dev/null +++ b/src/DependencyInjection/Configuration.php @@ -0,0 +1,29 @@ +root('noccy_labs_pdf_converter'); + + // Here you should define the parameters that are allowed to + // configure your bundle. See the documentation linked above for + // more information on that topic. + + return $treeBuilder; + } +} diff --git a/src/DependencyInjection/NoccyLabsPdfExtension.php b/src/DependencyInjection/NoccyLabsPdfExtension.php new file mode 100644 index 0000000..6205941 --- /dev/null +++ b/src/DependencyInjection/NoccyLabsPdfExtension.php @@ -0,0 +1,28 @@ +processConfiguration($configuration, $configs); + + $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.yml'); + } +} diff --git a/src/NoccyLabsPdfBundle.php b/src/NoccyLabsPdfBundle.php new file mode 100644 index 0000000..b647260 --- /dev/null +++ b/src/NoccyLabsPdfBundle.php @@ -0,0 +1,9 @@ +tempDir = $tempDir; + } + + public function generate($html, array $options=[]) + { + $pdf = new Dompdf(); + $pdf->loadHtml($html); + $pdf->render(); + + $output = $pdf->output(); + + return $output; + + } + + public function generateResponseFromHtml($html, array $options=[]) + { + $tempFile = $this->tempDir . "/" . "pdf_" . md5(uniqid()) . ".pdf"; + + // Call on generate with the options provided + $output = $this->generate($html, $options); + file_put_contents($tempFile, $output); + + // Prepare the response + $response = new BinaryFileResponse($tempFile); + if (array_key_exists('filename', $options)) { + $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $options['filename']); + } else { + $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE); + } + $response->deleteFileAfterSend(true); + + return $response; + } +} \ No newline at end of file diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml new file mode 100644 index 0000000..36ba21f --- /dev/null +++ b/src/Resources/config/services.yml @@ -0,0 +1,14 @@ +services: + _defaults: + autowire: true + autoconfigure: true + + NoccyLabs\Bundle\PdfBundle\Controller\DefaultController: + public: true + calls: + - [ setGenerator, [ "@noccylabs.pdf.generator" ]] + + noccylabs.pdf.generator: + class: NoccyLabs\Bundle\PdfBundle\Pdf\PdfGenerator + arguments: [ "%kernel.cache_dir%" ] + public: true diff --git a/src/Resources/views/Example/example.html.twig b/src/Resources/views/Example/example.html.twig new file mode 100644 index 0000000..b9ef539 --- /dev/null +++ b/src/Resources/views/Example/example.html.twig @@ -0,0 +1,164 @@ +{% extends "NoccyLabsPdfBundle:Pdf:document.html.twig" %} +{% import "NoccyLabsPdfBundle:Pdf:document.html.twig" as page %} +{% import _self as helpers %} + +{% block footer %} + + + + + + +
+
Postal address:
+
Some Company Name
1234 Address St.
12345 Somewhere
+
+
Postal address:
+
Some Company Name
1234 Address St.
12345 Somewhere
+
+
Postal address:
+
Some Company Name
1234 Address St.
12345 Somewhere
+
+{% endblock %} + +{% block styles %} + body { + font-family: sans-serif; + font-size: 9pt; + } + .small { + font-size: 7pt; + } + table.full-width { + width: 100%; + } + table.bordered { + border: solid 1px #666; + border-collapse: collapse; + } + table.bordered td, + table.bordered th { + padding: 3px; + vertical-align: top; + } + table.bordered thead { + background: #cde; + border-bottom: solid 1px #666; + } + .page-footer { + border-top: solid 1px #000; + padding-top: 5px; + } + .monospaced { + font-family: monospace; + } + .align-right { + text-align: right; + } +{% endblock %} + +{% block body %} + +
+
Client No.
123456
+
Date
2017-12-01
+
Third
+
+
Page
1 (2)
+
+
+
+ + + + {{ helpers.thead() }} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
12345Abcd
multi-line!
199.99
12345Abcd
multi-line!
199.99
 
 
 
 
 
 
 
 
 
 
+ + {{ page.break() }} + +
+
 
+
Page
2 (2)
+
+ + + {{ helpers.thead }} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
12345Abcd
multi-line!
199.99
12345Abcd
multi-line!
199.99
 
 
 
 
 
 
 
 
 
 
+ + + +{% endblock %} + +{% macro thead() %} + + + SKU + Description + Price + + +{% endmacro %} \ No newline at end of file diff --git a/src/Resources/views/Pdf/document.html.twig b/src/Resources/views/Pdf/document.html.twig new file mode 100644 index 0000000..f69565f --- /dev/null +++ b/src/Resources/views/Pdf/document.html.twig @@ -0,0 +1,99 @@ +{# + # This is the base document template for PdfBundle. You should construct your + # views by extending from this template. + # + # (c) 2017, NoccyLabs. Licensed under GPL v3 + #} + +{# -- break macro -- #} +{% macro break() %} +
+ {% if block("footer") is defined %} + + {% endif %} +{% endmacro %} + + + + + + + + {% if block("footer") is defined %} + + {% endif %} +
+ {% block body %} + {% endblock %} +
+ + \ No newline at end of file