2014-09-04 32 views
1

一個網站我正在開發基於客戶端輸入的PDF格式,我已經決定一旦購買完成那就開始生成pdf。原因是pdf可能會達到50/60MB,我不希望客戶必須等待完成。來自Ajax的php腳本在瀏覽器關閉後停止加載

我使用Opencart和結帳成功頁面我有一個Ajax命令加載PHP腳本。

我的印象是用戶可以在腳本啓動後關閉頁面,但由於某種原因,我發現如果我希望文件出現在我的服務器上,我必須等待5/10秒才能關閉頁面。

作爲測試的一部分,它有點難以調試,需要立即關閉瀏覽器。

這是我的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) {} 
     }); 
    }); 

和我的PHP(目前進出口裝上它巨大的圖像來表示加載時間)

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'); 


} 

編輯:

只是增加更多信息,我已閱讀這些文章,看到的迴應試圖,

所以在t他提交了AJAX的香港專業教育學院設立..

ini_set('ignore_user_abort',true); 

,並在Ajax調用伊夫組功能...

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

對於第一個我跑的phpinfo(),它沒有確認對我而言,ignore_user_abort已啓用,意思很明顯,該部分不是問題。

+0

一些光睡前閱讀的主題:) http://stackoverflow.com/questions/4806637/continue-processing-after-closing-connection http://stackoverflow.com/questions/8548419/most-simple-way開始一個新的進程線程在PHP http://stackoverflow.com/questions/138374/close-a-connection-early – 2014-09-04 12:16:13

+0

這篇文章可能也是有益的/信息:[PHP的執行停止用戶離開頁面後?](http://stackoverflow.com/questions/1280291/does-php-execution-stop-after-a-user-leaves-the-page) – Lix 2014-09-04 12:17:19

+0

這取決於您的設置。 這個[主題] [1]可能會有所幫助。 [1]:http://stackoverflow.com/questions/1280291/does-php-execution-stop-after-a-user-leaves-the-page – 2014-09-04 12:34:35

回答

0

使用ignore_user_abort(true)即使用戶關閉頁面,腳本仍然保持運行。

+0

感謝,不幸的是它不工作,但我認爲它的一個主機問題,當我運行'phpinfo()'它說ignore_user_abort關 – Adrian 2014-09-04 13:28:46

+0

我試着改變它我的php.ini文件,但沒有運氣 – Adrian 2014-09-04 13:30:20

相關問題