Generating PDF with DOMPDF with codeigniter on button click after submission of data in to database

时光毁灭记忆、已成空白 提交于 2019-12-25 12:55:30

问题


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.



来源:https://stackoverflow.com/questions/17662438/generating-pdf-with-dompdf-with-codeigniter-on-button-click-after-submission-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!