old source
This commit is contained in:
parent
17e39fd07c
commit
50e99b7f61
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2025 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.
|
58
README.md
Normal file
58
README.md
Normal file
@ -0,0 +1,58 @@
|
||||
# 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
|
||||
|
||||
You can use the factory in your controllers 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.
|
@ -40,7 +40,11 @@
|
||||
}
|
||||
},
|
||||
"require" : {
|
||||
"php": ">=7.2"
|
||||
"php": ">=7.2",
|
||||
"mpdf/mpdf": "*",
|
||||
"symfony/config": "^5.1|^6.3|^7.0",
|
||||
"symfony/dependency-injection": "^5.1|^6.3|^7.0",
|
||||
"symfony/http-kernel": "^5.1|^6.3|^7.0"
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
|
@ -0,0 +1,30 @@
|
||||
<?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 <sinus@sasedev.net>
|
||||
* 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');
|
||||
}
|
||||
}
|
180
src/Sasedev/MpdfBundle/Factory/MpdfFactory.php
Normal file
180
src/Sasedev/MpdfBundle/Factory/MpdfFactory.php
Normal file
@ -0,0 +1,180 @@
|
||||
<?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 <sinus@sasedev.net>
|
||||
* 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);
|
||||
|
||||
}
|
||||
|
||||
}
|
4
src/Sasedev/MpdfBundle/Resources/config/services.yaml
Normal file
4
src/Sasedev/MpdfBundle/Resources/config/services.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
services:
|
||||
Sasedev\MpdfBundle\Factory\MpdfFactory:
|
||||
arguments: ["%kernel.cache_dir%"]
|
||||
public: true
|
Loading…
x
Reference in New Issue
Block a user