Lorem ipsum

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Sed elit dui, pellentesque a, faucibus vel, interdum nec, diam. Etiam posuere lacus quis dolor. Aliquam ante. Sed vel lectus. Donec odio tempus molestie, porttitor ut, iaculis quis, sem. Aliquam erat volutpat. Fusce wisi. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam eget nisl. Etiam bibendum elit eget erat.

ReadMe

Usage

How to prepare PDF from template

// in a Presenter
public function actionPdf()
{
    $template = $this->createTemplate();
    $template->setFile(__DIR__ . "/path/to/template.latte");
    $template->someValue = 123;
    // Tip: In template to make a new page use <pagebreak>

    $pdf = new \Contributte\PdfResponse\PdfResponse($template);

    // optional
    $pdf->documentTitle = date("Y-m-d") . " My super title"; // creates filename 2012-06-30-my-super-title.pdf
    $pdf->pageFormat = "A4-L"; // wide format
    $pdf->getMPDF()->setFooter("|© www.mysite.com|"); // footer

    // do something with $pdf
    $this->sendResponse($pdf);
}

Save file to server

public function actionPdf()
{
    $template = $this->createTemplate();
    $template->setFile(__DIR__ . "/path/to/template.latte");

    $pdf = new \Contributte\PdfResponse\PdfResponse($template);

    $pdf->save(__DIR__ . "/path/to/directory"); // as a filename $this->documentTitle will be used
    $pdf->save(__DIR__ . "/path/to/directory", "filename"); // OR use a custom name
}

Attach file to an email

public function actionPdf()
{
    $template = $this->createTemplate();
    $template->setFile(__DIR__ . "/path/to/template.latte");

    $pdf = new \Contributte\PdfResponse\PdfResponse($template);

    $savedFile = $pdf->save(__DIR__ . "/path/to/directory");
    $mail = new Nette\Mail\Message;
    $mail->addTo("john@doe.com");
    $mail->addAttachment($savedFile);
    $mailer = new SendmailMailer();
    $mailer->send($mail);
}

Force file to download

public function actionPdf()
{
    $template = $this->createTemplate();
    $template->setFile(__DIR__ . "/path/to/template.latte");

    $pdf = new \Contributte\PdfResponse\PdfResponse($template);
    $pdf->setSaveMode(PdfResponse::DOWNLOAD); //default behavior
    $this->sendResponse($pdf);
}

Force file to display in a browser

public function actionPdf()
{
    $template = $this->createTemplate();
    $template->setFile(__DIR__ . "/path/to/template.latte");

    $pdf = new \Contributte\PdfResponse\PdfResponse($template);
    $pdf->setSaveMode(PdfResponse::INLINE);
    $this->sendResponse($pdf);
}

Set a pdf background easily

public function actionPdf()
{
    $pdf = new \Contributte\PdfResponse\PdfResponse('');
    $pdf->setBackgroundTemplate(__DIR__ . "/path/to/an/existing/file.pdf");

    // to write into an existing document use the following statements
    $mpdf = $pdf->getMPDF();
    $mpdf->WriteFixedPosHTML('hello world', 1, 10, 10, 10);

    // to write to another page
    $mpdf->AddPage();

    // to move to exact page, use
    $mpdf->page = 3; // = move to 3rd page

    $this->sendResponse($pdf);
}

Create pdf with latte only

public function actionPdf()
{
    $latte = new Latte\Engine;
    $latte->setTempDirectory('/path/to/cache');
    $latte->addFilter('money', function($val) { return ...; }); // formerly registerHelper()

    $latte->onCompile[] = function($latte) {
        $latte->addMacro(...); // when you want add some own macros, see http://goo.gl/d5A1u2
    };

    $template = $latte->renderToString(__DIR__ . "/path/to/template.latte");

    $pdf = new \Contributte\PdfResponse\PdfResponse($template);
    $this->sendResponse($pdf);
}

Configuration of custom temp dir for mPDF in PdfResponse

services:
    - factory: Contributte\PdfResponse\PdfResponse
        setup:
            - $mpdfConfig([tempDir: %tempDir%/mpdf])

See also


Thanks for testing, reporting and contributing.