Is possible convert directly HTML into a pdf file using Zend_Pdf?, if so, How can I do that?
Asked
Active
Viewed 2.8k times
13
-
Nope, I don't think this is what it was built for. http://framework.zend.com/manual/en/zend.pdf.introduction.html – Pekka Mar 03 '11 at 16:09
-
Although this might be Frankenstein-ish (and not really an answer to your question) I've used PHP to generate content, send the output buffer to a file, and used _wkhtmltopdf_ to generate PDFs. – Guttsy Mar 03 '11 at 16:47
4 Answers
12
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');

Radek Benkel
- 8,278
- 3
- 32
- 41
-
Is 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! – joren Oct 18 '12 at 13:26
-
@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 TCPDF `writeHTML()` method. – Radek Benkel Oct 18 '12 at 17:48
-
-
like the approach but didn't work for me since i need to pass variable in the view via
$this->view->entities= $entities
All i got it the first th column... Any Hint? – Marcel Djaman Feb 04 '14 at 16:20 -
2
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!!!

Rajan Rawal
- 6,171
- 6
- 40
- 62
2
tcpdf is a little limited when it comes to html, wkhtmltopdf uses webkit

Brendon-Van-Heyzen
- 2,493
- 2
- 24
- 23
-
The question is regarding zend_pdf for which tcppdf, dompdf and mpdf are more sutiable. – SP Singh Jun 14 '17 at 01:53
1
i've used dompdf https://github.com/dompdf/dompdf its pretty easy and straight forward. it even reads/formats css.

Fabien Ménager
- 140,109
- 3
- 41
- 60

Jane Yun
- 185
- 12
-
I 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) – Zsolti Aug 06 '12 at 08:13