Is possible convert HTML into pdf using Zend_Pdf?
27,098
Solution 1
Zend_PDF
isn't able to generate PDF based on HTML. But you can render view and use other library for convert it to PDF. I've done such thing with TCPDF. Little code snippet below:
//SomeController.php
$this->_helper->layout->disableLayout();
//set content for view here
// create new PDF document
require_once('tcpdf/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
//whole TCPDF's settings goes here
$htmlcontent = $this->view->render('recipe/topdf.phtml');
// output the HTML content
$pdf->writeHTML($htmlcontent, true, 0, true, 0);
$pdf->lastPage();
$pdf->Output("pdf-name.pdf", 'D');
Solution 2
tcpdf is a little limited when it comes to html, wkhtmltopdf uses webkit
Solution 3
Check out MPDF . The ultimate one. Create your html with inline css, store it in one php variable and echo to pdf file. you are done!!!
Solution 4
i've used dompdf https://github.com/dompdf/dompdf its pretty easy and straight forward. it even reads/formats css.
Related videos on Youtube
Comments
-
texai over 3 years
Is possible convert directly HTML into a pdf file using Zend_Pdf?, if so, How can I do that?
-
Zsolti over 11 yearsI tried to use dompdf for generating pdf from html, but it has a really annoying (known) issue: it puts a few empty pages between two table rows (at least in 0.6.0 beta 3)
-
joren over 11 yearsIs there a chance you could go into more detail of an example of what somecontroller.php would be, and what goes in 'set content for view here'? Thanks!
-
Radek Benkel over 11 years@joren It's the exact same way in which you create normal controllers in ZF:
SomeController extends Zend_Controller_Action
. Same with view:$this->view->foo = 'bar'
. Whole trick is to render page as normal ZF page, but instead outputing it into screen, you catch this into variable$htmlcontent
and pass to TCPDFwriteHTML()
method. -
joren over 11 yearsThanks! I ended up working around it a different way, bu this help.
-
Marcel Djaman about 10 yearslike the approach but didn't work for me since i need to pass variable in the view via <code>$this->view->entities= $entities</code> All i got it the first th column... Any Hint?
-
Marcel Djaman about 10 yearssorry it was about unclosed tags
-
SP Singh almost 7 yearsThe question is regarding zend_pdf for which tcppdf, dompdf and mpdf are more sutiable.