2016-02-12 64 views
0

我在我的項目中使用DOMPDF,並且在我的系統中實現了一些測試之前,我已經運行了服務,它告訴我超過60秒的時間, 創建控制器:Laravel 5 barryvdh/laravel-dompdf time exceeded

public function invoice() 
    { 
     $data = $this->getData(); 
     $date = date('Y-m-d'); 
     $invoice = "2222"; 
     $view = \View::make('pdf.invoice', compact('data', 'date', 'invoice'))->render(); 
     $pdf = \App::make('dompdf.wrapper'); 
     $pdf->loadHTML($view); 
     return $pdf->stream('invoice'); 
    } 

    public function getData() 
    { 
     $data = [ 
      'quantity'  => '1' , 
      'description' => 'some ramdom text', 
      'price' => '500', 
      'total'  => '500' 
     ]; 
     return $data; 
    } 

然後我添加路由:

Route::get('pdf', '[email protected]'); 

最後一部分我補充的是在視圖中的文件夾名稱的PDF和文件invoice.blade.php:

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
    <title>Example 2</title> 
    <link rel="stylesheet" href="{{asset('plugins/bootstrap/css/bootstrap.css')}}"> 
    </head> 
    <body> 

    <main> 
     <div id="details" class="clearfix"> 
     <div id="invoice"> 
      <h1>INVOICE {{ $invoice }}</h1> 
      <div class="date">Date of Invoice: {{ $date }}</div> 
     </div> 
     </div> 
     <table border="0" cellspacing="0" cellpadding="0"> 
     <thead> 
      <tr> 
      <th class="no">#</th> 
      <th class="desc">DESCRIPTION</th> 
      <th class="unit">UNIT PRICE</th> 
      <th class="total">TOTAL</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr> 
      <td class="no">{{ $data['quantity'] }}</td> 
      <td class="desc">{{ $data['description'] }}</td> 
      <td class="unit">{{ $data['price'] }}</td> 
      <td class="total">{{ $data['total'] }} </td> 
      </tr> 

     </tbody> 
     <tfoot> 
      <tr> 
      <td colspan="2"></td> 
      <td >TOTAL</td> 
      <td>$6,500.00</td> 
      </tr> 
     </tfoot> 
     </table> 
    </body> 
    <script src="{{asset('plugins/jquery/js/jquery-2.1.4.js')}}"></script> 
    <script src="{{asset('plugins/bootstrap/js/bootstrap.js')}}" ></script> 
</html> 

URL:http://localhost:8000/pdf

我真的很感激,如果有人能告訴我我的錯誤在哪裏。

+0

你確定你正確設置laravel-dompdf嗎?爲什麼不使用'PDF :: loadView('pdf.invoice',$ data)''就像文檔中建議的那樣? – phaberest

回答

0

您的腳本執行超過60秒,並被終止,並且與Laravel無關,但與php相關。默認限制是60秒,存儲在php.ini文件中。暫時延長時間限制,你可以使用以下行,如果代碼在當前的腳本,但嘗試過優化您的腳本(如果可能)

set_time_limit(120); //120 seconds = 2 minute 

Read more on php manual.

你可以做的set_time_limit(0 );所以腳本將永遠運行 - 但是,這是不推薦的,你的web服務器可能會用強制的HTTP超時(通常大約5分鐘)趕上你 。您可能也 使用

ini_set('max_execution_time', 120); 

Check ini_set.

更新

這可能是一個命名空間的問題。試試這個

$pdf = PDF::loadHTML('your view here '); 
    return $pdf->stream(); 
+0

我不確定這是否是真正的問題,我認爲腳本花費太多時間在單個陣列上執行任務。 – phaberest

+0

是的,這需要太多時間,這就是爲什麼你需要增加腳本的max_execution_time。 – Drudge

+0

我正在檢查文件php.ini,我發現這個,max_execution_time = 120。 –