1

A site Im working on builds a pdf based on the clients input, I have decided that once the purchase is complete thats when the pdf will begin to get generated. The reason is the pdf may be up tp 50/60MB and I dont want the client to have to wait for this to complete.

Im using Opencart and on the checkout success page I have an Ajax command loading a PHP script.

I was under the impression that the user could close the page once the script starts, but for some reason I find I have to wait for 5/10 seconds before closing if I want the file to appear on my server.

Its a little difficult to debug as part of testing involves closing the browser immediately.

Here is my Ajax...

$(document).ready(function() {  

    $.ajax({
        url: 'index.php?route=pdfengine/pdfengine/generate_final_pdf',
        type: 'post',
        dataType: 'json',
        data: {     
        },
        beforeSend: function() {},
        complete: function() {},
        success: function(json) {}
        });
    });

and my php (currently Im loading huge images on it to represent the load time)

 public function generate_final_pdf() {

    include('convert-to-pdf/mpdf.php');

    $mpdf=new mPDF();


    $stylesheet = file_get_contents(DIR_APPLICATION.'view/theme/rascal/stylesheet/stylesheet.css');
    $mpdf->WriteHTML($stylesheet,1);
    $mpdf->WriteHTML('<html><body><div class="pdf-test-style"><img src="http://example.com/top_quality_image.jpg" />test</div><pagebreak><img src="http://example.com/top_quality_image2.jpg" />test<pagebreak><img src="http://example.com/top_quality_image3.jpg" />test</body></html>',2);           

    $mpdf->Output(DIR_FINAL_PDFS.'order_idpdfid-'.$this->session->data['pdf_id'].'.pdf','F');


 }

EDIT:

Just adding more info that Ive tried since reading those articles and seeing the responses,

So on the file where the ajax is Ive set..

ini_set('ignore_user_abort',true);

and in the function that the ajax calls Ive set...

ini_set('ignore_user_abort',true);   
set_time_limit(0);

For the first one I ran phpinfo() and it did confirm to me that ignore_user_abort was on, meaning obviously that part isnt an issue.

Adrian
  • 1,976
  • 5
  • 41
  • 105
  • Some light bedtime reading around the topic :) http://stackoverflow.com/questions/4806637/continue-processing-after-closing-connection http://stackoverflow.com/questions/8548419/most-simple-way-to-start-a-new-process-thread-in-php http://stackoverflow.com/questions/138374/close-a-connection-early – Rob Schmuecker Sep 04 '14 at 12:16
  • This post might also be helpful/informative: [Does php execution stop after a user leaves the page?](http://stackoverflow.com/questions/1280291/does-php-execution-stop-after-a-user-leaves-the-page) – Lix Sep 04 '14 at 12:17
  • It depends on your settings. This [topic][1] might be helpful. [1]: http://stackoverflow.com/questions/1280291/does-php-execution-stop-after-a-user-leaves-the-page – Omran Shagooj Sep 04 '14 at 12:34
  • Thanks for the links, they make sense – Adrian Sep 04 '14 at 13:26
  • Hi all, Ive made an edit to the question including what Ive learned from those links, Im wondering is there anything else that can contribute to the problem of a script failing to continue to run after the window closes? – Adrian Sep 04 '14 at 16:12

1 Answers1

0

Use ignore_user_abort(true) to keep the script running even if the user closes the page.

JoelP
  • 439
  • 1
  • 3
  • 13
  • Thanks, unfortunately its not working, but I think its a hosting issue, when I run `phpinfo()` it says ignore_user_abort Off – Adrian Sep 04 '14 at 13:28