PHP code example of surda / mpdf

1. Go to this page and download the library: Download surda/mpdf library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

surda / mpdf example snippets


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

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

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