In previous tutorial, we have learned how to generate PDFs from html and snappy. In this tutorial, we will use the same approach, but apply it in CakePHP 3 framework. This tactic will enable the CakePHP controllers of your application to generate PDFs with a few lines of code. Table Of Content • • • • • Installing dependencies The same as previous email, we will need to install the dependencies, which include wkhtmltopdf and wkhtmltoimage's binary files and the Snappy package. A gentle reminder: make sure you specify the correct wkhtmltopdf and wkhtmltoimage's binary files according to your operating system. Otherwise it won't work. Here is of the binary files again. In the CakePHP project's root folder, install dependencies via composer require. We are still demonstrating in this in an OSX machine. $html = (new ViewBuilder())->layout('ajax')->build($data)->render($template); Note how we use CakePHP's ViewBuilder to render the template's HTML source code. ViewBuilder is a pretty useful class of CakePHP. I am trying to get HTML from view in CakePHP and want to use it in controller to generate PDF, but when I send HTML in variable I get an error, but it's working for texts. Generate PDF in CakePHP 3.x with CakePDF And Wkhtmltopdf. Generate PDF in CakePHP 3.x with CakePDF And DomPDF. Lastly we will generate the PDF by Snappy and return it: return $snappy->getOutputFromHtml($html); Note in this step, we are using a different Snappy API getOutputFromHtmlinstead of generateFromHtml in previous tutorial. The reason is we want to get a PDF as a return value. Below is the full source code for the PdfWriter class. Configuration options: • engine: Engine to be used (required), or an array of engine config options • className: Engine class to use • options: Engine specific options. Currently only for WkHtmlToPdf, where the options are passed as CLI arguments, and for DomPdf, where the options are passed to the DomPdf class constructor. You can find how to save a file here: $html = '. 'Put your html here, or generate it with your favourite '. 'templating system.' '; $dompdf = new DOMPDF(); $dompdf->load_html($html); $dompdf->render(); $output = $dompdf->output(); file_put_contents('Brochure.pdf', $output); Sending email is very straight forward with CakeEmailL. Take a look here: $Email = new CakeEmail(); $Email->from(array('[email protected]' => 'My Site')); $Email->to('[email protected]'); $Email->subject('About'); $Email->attachments('/full/file/path/Brochure.pdf'); $Email->send('My message').
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2018
Categories |