Laravel DOMPDF 超出内存限制

Laravel DOMPDF exceed memory limit

我对 Laravel DOMPDF 包有疑问。 我已将 php.ini 内存限制设置为 2G,并使用 --timeout=3600 启动了一个队列。 该脚本是一个每次都调用辅助函数的 foreach,它生成 pdf 7/8 分钟,然后停止执行,10 分钟后它在终端 returns

Symfony\Component\ErrorHandler\Error\FatalError 

 Allowed memory size of 2147483648 bytes exhausted (tried to allocate 20480 bytes)

 at vendor/dompdf/dompdf/src/Css/Style.php:577
   573▕             //Early check on cache, before converting $length to array
   574▕             if (isset($cache[$key])) {
   575▕                 return $cache[$key];
   576▕             }
 ➜ 577▕             $length = [$length];
   578▕         } else {
   579▕             $key = implode("@", $length) . "/$ref_size";
   580▕             if (isset($cache[$key])) {
   581▕                 return $cache[$key];


  Whoops\Exception\ErrorException 

 Allowed memory size of 2147483648 bytes exhausted (tried to allocate 20480 bytes)

 at vendor/dompdf/dompdf/src/Css/Style.php:577
   573▕             //Early check on cache, before converting $length to array
   574▕             if (isset($cache[$key])) {
   575▕                 return $cache[$key];
   576▕             }
 ➜ 577▕             $length = [$length];
   578▕         } else {
   579▕             $key = implode("@", $length) . "/$ref_size";
   580▕             if (isset($cache[$key])) {
   581▕                 return $cache[$key];

     +1 vendor frames 
 2   [internal]:0
     Whoops\Run::handleShutdown()

我正在使用表格,我读到可能是因为这个原因。 感谢任何能帮助我的人。

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
//helper
use App\Helpers\GeneratePdfHelper;

//models
use App\Models\Province;
use App\Models\Process;
use App\Models\Movement;
//others
use PDF;
use Illuminate\Support\Facades\Storage;

class GeneratePdfFiles implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $process_id;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($process_id)
    {
        $this->process_id = $process_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        
        $process = Process::find($this->process_id);
      //look for all provinces
      $provinces = Province::all();
      //cicle them
      foreach($provinces as $province)
      {
          //questa query mi dà una collection con tutti i movimenti per quel mese/anno/provincia
          $movements = Movement::where('process_id', '=', $process->id)->where('district', '=', $province['code'])->get();
          $movements = $movements->toArray();

          if(!empty($movements))
          {
              //passo il risultato alla funzione che fa il pdf
              //GENERATEPDFHELPER

              $test = new GeneratePdfHelper($movements);
              $test->create_pdf();
              
          }         
      }
      //Update the record in DB
        $process->update(['is_pdf_generated' => 1]);
    }
}

<?php
namespace app\Helpers; // Your helpers namespace 

use PDF;
use Illuminate\Support\Facades\Storage;


if(!class_exists('GeneratePdfHelper')){

    class GeneratePdfHelper
    {
        public $movements;

        public function __construct(array $movements){
            $this->movements = $movements;
        }

        public function create_pdf(){
        
        //contatore per vedere quanti record ci sono per i movimenti di quel processo per quella provincia
        $count = count($this->movements);
        //totale ritenute e ruolo
        $amount = 0; // ritenute
        $roles = array(); //ruoli

            foreach($this->movements as $key => $movement){
                $amount = $amount + $movement['amount'];
                if(!array_key_exists($movement['position'], $roles)){
                    $roles[$movement['position']] = 0;
                    $roles[$movement['position']] = $roles[$movement['position']] + 1;
                } else {
                    $roles[$movement['position']] = $roles[$movement['position']] + 1;
                }
            }

            $summary = array();
            $summary['amount'] = $amount;
            $summary['count'] = $count;
            $summary['roles'] = $roles;

            $pdf = PDF::loadView('pdf.index', ['movements' => $this->movements, 'summary' => $summary])->setPaper('a4', 'landscape');
            $content = $pdf->download()->getOriginalContent();
            Storage::put("processes/{$this->movements[0]['process_id']}/{$this->movements[0]['district']}.pdf", $content);
     
          }        

        }
    }


<!DOCTYPE html>
<html lang="it-IT">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Titolo</title>
<style>
    @page{size:29.7cm 21cm; margin:0px}
    body{margin:0px}
    table {
        table-layout: fixed;
    }
    
</style>
</head>
<body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0" style="padding: 0;">
        <div id="wrapper" style="background-color: #ffffff; margin: 0; padding: 10px 0; width: 100%; -webkit-text-size-adjust: none;">
            <div cellpadding="0" cellspacing="0" width="100%">
                <!-- Header -->
                <table cellpadding="0" cellspacing="0" width="100%" id="template_header" style='display: inline-table; background-color: #d4d4d4; color: #000; border-bottom: 0; line-height: 100%; vertical-align: middle; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; border-radius: 3px;'>
                    <tr>
                        <td style="padding: 25px;">
                            <h1 style='font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; font-size: 35px; line-height: 100%; margin: 0; text-align: left; color: #000; background-color: inherit;'><strong>{{$movements[0]['district']}}</strong></h1>
                        </td>
                        <td style="padding: 25px;">
                            <div style='font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; font-size: 25px; line-height: 100%; margin: 0; text-align: right; color: #6b6b6b; background-color: inherit; font-style: italic;'>(Mese di confronto: {{$movements[0]['month_payment']}}/{{$movements[0]['year_payment']}})</div>
                        </td>
                    </tr>
                </table>
                <!-- End Header -->


                <!-- Middle -->
                <table cellpadding="0" cellspacing="0" width="100%" id="template_middle" style='display: inline-table; background-color: #ffffff; color: #000; border-bottom: 0; line-height: 100%; vertical-align: middle; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; border-radius: 3px;'>
                    <tr>
                        <td style="padding: 25px; width: 50%;  vertical-align: top;">
                            <div style='font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; font-size: 20px; line-height: 100%; margin: 0; text-align: left; color: #000; background-color: inherit;'>
                                <div style="padding: 8px 0;">
                                    <strong>Iscritti:</strong> {{$summary['count']}}
                                </div>
                                <div style="padding: 8px 0;">
                                    <strong>Nuove iscrizioni:</strong> ?
                                </div>
                            </div>

                            <table cellpadding="0" cellspacing="0" width="100%" style='display: inline-table; background-color: #eaeaea; color: #000; border-bottom: 0; line-height: 100%; vertical-align: middle; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; border-radius: 3px; margin-top: 10px;'>
                                <tr>
                                    <td style="padding: 15px;">
                                        <strong>Ritenute nel mese:</strong> ?
                                    </td>
                                    <td style="padding: 15px;">
                                        <strong>Riattivazioni:</strong> ?
                                    </td>
                                    <td style="padding: 15px;">
                                        <strong>Cessazioni:</strong> ?
                                    </td>
                                </tr>
                            </table>

                            <table cellpadding="0" cellspacing="0" width="100%" style='display: inline-table; background-color: #ffffff; color: #000; border-bottom: 0; line-height: 100%; vertical-align: middle; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; border-radius: 3px; margin-top: 10px;'>
                                <tr>
                                    <td style="width: 70%; padding-right: 5px;">
                                        <table cellpadding="0" cellspacing="0" width="100%" style='display: inline-table; background-color: #eaeaea; color: #000; border-bottom: 0; line-height: 100%; vertical-align: middle; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; border-radius: 3px; padding: 15px;'>
                                            <tr>
                                                <td style="padding: 0 0 10px;">
                                                    <strong>TOTALE (ritenute rimborsi):</strong>
                                                </td>
                                                <td style="padding: 0 0 10px; text-align: right;">
                                                    {{$summary['amount']}}
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="padding: 0 0 10px;">
                                                    <strong>Quota al Nazionale:</strong>
                                                </td>
                                                <td style="padding: 0 0 10px; text-align: right;">
                                                    ?
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="padding: 0 0 10px;">
                                                    <strong>NETTO per la Sede prov.:</strong>
                                                </td>
                                                <td style="padding: 0 0 10px; text-align: right;">
                                                    €355,62
                                                </td>
                                            </tr>
                                            
                                        </table>
                                    </td>
                                    <td style="width: 30%; padding-left: 5px; vertical-align: top;">
                                        <table cellpadding="0" cellspacing="0" width="100%" style='display: inline-table; background-color: #eaeaea; color: #000; border-bottom: 0; line-height: 100%; vertical-align: middle; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; border-radius: 3px; padding: 15px;'>
                                            <tr>
                                                <td style="padding: 0 0 10px;">
                                                    <strong>ATA:</strong>
                                                </td>
                                                <td style="padding: 0 0 10px; text-align: right;">
                                                    16
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="padding: 0 0 10px;">
                                                    <strong>DOCENTI:</strong>
                                                </td>
                                                <td style="padding: 0 0 10px; text-align: right;">
                                                    33
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="padding: 0 0 10px;">
                                                    <strong>ITP:</strong>
                                                </td>
                                                <td style="padding: 0; text-align: right;">
                                                    0
                                                </td>
                                            </tr>
                                        </table>
                                        <table cellpadding="0" cellspacing="0" width="100%" style='display: inline-table; background-color: #eaeaea; color: #000; border-bottom: 0; line-height: 100%; vertical-align: middle; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; border-radius: 3px; padding: 15px; margin-top: 10px;'>
                                            <tr>
                                                <td style="padding: 0 0 10px;">
                                                    <strong>Precari:</strong>
                                                </td>
                                                <td style="padding: 0 0 10px; text-align: right;">
                                                    5
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="padding: 0 0 10px;">
                                                    <strong>Pensionandi:</strong>
                                                </td>
                                                <td style="padding: 0 0 10px; text-align: right;">
                                                    0
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td style="padding: 25px; width: 50%; vertical-align: top;">
                            <div style='font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; line-height: 120%; margin: 0; text-align: left; color: #000; background-color: inherit; font-style: italic; border: 1px solid; padding: 15px;'>
                                <p style="margin: 0 0 20px;">
                                    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                                </p>
                                <p style="margin: 0 0 20px;">
                                    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                                </p>
                            </div>
                        </td>
                    </tr>
                </table>
                <!-- End Middle -->


                    <!-- Righe nominativi -->
                    <div style='font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; font-size: 20px; line-height: 100%; margin: 0; text-align: left; color: #000; background-color: inherit;'>
                    <strong>1 - RITENUTE NEL Mese</strong>
                    </div>
                            <table cellpadding="0" cellspacing="0" width="100%" id="template_middle" style='display: inline-table; background-color: #ffffff; color: #000; line-height: 100%; vertical-align: middle; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; border-radius: 3px; font-size: 11px; text-transform: uppercase; text-align: left;'>
                                <tr style="background-color: #ffffff; font-style: italic;">
                                    <th style="padding: 10px; border-bottom: 1px solid;  width: 10px;">
                                        Partita
                                    </th>
                                    <th style="padding: 10px; border-bottom: 1px solid;">
                                        SS
                                    </th>
                                    <th style="padding: 10px; border-bottom: 1px solid;">
                                        Eta'
                                    </th>
                                    <th style="padding: 10px; border-bottom: 1px solid;">
                                        Nominativo
                                    </th>
                                    <th style="padding: 10px; border-bottom: 1px solid;">
                                        Qualifica
                                    </th>
                                    <th style="padding: 10px; border-bottom: 1px solid;">
                                        Sede di servizio
                                    </th>
                                    <th style="padding: 10px; border-bottom: 1px solid;">
                                        Tel sede
                                    </th>
                                    <th style="padding: 10px; border-bottom: 1px solid;">
                                        Fax sede
                                    </th>
                                    <th style="padding: 10px; border-bottom: 1px solid;">
                                        Scadenze
                                    </th>
                                </tr>
                                @foreach ($movements as $movement)
                                            <tr style="background-color: #ffffff;">
                                                <td style="padding: 10px;">
                                                    {{$movement['receipt_id']}}
                                                </td>
                                                <td style="padding: 10px;">
                                                    M
                                                </td>
                                                <td style="padding: 10px;">
                                                    54
                                                </td>
                                                <td style="padding: 10px;">
                                                    {{$movement['name_lastname']}}
                                                </td>
                                                <td style="padding: 10px;">
                                                    {{$movement['position']}}
                                                </td>
                                                <td style="padding: 10px;">
                                                    ANIC83300cc Senigallia Marchetti
                                                </td>
                                                <td style="padding: 10px;">
                                                    0717922289
                                                </td>
                                                <td style="padding: 10px;">
                                                    07165487
                                                </td>
                                                <td style="padding: 10px;">
                                                    202106 scadenza contratto
                                                </td>
                                            </tr>
                                            @endforeach
                            </table>
                    <!-- End Righe nominativi -->
            </div>
        </div>
</body>
</html>

总的来说,就一份工作的实际成就而言,您的方法似乎是错误的或有点被误解了。作业是简短的 运行 作业,可执行防止 Http 服务器响应缓慢的代码,并且是一种更具可扩展性的解决方案。这不是非黑即白,但总的来说,长 运行 任务更适合命令。

您正在使用 2000 mb 内存,这已经很多了,只要您继续添加模型,它就会更高。相反,创建多个执行相同操作的小作业,每个作业都会自行清理,因此永远不会超过内存限制,同时还能够同时执行多个 pdf's

为单个 Movement 创建工作。

class GeneratePdfFile implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $province;

    public $process_id;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Province $province, $process_id)
    {
        $this->province = $province;
        $this->process_id = $process_id;
    }

    public function handle() {
      $movements = Movement::where('process_id', '=', $process->id)->where('district', '=', $this->province['code'])->get();
      $movements = $movements->toArray();

      if(!empty($movements))
      {
          //passo il risultato alla funzione che fa il pdf
          //GENERATEPDFHELPER

          $test = new GeneratePdfHelper($movements);
          $test->create_pdf();
      }    
    }
}

然后在创建作业时将 Province 循环到那里。

foreach($provinces as $province)
{
    dispatch(new GeneratePdfFile($province, $process_id));
}