-->

Generating PDF with DOMPDF with codeigniter on but

2019-08-30 14:14发布

问题:

I have a question regarding PDF creation with DOMPDF. Basically I have a form that submits data to a DB with jquery that looks like below

$('#submit').click(function(){
var submit = $.ajax({
    url: 'setController.php',
    type: 'POST',
    data: dataString,
    success: function(msg){
        alert("sent");

        },
        error: function(msg){
    alert("fail");
        }
    });
});

The setController calls my setModel which returns a render of my html after it has submited the data in to the DB. Which looks like this.

$html = $this -> setModel  -> setData($arr1,$arr2);

This returns a simple html table.

Then I push this down to dompdf as follows

    $this->load->helper(array('dompdf', 'file'));
    pdf_create($html, 'report');

My issue is when the button with ID #submit is clicked no pdf is returned as a download. However if I was to navigate directly to my url

"http://www.url/setData"

I am returns with a pdf download with no data in it.

Can anyone with causing a pdf to be downloaded once the Submit button is clicked?

Thank you.

回答1:

Is your desired interaction for the user to submit the form then be presented with a download dialog for the PDF? By using AJAX to manage the process you won't be offered the opportunity to download the PDF since the AJAX process makes the call and receives the response. This all within the context of the current page.

You could capture the response then open a new window and populate it with your PDF. But I'd advise against it unless you have a true need. Don't make the process harder than it needs to be. The easiest method is to make a normal POST request instead of an AJAX POST request. Typically the user won't ever leave the current page because the download process will cancel page navigation.

If you're intent on doing this using AJAX, there are lots of related questions.