Statistics
23359
18
1
253d
Badges
Dependencies

PDF Response for Nette 2

Build Status Latest Stable Version Total Downloads

  • sends template as PDF output using mPDF library
  • works with Nette v2.2+
  • requires PHP 5.4 or higher

Install

Installation via Composer.

composer require joseki/pdf-response ">= 2.1"

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 \Joseki\Application\Responses\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 \Joseki\Application\Responses\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 \Joseki\Application\Responses\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 \Joseki\Application\Responses\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 \Joseki\Application\Responses\PdfResponse($template);
    $pdf->setSaveMode(PdfResponse::INLINE);
    $this->sendResponse($pdf);
}

Set a pdf background easily

public function actionPdf()
{
    $pdf = new Joseki\Application\Responses\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 \Joseki\Application\Responses\PdfResponse($template);
    $this->sendResponse($pdf);
}

See also

  • v3.0.0 3.0.0 Released

    • upgraded to mPDF v6+
    • travis: tests against php7
    • composer: required php5.4+
    • PdfResponse: introduced PdfResponse::ZOOM_* and PdfResponse::LAYOUT_* constants
    • PdfResponse refactored - added property accessors with validation for most of the fields
    • removed (deprecated) ITemplate support from Nette <2.2
  • v2.2.3 Released version 2.2.3

    • tests in travis no longer tests against hhwm
    • mPDF series 5.7.* begining with 5.7.3 (including) are not supported
  • v2.2.2 Released version 2.2.2

    • fixed incompatibility with bugged mpdf v5.7.3
    • all changes: diff
  • v2.2.1 Released version 2.2.1

    • fixed onBeforeComplete event
    • fixed setting page format, orientation and margin that had no efect when called after mPDF instance was created. These properties have to be set before getMPDF() is called, an exception is thrown otherwise.
    • tests reworked, enabled travis-ci testing
    • all changes: diff
  • v2.2.0 Released version 2.2

    • Nette 2.2.* compatible
    • added tutorial for usage with standalone Latte
    • removed inner callback createPDF
    • added several error handlers
    • new Exception classes (BC break)
    • added __toString()
    • all changes: diff
  • v2.1.1 Released version 2.1.1

    • background templates fixed (fixed bug with blocked writing to multiple pages)
    • removed example (will be path of a demo repository soon)
    • composer.json updated (package name changed to joseki/pdf-response)
  • v2.1.0 Released version 2.1.0

    Moved to Joseki, updated to Nette 2.1, reorganized, added example.

Is this addon outdated? Did you find an issue? Please report it.

Componette Componette admin@componette.com