0
votes

I am using dompdf library to convert my html content to pdf.
I am sending html content from view to controller with ajax post request.
In this controller, I am calling dompdf helper class to convert my html to pdf file and save it on client side.

Here is my ajax request:

$.ajax({  
type: "POST",
url: "<?php echo $this->config->base_url('reports/exportPDF')?>",
data:
{
report : totalHtml
},
success: function (response) {
console.log(response);
},     
error: function (err) {
//console.log(err);
            }
});

Here is my controller code where I am submitting my html content:

$this->load->helper('url');
$this->load->helper(array('dompdf', 'file'));
$html = $this->input->post('report');
$data = pdf_create($html);

Finally, Here is my dompdf helper library integration 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();

    $htmlData = $dompdf->load_html($html);
    $dompdf->set_paper("a4", "landscape" ); 
    $dompdf->render();
    $dompdf->stream("report.pdf");

}
?>

As per dompdf documentation, the download dialogue box should appear but I am unable to get it.
How should I save the pdf file on client side with dompdf?

Thanks in advance.

1
It's non-trivial to do what you're trying to do via AJAX. Instead you should just submit to "reports/exportPDF" normally (create a hidden form if you need to post content to the server).BrianS
recommend looking through the dompdf ajax and pdf ajax quetionsBrianS
But if you really want to do this via AJAX: stackoverflow.com/q/16086162/264628BrianS

1 Answers

0
votes

What happens when you send ajax call? File open inside page or have errors? I usually send response to a view opening a new window where I set header to binary file that force browser to open save dialog

$this->output->set_header('Content-Type: application/octet-stream');