geniv/nette-wrapper-mpdf

Wrapper Mpdf component for Nette Framework

v1.0.7 2021-08-16 08:46 UTC

This package is auto-updated.

Last update: 2024-12-16 16:41:04 UTC


README

mpdf package:

mpdf doc:

mpdf stackoverflow:

setBacktrackLimit:

Installation

$ composer require geniv/nette-wrapper-mpdf

or

"geniv/nette-wrapper-mpdf": "^1.0"

require:

"php": ">=7.0",
"nette/nette": ">=2.4",
"geniv/nette-general-form": ">=1.0",
"mpdf/mpdf": ">=7.1"

Include in application

neon configure:

services:
    - WrapperMpdf\WrapperMpdf
#    - WrapperMpdf\Logger

presenter define:

public function createComponentWrapperMpdf(WrapperMpdf $wrapperMpdf): WrapperMpdf
{
    $wrapperMpdf->setTemplatePath(__DIR__ . '/templates/Project/pdf.latte');
    $wrapperMpdf->setTemplatePathHeader(__DIR__ . '/templates/pdfHeader.latte');
    $wrapperMpdf->setTemplatePathFooter(__DIR__ . '/templates/pdfFooter.latte');
    $wrapperMpdf->setTemplatePathStyle(__DIR__ . '/templates/pdfStyle.latte');
    
    $wrapperMpdf->setTitle('Titulek');
//    $wrapperMpdf->setFormat('A4-L');
//    $wrapperMpdf->setFormat('A4');
    $wrapperMpdf->addFontDirectory(__DIR__ . '/../components/mPDF/ttfonts');
    $wrapperMpdf->addFont('roboto', [
        'R'  => 'Roboto-Regular.ttf',
        'B'  => 'Roboto-Bold.ttf',
        'I'  => 'Roboto-Italic.ttf',
        'BI' => 'Roboto-BoldItalic.ttf',
    ]);

    $wrapperMpdf->addConfig('setAutoBottomMargin', 'stretch');
    $wrapperMpdf->addConfig('autoMarginPadding', -9);

//    $wrapperMpdf->setLogger($logger);
//    $wrapperMpdf->setShowImageErrors(true);
//    $wrapperMpdf->setDebug(true);
//    $wrapperMpdf->setBacktrackLimit('5000000');

    return $wrapperMpdf;
}

presenter usage:

public function actionPdf()
{
    $wrapper = $this['wrapperMpdf'];
    $wrapper->setTemplatePath(__DIR__ . '/templates/pdf.latte');

//    $wrapper->setFormat('A4-L');
    $wrapper->setFormat('A4');

    $title = 'title of page';
    $wrapper->setTitle($title);
    $wrapper->addVariableTemplate('title', $title);

    $wrapper->render();
//    $wrapper->render(true);   // preview
//    $wrapper->render(false, 'mpdf.pdf');  // save to file
//    $pdf = $wrapper->render(false, null, \Mpdf\Output\Destination::STRING_RETURN);  // return to variable
    $this->terminate();
}

tips for latte:

{* use dataStream for convert do base64 if https is wrong configure *}
{thumb projectDetail, $item['image']|dataStream}
{dataStream}/www/images/pdf-logo.png{/dataStream}

{* pager: *}
{l}PAGENO{r}/{l}nbpg{r}

{* style.latte: *}
background: url('{dataStream}/www/images/line.png{/dataStream}') repeat-x center;