简体   繁体   中英

Laravel 5 barryvdh/laravel-dompdf time exceeded

I'm using DOMPDF in my project and I making some tests before I implement in my system but very time I ran the service it show me that 60 seconds time exceeded, First I created a controller:

 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;
    }

then I add the route :

Route::get('pdf', 'PdfController@invoice');

The last part I add is a folder in view with the name pdf and the file 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

I really appreciate if some one can tell me where is my mistake.

Your script executed for more then 60 seconds and was terminated and not related to Laravel but php. The default limit is 60 seconds, stored in php.ini file. To temporarily extend the time limit, you may use following line if code in your current script, but try to optimize your script too (if possible)

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

Read more on php manual.

You can do set_time_limit(0); so that the script will run forever - however this is not recommended and your web server might catch you out with an imposed HTTP timeout (usually around 5 minutes). You may also use

ini_set('max_execution_time', 120);

Check ini_set.

Update

It might be a namespace problem. Try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM