Button for getting a PDF back (Zend/PHP)

When using PHP with Zend, the requests for having a button that creates a PDF are like a train that’s never late. Often there are two cases:

  1. Easy: The PDF is created on the backed and stored there. Just pick your favorite PDF library (I use tcpdf). Then the file can be saved on the server, and you can keep track of it by creating a database table for it. For the purpose of downloading it, just make a route (and action) on Zend that handles the request, normally by ID.
  2. Easier but with a catch. The PDF is created every time and not stored on the server. Once that you verify that the PDF object was created, TCPDF has an output method that makes the response for you. The catch is that on the controller you need to set the return view model to terminal (via setTerminal(true)), and directly get out of the controller’s action via return $this->getResponse(); Otherwise, the PDF will contain (after EOF) some HTML, which is not clean, and it could trigger errors (such as unable to find the phtml for the action).

For option 1, consider having a separate class for handling the PDF files together with their corresponding database table, because, if later you have other sections that will make PDFs, you would not want to be repeating such code again and again.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s