surda/mpdf

mPDF integration into Nette Framework

v1.0.4 2019-10-01 08:32 UTC

This package is auto-updated.

Last update: 2024-10-29 05:34:49 UTC


README

Build Status Licence Latest stable PHPStan

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)));