From 9cdc547b46a3e89e29397cbb0aaa8ae83071c0cd Mon Sep 17 00:00:00 2001 From: sasedev Date: Tue, 25 Aug 2020 11:46:09 +0200 Subject: [PATCH] v2 --- .gitignore | 9 + LICENSE | 21 ++ README.md | 59 ++++++ composer.json | 56 ++++++ .../SasedevMpdfExtension.php | 34 ++++ .../MpdfBundle/Factory/MpdfFactory.php | 181 ++++++++++++++++++ .../MpdfBundle/Resources/config/services.yaml | 4 + src/Sasedev/MpdfBundle/SasedevMpdfBundle.php | 30 +++ 8 files changed, 394 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/SasedevMpdfExtension.php create mode 100644 src/Sasedev/MpdfBundle/Factory/MpdfFactory.php create mode 100644 src/Sasedev/MpdfBundle/Resources/config/services.yaml create mode 100644 src/Sasedev/MpdfBundle/SasedevMpdfBundle.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3da7c25 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/.classpath +/.buildpath +/.project +/.settings/ +/bin/ +/vendor/ +/composer.phar +/composer.lock +.phpunit.result.cache diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7875b6e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2020 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..1c59f1f --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# Sasedev - Mpdf Bundle + +Pdf generator for Symfony. + +## What is it? + +This is a Symfony Pdf Factory for use inside a controller to generate a PDF file from twig rendring using MPDF lib. + +## Installation + +### Step 1: Download HiddenEntityTypeBundle using composer +```bash +$ composer require sasedev/mpdf-bundle +``` +Composer will install the bundle to your project's vendor directory. + +### Step 2: Enable the bundle +Enable the bundle in the config if flex it did´nt do it for you: +```php + ['all' => true], + // ... +]; +``` + +## Usage + +### Simple usage: +You can use the type in your forms just like this: +```php +createMpdfObject([ +'mode' => 'utf-8', +'format' => 'A4', +'margin_header' => 5, +'margin_footer' => 5, +'orientation' => 'P' +]); +$mPdf->SetTopMargin("50"); +$mPdf->SetHTMLHeader($this->renderView('twigfolder/pdf/pdf_header.html.twig', $TwigVars)); +$mPdf->SetFooter($this->renderView('twigfolder/pdf/pdf_footer.html.twig', $TwigVars)); +$mPdf->WriteHTML($this->renderView('twigfolder/pdf/pdf_content.html.twig', $TwigVars)); +return $MpdfFactory->createDownloadResponse($mPdf, "file.pdf"); +} +``` + +## Reporting an issue or a feature request +Feel free to report any issues. If you have an idea to make it better go ahead and modify and submit pull requests. + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..c267160 --- /dev/null +++ b/composer.json @@ -0,0 +1,56 @@ +{ + "name" : "sasedev/mpdf-bundle", + "description" : "Mpdf for Symfony (5.1)", + "keywords" : [ + "mpdf", + "symfony", + "sasedev" + ], + "type" : "symfony-bundle", + "homepage": "https://github.com/sasedev/mpdf-bundle", + "support": { + "email": "sinus@sasedev.net", + "issues": "https://github.com/mpdf-bundle/issues" + }, + "license" : "MIT", + "authors": [ + { + "name": "SALAH Abdelkadeur Seifeddine", + "homepage": "https://sasedev.net", + "email": "sinus@sasedev.net" + } + ], + "config" : { + "bin-dir" : "bin", + "component-dir" : "vendor", + "preferred-install" : { + "*" : "dist" + }, + "process-timeout" : 1200, + "sort-packages" : true + }, + "autoload" : { + "psr-4" : { + "" : "src" + } + }, + "autoload-dev" : { + "psr-4" : { + "" : "tests" + } + }, + "require" : { + "php": ">=7.2", + "mpdf/mpdf": "*", + "symfony/config": "^5.1", + "symfony/dependency-injection": "^5.1", + "symfony/http-kernel": "^5.1" + }, + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + }, + "minimum-stability": "dev", + "prefer-stable": true +} diff --git a/src/Sasedev/MpdfBundle/DependencyInjection/SasedevMpdfExtension.php b/src/Sasedev/MpdfBundle/DependencyInjection/SasedevMpdfExtension.php new file mode 100644 index 0000000..585cb32 --- /dev/null +++ b/src/Sasedev/MpdfBundle/DependencyInjection/SasedevMpdfExtension.php @@ -0,0 +1,34 @@ + + * Created on: 5 juil. 2020 @ 22:25:09 + */ +class SasedevMpdfExtension extends Extension +{ + + /** + * + * {@inheritdoc} + * + */ + public function load(array $configs, ContainerBuilder $container) + { + + $locator = new FileLocator(__DIR__ . '/../Resources/config'); + $loader = new Loader\YamlFileLoader($container, $locator); + $loader->load('services.yaml'); + + } + +} diff --git a/src/Sasedev/MpdfBundle/Factory/MpdfFactory.php b/src/Sasedev/MpdfBundle/Factory/MpdfFactory.php new file mode 100644 index 0000000..3bd57d6 --- /dev/null +++ b/src/Sasedev/MpdfBundle/Factory/MpdfFactory.php @@ -0,0 +1,181 @@ + + * Created on: 1 juin 2020 @ 22:56:28 + */ +class MpdfFactory +{ + + /** + * + * @var string + */ + private $cacheDir = '/tmp'; + + /** + * MpdfFactory constructor. + * + * @param string $cacheDir + */ + public function __construct(string $cacheDir) + { + + $this->cacheDir = $cacheDir; + + } + + /** + * + * @return array + */ + public function getDefaultConstructorParams(): array + { + + return [ + 'mode' => '', + 'format' => 'A4', + 'default_font_size' => 0, + 'default_font' => '', + 'margin_left' => 15, + 'margin_right' => 15, + 'margin_top' => 16, + 'margin_bottom' => 16, + 'margin_header' => 9, + 'margin_footer' => 9, + 'orientation' => 'P' + ]; + + } + + /** + * + * @return array + */ + public function getDefaultConfigVariables(): array + { + + $configObject = new ConfigVariables(); + return $configObject->getDefaults(); + + } + + /** + * + * @return array + */ + public function getDefaultFontVariables(): array + { + + $fontObject = new FontVariables(); + return $fontObject->getDefaults(); + + } + + /** + * Get an instance of mPDF class + * + * @param array $mpdfArgs + * arguments for mPDF constror + * @return Mpdf + */ + public function createMpdfObject($mpdfArgs = []) + { + + $defaultOptions = \array_merge($this->getDefaultConstructorParams(), $this->getDefaultConfigVariables(), $this->getDefaultFontVariables(), + [ + 'mode' => 'utf-8', + 'format' => 'A4', + 'tempDir' => $this->cacheDir + ]); + + $argOptions = \array_merge($defaultOptions, $mpdfArgs); + + $mPdf = new Mpdf($argOptions); + + return $mPdf; + + } + + /** + * + * @param Mpdf $mPdf + * @param string $filename + * @param int $status + * @param array $headers + * + * @return Response + */ + public function createInlineResponse(Mpdf $mPdf, ?string $filename = null, ?int $status = 200, ?array $headers = []) + { + + $content = $mPdf->Output('', Destination::STRING_RETURN); + + $defaultHeaders = [ + 'Content-Type' => 'application/pdf', + 'Cache-Control' => 'public, must-revalidate, max-age=0', + 'Pragma' => 'public', + 'Expires' => 'Sat, 26 Jul 1997 05:00:00 GMT', + 'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT' + ]; + if (false == \is_null($filename)) + { + $defaultHeaders['Content-disposition'] = sprintf('inline; filename="%s"', $filename); + } + else + { + $defaultHeaders['Content-disposition'] = sprintf('inline; filename="%s"', 'file.pdf'); + } + + $headers = \array_merge($defaultHeaders, $headers); + + return new Response($content, $status, $headers); + + } + + /** + * + * @param Mpdf $mPdf + * @param string $filename + * @param int $status + * @param array $headers + * + * @return Response + */ + public function createDownloadResponse(Mpdf $mPdf, string $filename, ?int $status = 200, ?array $headers = []) + { + + $content = $mPdf->Output('', Destination::STRING_RETURN); + + $defaultHeaders = [ + 'Content-Type' => 'application/pdf', + 'Content-Description' => 'File Transfer', + 'Content-Transfer-Encoding' => 'binary', + 'Cache-Control' => 'public, must-revalidate, max-age=0', + 'Pragma' => 'public', + 'Expires' => 'Sat, 26 Jul 1997 05:00:00 GMT', + 'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT', + 'Content-disposition' => sprintf('attachment; filename="%s"', $filename) + ]; + + $headers = \array_merge($headers, $defaultHeaders); + + $headers = \array_merge($defaultHeaders, $headers); + + return new Response($content, $status, $headers); + + } + +} + diff --git a/src/Sasedev/MpdfBundle/Resources/config/services.yaml b/src/Sasedev/MpdfBundle/Resources/config/services.yaml new file mode 100644 index 0000000..4f62d71 --- /dev/null +++ b/src/Sasedev/MpdfBundle/Resources/config/services.yaml @@ -0,0 +1,4 @@ +services: + Sasedev\MpdfBundle\Factory\MpdfFactory: + arguments: ["%kernel.cache_dir%"] + public: true diff --git a/src/Sasedev/MpdfBundle/SasedevMpdfBundle.php b/src/Sasedev/MpdfBundle/SasedevMpdfBundle.php new file mode 100644 index 0000000..0778ef6 --- /dev/null +++ b/src/Sasedev/MpdfBundle/SasedevMpdfBundle.php @@ -0,0 +1,30 @@ + + * Created on: 1 juin 2020 @ 22:58:16 + */ +class SasedevMpdfBundle extends Bundle +{ + + /** + * + * {@inheritdoc} + * @see \Symfony\Component\HttpKernel\Bundle\Bundle::getContainerExtension() + */ + public function getContainerExtension() + { + + return new SasedevMpdfExtension(); + + } + +}