From 1434b8929bcc6c84b1ff53bd6700124fc78b823b Mon Sep 17 00:00:00 2001 From: sasedev Date: Fri, 22 Jan 2016 15:12:21 +0100 Subject: [PATCH] Initial Commit --- .gitignore | 46 +++ LICENSE | 21 ++ README.md | 4 + composer.json | 36 +++ .../DependencyInjection/Configuration.php | 35 +++ .../SasedevMpdfExtension.php | 35 +++ .../MpdfBundle/Resources/config/services.yml | 7 + src/Sasedev/MpdfBundle/SasedevMpdfBundle.php | 9 + .../MpdfBundle/Service/MpdfService.php | 262 ++++++++++++++++++ 9 files changed, 455 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/Sasedev/MpdfBundle/DependencyInjection/Configuration.php create mode 100644 src/Sasedev/MpdfBundle/DependencyInjection/SasedevMpdfExtension.php create mode 100644 src/Sasedev/MpdfBundle/Resources/config/services.yml create mode 100644 src/Sasedev/MpdfBundle/SasedevMpdfBundle.php create mode 100644 src/Sasedev/MpdfBundle/Service/MpdfService.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..23761b4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,46 @@ +# Eclipse Files +/.classpath +/.buildpath +/.project +/.settings/ + +# Cache and logs (Symfony2) +/app/cache/* +/app/logs/* +!app/cache/.gitkeep +!app/logs/.gitkeep + +# Cache and logs (Symfony3) +/var/cache/* +/var/logs/* +!var/cache/.gitkeep +!var/logs/.gitkeep + +# Parameters +/app/config/parameters.yml +/app/config/parameters.ini + +# Managed by Composer +/app/bootstrap.php.cache +/var/bootstrap.php.cache +/bin/* +!bin/console +!bin/symfony_requirements +/vendor/ + +# Assets and user uploads +/web/bundles/ +/web/uploads/ + +# PHPUnit +/app/phpunit.xml +/phpunit.xml + +# Build data +/build/ + +# Composer PHAR +/composer.phar + +# Composer lock +/composer.lock diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9fa950b --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Salah Abdelkader Seif Eddine + +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..8f6a996 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +sasedev/mpdf-bundle +========================= + +Mpdf Bundle for Symfony2 (3.0) diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..8899d7f --- /dev/null +++ b/composer.json @@ -0,0 +1,36 @@ +{ + "name" : "sasedev/mpdf-bundle", + "description" : "Mpdf for Symfony2 (3.0)", + "require" : { + "php" : ">=5.5", + "mpdf/mpdf" : "6.0.*", + "symfony/twig-bridge" : "~2.8|~3.0", + "symfony/monolog-bridge" : "~2.8|~3.0", + "symfony/http-foundation" : "~2.8|~3.0", + "symfony/config" : "~2.8|~3.0", + "symfony/dependency-injection" : "~2.8|~3.0", + "symfony/yaml" : "~2.8|~3.0", + "symfony/http-kernel" : "~2.8|~3.0" + }, + "license" : "MIT", + "keywords" : [ + "symfony2", + "form" + ], + "extra" : { + "branch-alias" : { + "dev-master" : "1.0.x-dev" + } + }, + "autoload" : { + "psr-4" : { + "" : "src" + } + }, + "type" : "symfony-bundle", + "authors" : [{ + "name" : "Salah Abdelkader Seif Eddine", + "email" : "seif.salah@gmail.com" + } + ] +} \ No newline at end of file diff --git a/src/Sasedev/MpdfBundle/DependencyInjection/Configuration.php b/src/Sasedev/MpdfBundle/DependencyInjection/Configuration.php new file mode 100644 index 0000000..45919e3 --- /dev/null +++ b/src/Sasedev/MpdfBundle/DependencyInjection/Configuration.php @@ -0,0 +1,35 @@ +root('sasedev_mpdf'); + $treeBuilder->root('sasedev_mpdf'); + + // 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/Sasedev/MpdfBundle/DependencyInjection/SasedevMpdfExtension.php b/src/Sasedev/MpdfBundle/DependencyInjection/SasedevMpdfExtension.php new file mode 100644 index 0000000..a780184 --- /dev/null +++ b/src/Sasedev/MpdfBundle/DependencyInjection/SasedevMpdfExtension.php @@ -0,0 +1,35 @@ +processConfiguration($configuration, $configs); + $this->processConfiguration($configuration, $configs); + + $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); + $loader->load('services.yml'); + + } + +} diff --git a/src/Sasedev/MpdfBundle/Resources/config/services.yml b/src/Sasedev/MpdfBundle/Resources/config/services.yml new file mode 100644 index 0000000..cec0488 --- /dev/null +++ b/src/Sasedev/MpdfBundle/Resources/config/services.yml @@ -0,0 +1,7 @@ +parameters: + sasedev_mpdf.class: Sasedev\MpdfBundle\Service\MpdfService + +services: + sasedev_mpdf: + class: "%sasedev_mpdf.class%" + arguments: ["@templating", "@logger", "%kernel.cache_dir%"] diff --git a/src/Sasedev/MpdfBundle/SasedevMpdfBundle.php b/src/Sasedev/MpdfBundle/SasedevMpdfBundle.php new file mode 100644 index 0000000..b967492 --- /dev/null +++ b/src/Sasedev/MpdfBundle/SasedevMpdfBundle.php @@ -0,0 +1,9 @@ + + */ +class MpdfService +{ + + /** + * + * @var mPDF $mpdf + */ + protected $mpdf; + + /** + * + * @var TwigEngine $renderer; + */ + protected $renderer; + + /** + * + * @var Logger $logger + */ + protected $logger; + + /** + * + * @var float + */ + protected $start_time; + + /** + * + * @param + * $renderer + * @param + * $logger + * @param + * $cache_dir + */ + public function __construct($renderer, $logger, $cache_dir) + { + // vendor folder probably doesn't have write access, + // so put the temp folder under the cache folder which should have write access + $tmp_folder = $cache_dir . '/tmp/'; + if (!is_dir($tmp_folder)) { + mkdir($tmp_folder); + } + $font_folder = $cache_dir . '/ttfontdata/'; + if (!is_dir($font_folder)) { + mkdir($font_folder); + } + if (!defined('_MPDF_TEMP_PATH')) { + define("_MPDF_TEMP_PATH", $tmp_folder); + } + if (!defined('_MPDF_TTFONTDATAPATH')) { + define("_MPDF_TTFONTDATAPATH", $font_folder); + } + $this->renderer = $renderer; + $this->logger = $logger; + + } + + /** + * + * @param string $mode + * @param string $format + * @param string $default_font_size + * @param string $default_font + * @param string $margin_left + * @param string $margin_right + * @param string $margin_top + * @param string $margin_bottom + * @param string $margin_header + * @param string $margin_footer + * @param string $orientation + * @see mPDF + */ + public function init( + $mode = '', + $format = '', + $default_font_size = '', + $default_font = '', + $margin_left = '', + $margin_right = '', + $margin_top = '', + $margin_bottom = '', + $margin_header = '', + $margin_footer = '', + $orientation = '') + { + + $this->start_time = microtime(true); + $this->mpdf = new mPDF( + $mode, + $format, + $default_font_size, + $default_font, + $margin_left, + $margin_right, + $margin_top, + $margin_bottom, + $margin_header, + $margin_footer, + $orientation); + $this->logger->debug("sasedev_mpdf: Using temp folder " . _MPDF_TEMP_PATH); + $this->logger->debug("sasedev_mpdf: Using font folder " . _MPDF_TTFONTDATAPATH); + + } + + /** + * Set property of mPDF + * + * @param + * $name + * @param + * $value + */ + public function setProperty($name, $value) + { + + $this->mpdf->{$name} = $value; + + } + + /** + * Call method of mPDF + * + * @param + * $name + * @param array $data + */ + public function callMethod($name, array $data) + { + + call_user_func_array(array( + $this->mpdf, + $name + ), $data); + + } + + /** + * Get instance of mPDF object + * + * @return mPDF + */ + public function getMpdf() + { + + return $this->mpdf; + + } + + /** + * Set html + * + * @param string $html + */ + public function setHtml($html) + { + + if (!$this->mpdf) { + $this->init(); + } + $this->mpdf->WriteHTML($html); + + } + + /** + * Renders and set as html the template with the given context + * + * @param + * $template + * @param array $data + */ + public function useTwigTemplate($template, array $data = array()) + { + + $html = $this->renderer->render($template, $data); + $this->setHtml($html); + return $this; + + } + + /** + * Generate pdf document and return it as string + * + * @return string + */ + public function generate() + { + + if (!$this->mpdf) { + $this->init(); + } + // Better to avoid having mpdf set any headers as these can interfer with symfony responses + $output = $this->mpdf->Output('', 'S'); + $time = microtime(true) - $this->start_time; + $this->logger->debug("sasedev_mpdf: pdf generation took " . $time . " seconds"); + return $output; + + } + + /** + * Generate pdf document and returns it as Response object + * + * @param string $filename + * @return Response + */ + public function generateInlineFileResponse($filename) + { + + $headers = array( + 'content-type' => 'application/pdf', + 'content-disposition' => sprintf('inline; filename="%s"', $filename) + ); + $content = $this->generate(); + return new Response($content, 200, $headers); + + } + + /** + * Generate pdf document and returns it as Response object + * + * @param string $filename + * @return Response + */ + public function generateStreamedResponse($status = 200, $headers = array()) + { + + $content = $this->generate(); + return new StreamedResponse(function () use ($content) + { + file_put_contents('php://output', $content); + }, $status, $headers); + + } + + /** + * Return mPDF version + * + * @return null|string + */ + public function getVersion() + { + + return defined('mPDF_VERSION') ? mPDF_VERSION : null; + + } + +} \ No newline at end of file