Skip to content
mPDF integration into Nette Framework
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitignore
.travis.yml
LICENSE
README.md
composer.json
phpstan.neon

README.md

mPDF integration into Nette Framework


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)));
You can’t perform that action at this time.