0

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.

nagesh29
  • 56
  • 3
  • 9
  • 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 Sep 25 '14 at 03:13
  • recommend looking through the [dompdf ajax](http://stackoverflow.com/search?q=dompdf+ajax) and [pdf ajax](http://stackoverflow.com/search?q=pdf+ajax) quetions – BrianS Sep 25 '14 at 03:16
  • But if you *really* want to do this via AJAX: http://stackoverflow.com/q/16086162/264628 – BrianS Sep 25 '14 at 03:17

1 Answers1

0

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');
tudo75
  • 97
  • 1
  • 2
  • I am getting pdf response in ajax success function with no errors. Also, when I try to save file on server it works perfect. But I want dialogue box which downloads the pdf. – nagesh29 Sep 23 '14 at 12:57