Is possible convert HTML into pdf using Zend_Pdf?
Asked Answered
L

4

13

Is possible convert directly HTML into a pdf file using Zend_Pdf?, if so, How can I do that?

Lepley answered 3/3, 2011 at 16:2 Comment(2)
Nope, I don't think this is what it was built for. framework.zend.com/manual/en/zend.pdf.introduction.htmlPhenylalanine
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.Adigun
H
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');
Handiwork answered 3/3, 2011 at 18:30 Comment(5)
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!Ludlow
@Ludlow 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.Handiwork
Thanks! I ended up working around it a different way, bu this help.Ludlow
like 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?Stipel
sorry it was about unclosed tagsStipel
M
2

tcpdf is a little limited when it comes to html, wkhtmltopdf uses webkit

Maziemazlack answered 3/3, 2011 at 18:54 Comment(1)
The question is regarding zend_pdf for which tcppdf, dompdf and mpdf are more sutiable.Julesjuley
M
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!!!

Mercie answered 15/5, 2013 at 7:17 Comment(0)
J
1

i've used dompdf https://github.com/dompdf/dompdf its pretty easy and straight forward. it even reads/formats css.

Jolty answered 5/3, 2011 at 9:7 Comment(1)
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)Cloistered

© 2022 - 2024 — McMap. All rights reserved.