PHP - DOMPDF 1 function works, another does not

1.7k views Asked by At

I am working with DOMPDF currently, and I trying to work out why my PDF's are not being created correctly.

When I run the following code,

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename, $stream=TRUE) 
{
    require_once("dompdf/dompdf_config.inc.php");

    $dompdf = new DOMPDF();
    $dompdf->set_base_path($_SERVER['DOCUMENT_ROOT']);
    $dompdf->output_html();
    //$dompdf->load_html($html);
    //$dompdf->set_paper("a4", "portrait" );
    //$dompdf->render();
    //$dompdf->stream($filename . ".pdf");
}
?>

In my browser I see the PDF content that I expect to be generated, however if I run this code,

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename, $stream=TRUE) 
{
    require_once("dompdf/dompdf_config.inc.php");

    $dompdf = new DOMPDF();
    $dompdf->set_base_path($_SERVER['DOCUMENT_ROOT']);
    //$dompdf->output_html();
    $dompdf->load_html($html);
    $dompdf->set_paper("a4", "portrait" );
    $dompdf->render();
    $dompdf->stream($filename . ".pdf");
}
?>

I get no repsonse, where I would be expecting to get a download window. Is there a reason for this, I wonder if poorly formatted HTML could the problem?

1

There are 1 answers

1
Ken Cheung On

Your CI plugin code should work on 1.7.x. Here's mind for your reference:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

function pdf_create($html, $filename, $stream=true) {
        require_once("dompdf/dompdf_config.inc.php");
        spl_autoload_register('DOMPDF_autoload');
        $dompdf = new DOMPDF();
        $dompdf->load_html($html);
        $dompdf->render();
        if ($stream) {
                $dompdf->stream($filename);
        } else {
                $CI =& get_instance();
                $CI->load->helper('file');
                write_file($filename, $dompdf->output());
        }
}