Search code examples
pdf-generationcodeigniter-2dompdf

DOM PDF is not working when loading Codeigniter's view file


I am trying to create PDF file using DOM pdf and Codeigniter. I have the following script in my Controller and I have kept the DOM folder inside my controller . But when I run the script I get fatal error message.

But when I send a value from my model to DOMPDF's $html which is in my controller .. I can generate the pdf.

Could you please kindly help me how to get rid of this error?

    include_once('dompdf/dompdf_config.inc.php'); 
    $html= $this->load->view('view_pdf');
    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->set_paper("a4", "landscape" ); 

    $dompdf->render();
    $dompdf->stream("my_pdf.pdf", array("Attachment" => 0));   

This is my View File ;

<html>
 <head>
<style>
    #header { position: absolute; left: 0; top: -50px; right: 0; height: 140px; text-align:  center;  }
    #footer {position: fixed ; left: 0px; bottom: -20px; right: 0px; height: 30px; font-size:10; }
    #footer .page:after { content: counter(page, upper-roman); }


        #table-6 {
        width: 100%
        border: 1px solid #B0B0B0;
        }
        #table-6 tbody {
        /* Kind of irrelevant unless your .css is alreadt doing something else */
        margin: 0;
        padding: 0;
        border: 0;
        outline: 0;
        font-size: 100%;
        vertical-align: baseline;
        background: transparent;
        }


        #table-6 td {
        padding: 3px 10px;
        font-size: 12px;
        font-weight: normal;
        text-align:center;
        }
        #table-6 tr:nth-child(even) {
        background: #F2F2F2;
        font-size: 12px;
        font-weight: normal;
        }




 </style>

  </head>

    <body>


     This is a test 
    </body>

    </html>

Solution

  • According to the CodeIgniter documentation on views you have to modify your call to load->view() so that the data is returned as a string to the controller rather than pushed to the web browser. Try modifying your controller code as follows:

    include_once('dompdf/dompdf_config.inc.php');
    $html= $this->load->view('view_pdf','',true);
    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->set_paper("a4", "landscape" );
    
    $dompdf->render();
    $dompdf->stream("my_pdf.pdf", array("Attachment" => 0));
    

    The only change I made was to add the necessary parameters to $this->load->view().

    (I don't use CodeIgniter. Do any CI users have anything to add?)