-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sasedev
committed
Aug 25, 2020
0 parents
commit 9cdc547
Showing
8 changed files
with
394 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/.classpath | ||
/.buildpath | ||
/.project | ||
/.settings/ | ||
/bin/ | ||
/vendor/ | ||
/composer.phar | ||
/composer.lock | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
<?php | ||
// config/bundles.php | ||
|
||
return [ | ||
// ... | ||
Sasedev\MpdfBundle\SasedevMpdfBundle::class => ['all' => true], | ||
// ... | ||
]; | ||
``` | ||
|
||
## Usage | ||
|
||
### Simple usage: | ||
You can use the type in your forms just like this: | ||
```php | ||
<?php | ||
|
||
use Sasedev\MpdfBundle\Factory\MpdfFactory; | ||
|
||
// ... | ||
public function pdf($id, Request $request, MpdfFactory $MpdfFactory) { | ||
// ... | ||
$mPdf = $MpdfFactory->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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "[email protected]", | ||
"issues": "https://github.com/mpdf-bundle/issues" | ||
}, | ||
"license" : "MIT", | ||
"authors": [ | ||
{ | ||
"name": "SALAH Abdelkadeur Seifeddine", | ||
"homepage": "https://sasedev.net", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"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 | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Sasedev/MpdfBundle/DependencyInjection/SasedevMpdfExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
namespace Sasedev\MpdfBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Loader; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
|
||
/** | ||
* | ||
* Sasedev\MpdfBundle\DependencyInjection\SasedevMpdfExtension | ||
* | ||
* | ||
* @author sasedev <[email protected]> | ||
* 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'); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
<?php | ||
namespace Sasedev\MpdfBundle\Factory; | ||
|
||
use Mpdf\Config\ConfigVariables; | ||
use Mpdf\Config\FontVariables; | ||
use Mpdf\Mpdf; | ||
use Mpdf\Output\Destination; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* | ||
* Sasedev\MpdfBundle\Factory\MpdfFactory | ||
* | ||
* | ||
* @author sasedev <[email protected]> | ||
* 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); | ||
|
||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
services: | ||
Sasedev\MpdfBundle\Factory\MpdfFactory: | ||
arguments: ["%kernel.cache_dir%"] | ||
public: true |
Oops, something went wrong.