Generating PDFs with Cake. PHP « Der. Euro. Mark. The recommended approach is to use the Cake. PHP plugin Cake. Pdf for this. With that plugin it is . If you already have PDF files you just want to output or serve as download, then this article is not for you. Generating PDFs with CakePHP. With that plugin it is 'a piece of cake' to output HTML views as PDF or render them to files for download/emailing etc. You can obviously just use an approach just like this article. See serving- views- as- files- in- cake. Demo. A live demo can be found in my sandbox app. It also helps to compare the different engines. Installation. First you need to decide on what engine you want to go with. Wk. Html. To. Pdf is the fastest, but it also depends on CLI and the binaries being installed properly. So it might work differently or not at all on different OS. But using binary files it should work out of the both on both UNIX and Windows. The latter just needs a single exe file drag- and- dropped on your system. You need to manually compile it in this case. But on most systems it should be fine. Don’t forget to adjust the path to the binary here, as well, if it is not the default one: Configure: :write('Cake. Pdf. binary', YOUR. If speed really is an issue you might want to reconsider using the server internal libraries. But for me generating invoices and such never is time- critical and usually done asynchronous using background tasks via shells . Check it out. Advanced Setup. The recommended way is to use Cake. Plugin: :load('Cake. Pdf', array('bootstrap' => true, 'routes' => true)); which is supposed to take care of automatic View class switching. If you don’t want to load the plugin bootstrap/routes and rather do it manually you need to automatically map all . URLs to the Cake. Pdf plugin Pdf. View ourselves: public $components = array(. Request. Handler' => array(. Class. Map' => array('pdf' => 'Cake. Pdf. Pdf'). ); This can also be useful if you want to overwite/extend the Pdf. View class for some reason (My. Cake. Pdf etc). Skipping the plugin routes file is also useful, if you already have extension settings in your routes. You can then simply update your APP/Config/routes. Router: :parse. Extensions(). Router: :set. Extensions(array('json', 'xml', 'rss', 'pdf')); // We just add pdf to the already defined ones. With both routes and bootstrap defined manually, Cake. Plugin: :load('Cake. Pdf'); suffices. Usage. The official documentation already states that, so I won’t go into much detail. For the sake of completeness a short example: public function view($id) . Using the PdfWriter class in a controller action. Let's pretend we have a PdfsController controller and we want its generate action to create a PDF using the generate.tcp template as its content, and force user to download the.If you link to it with ? Html- > link('Download PDF', array(. This way the action can do both automatically. Tips. URLs and Paths. For all engines to work with your URLs and images outputted by helpers and therefore with schema- less absolute URLs (/controller/action/ and /img/foo. App. Helper is quite useful: /**. Overwrite to make URLs absolute for PDF content.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |