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:
- 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.
- 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
outputmethod 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.