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.

Share:
27,098

Related videos on Youtube

texai
Author by

texai

Software engineer and PHP developer.

Updated on August 15, 2020

Comments

  • texai
    texai over 3 years

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

  • Zsolti
    Zsolti over 11 years
    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)
  • joren
    joren over 11 years
    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!
  • Radek Benkel
    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 TCPDF writeHTML() method.
  • joren
    joren over 11 years
    Thanks! I ended up working around it a different way, bu this help.
  • Marcel Djaman
    Marcel Djaman about 10 years
    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?
  • Marcel Djaman
    Marcel Djaman about 10 years
    sorry it was about unclosed tags
  • SP Singh
    SP Singh almost 7 years
    The question is regarding zend_pdf for which tcppdf, dompdf and mpdf are more sutiable.

Related