From 451b98043a18ba80ae7f7c028aa767bdb19400b9 Mon Sep 17 00:00:00 2001 From: sasedev Date: Fri, 28 Apr 2017 09:56:29 +0200 Subject: [PATCH] update mpdf version deps --- .gitignore | 16 ++ LICENSE | 21 ++ README.md | 4 + composer.json | 36 +++ .../DependencyInjection/Configuration.php | 31 +++ .../SasedevMpdfExtension.php | 31 +++ .../MpdfBundle/Resources/config/services.yml | 7 + src/Sasedev/MpdfBundle/SasedevMpdfBundle.php | 8 + .../MpdfBundle/Service/MpdfService.php | 216 ++++++++++++++++++ 9 files changed, 370 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..58e6a6a --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# Eclipse Files +/.classpath +/.buildpath +/.project +/.settings/ + +/vendor/ + +# PHPUnit +/phpunit.xml + +# 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..8acd514 --- /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|~7.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" + } + ] +} diff --git a/src/Sasedev/MpdfBundle/DependencyInjection/Configuration.php b/src/Sasedev/MpdfBundle/DependencyInjection/Configuration.php new file mode 100644 index 0000000..bc9fa35 --- /dev/null +++ b/src/Sasedev/MpdfBundle/DependencyInjection/Configuration.php @@ -0,0 +1,31 @@ +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..1a80143 --- /dev/null +++ b/src/Sasedev/MpdfBundle/DependencyInjection/SasedevMpdfExtension.php @@ -0,0 +1,31 @@ +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..f553ef5 --- /dev/null +++ b/src/Sasedev/MpdfBundle/SasedevMpdfBundle.php @@ -0,0 +1,8 @@ + + */ +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) + { + return 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; + } +}