surda / mpdf
mPDF integration into Nette Framework
v1.0.4
2019-10-01 08:32 UTC
Requires
- php: >=7.1
- mpdf/mpdf: >=7.0
- nette/application: ^3.0
- nette/di: ^3.0
Requires (Dev)
- nette/tester: ^2.0
- ninjify/nunjuck: 0.3.x-dev
- phpstan/phpstan: ^0.11
- phpstan/phpstan-deprecation-rules: ^0.11
- phpstan/phpstan-nette: ^0.11
- phpstan/phpstan-strict-rules: ^0.11
README
Installation
The recommended way to is via Composer:
composer require surda/mpdf
After that you have to register extension in config.neon:
extensions: mpdf: Surda\Mpdf\DI\MpdfExtension
Configuration
Default
mpdf: mpdf: tempDir: %tempDir%/mpdf
Custom mPDF config
See https://mpdf.github.io/reference/mpdf-functions/construct.html
mpdf: mpdf: tempDir: %tempDir%/mpdf margin_left: 15 margin_right: 15 margin_top: 16 margin_bottom: 16 margin_header: 9 margin_footer: 9 mode: 'utf-8' format: 'A4' orientation: 'P' default_font_size: 0 default_font: ''
Usage
use Surda\Mpdf\MpdfFactory; use Surda\Mpdf\Response\PdfResponse; class OrderPresenter extends Nette\Application\UI\Presenter { /** @var MpdfFactory */ private $pdfFactory; /** * @param MpdfFactory $pdfFactory */ public function injectMpdfFactory(MpdfFactory $pdfFactory): void { $this->pdfFactory = $pdfFactory; } public function actionPdf(): void { $this->template->setFile('/path/to/template.latte'); $mpdf = $this->mpdfFactory->create(); $mpdf->WriteHTML($this->template); $this->sendResponse(new PdfResponse($mpdf->Output())); } }
Create pdf with latte only
$latte = new Latte\Engine; $latte->setTempDirectory('/path/to/temp'); $parameters = [ 'foo' => 'bar', ]; $template = $latte->renderToString('/path/to/template.latte', $parameters); $mpdf = $this->mpdfFactory->create(); $mpdf->WriteHTML($template); $this->sendResponse(new PdfResponse($mpdf->Output()));
Responses
// Download $this->sendResponse(new PdfResponse($mpdf->Output('file.pdf', \Mpdf\Output\Destination::DOWNLOAD))); // Display in a browser $this->sendResponse(new PdfResponse($mpdf->Output('file.pdf', \Mpdf\Output\Destination::INLINE)));