title: Laravel DOMPDF exceed memory limit
body: <p>I have an issue with Laravel DOMPDF package.
  I've set the php.ini memory limit to 2G and launched a queue with --timeout=3600.
  The script is a foreach with a helper function called every times, it produces pdfs for 7/8 minutes, than it stops doing it and after 10 minutes it returns in the terminal</p>
  <pre><code>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(&quot;@&quot;, $length) . &quot;/$ref_size&quot;;
     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(&quot;@&quot;, $length) . &quot;/$ref_size&quot;;
     580▕             if (isset($cache[$key])) {
     581▕                 return $cache[$key];
  
       +1 vendor frames 
   2   [internal]:0
       Whoops\Run::handleShutdown()
  
  </code></pre>
  <p>I'm using tables and I've read that it may be for that reason.
  Thanks to anyone who could help me.</p>
  <pre><code>&lt;?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-&gt;process_id = $process_id;
      }
  
      /**
       * Execute the job.
       *
       * @return void
       */
      public function handle()
      {
          
          $process = Process::find($this-&gt;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-&gt;id)-&gt;where('district', '=', $province['code'])-&gt;get();
            $movements = $movements-&gt;toArray();
  
            if(!empty($movements))
            {
                //passo il risultato alla funzione che fa il pdf
                //GENERATEPDFHELPER
  
                $test = new GeneratePdfHelper($movements);
                $test-&gt;create_pdf();
                
            }         
        }
        //Update the record in DB
          $process-&gt;update(['is_pdf_generated' =&gt; 1]);
      }
  }
  
  </code></pre>
  <pre><code>&lt;?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-&gt;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-&gt;movements);
          //totale ritenute e ruolo
          $amount = 0; // ritenute
          $roles = array(); //ruoli
  
              foreach($this-&gt;movements as $key =&gt; $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' =&gt; $this-&gt;movements, 'summary' =&gt; $summary])-&gt;setPaper('a4', 'landscape');
              $content = $pdf-&gt;download()-&gt;getOriginalContent();
              Storage::put(&quot;processes/{$this-&gt;movements[0]['process_id']}/{$this-&gt;movements[0]['district']}.pdf&quot;, $content);
       
            }        
  
          }
      }
  
  
  </code></pre>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;it-IT&quot;&gt;
  &lt;head&gt;
  &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
  &lt;title&gt;Titolo&lt;/title&gt;
  &lt;style&gt;
      @page{size:29.7cm 21cm; margin:0px}
      body{margin:0px}
      table {
          table-layout: fixed;
      }
      
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body leftmargin=&quot;0&quot; marginwidth=&quot;0&quot; topmargin=&quot;0&quot; marginheight=&quot;0&quot; offset=&quot;0&quot; style=&quot;padding: 0;&quot;&gt;
          &lt;div id=&quot;wrapper&quot; style=&quot;background-color: #ffffff; margin: 0; padding: 10px 0; width: 100%; -webkit-text-size-adjust: none;&quot;&gt;
              &lt;div cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot;&gt;
                  &lt;!-- Header --&gt;
                  &lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot; id=&quot;template_header&quot; style='display: inline-table; background-color: #d4d4d4; color: #000; border-bottom: 0; line-height: 100%; vertical-align: middle; font-family: &quot;Helvetica Neue&quot;, Helvetica, Roboto, Arial, sans-serif; border-radius: 3px;'&gt;
                      &lt;tr&gt;
                          &lt;td style=&quot;padding: 25px;&quot;&gt;
                              &lt;h1 style='font-family: &quot;Helvetica Neue&quot;, Helvetica, Roboto, Arial, sans-serif; font-size: 35px; line-height: 100%; margin: 0; text-align: left; color: #000; background-color: inherit;'&gt;&lt;strong&gt;{{$movements[0]['district']}}&lt;/strong&gt;&lt;/h1&gt;
                          &lt;/td&gt;
                          &lt;td style=&quot;padding: 25px;&quot;&gt;
                              &lt;div style='font-family: &quot;Helvetica Neue&quot;, Helvetica, Roboto, Arial, sans-serif; font-size: 25px; line-height: 100%; margin: 0; text-align: right; color: #6b6b6b; background-color: inherit; font-style: italic;'&gt;(Mese di confronto: {{$movements[0]['month_payment']}}/{{$movements[0]['year_payment']}})&lt;/div&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/table&gt;
                  &lt;!-- End Header --&gt;
  
  
                  &lt;!-- Middle --&gt;
                  &lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot; id=&quot;template_middle&quot; style='display: inline-table; background-color: #ffffff; color: #000; border-bottom: 0; line-height: 100%; vertical-align: middle; font-family: &quot;Helvetica Neue&quot;, Helvetica, Roboto, Arial, sans-serif; border-radius: 3px;'&gt;
                      &lt;tr&gt;
                          &lt;td style=&quot;padding: 25px; width: 50%;  vertical-align: top;&quot;&gt;
                              &lt;div style='font-family: &quot;Helvetica Neue&quot;, Helvetica, Roboto, Arial, sans-serif; font-size: 20px; line-height: 100%; margin: 0; text-align: left; color: #000; background-color: inherit;'&gt;
                                  &lt;div style=&quot;padding: 8px 0;&quot;&gt;
                                      &lt;strong&gt;Iscritti:&lt;/strong&gt; {{$summary['count']}}
                                  &lt;/div&gt;
                                  &lt;div style=&quot;padding: 8px 0;&quot;&gt;
                                      &lt;strong&gt;Nuove iscrizioni:&lt;/strong&gt; ?
                                  &lt;/div&gt;
                              &lt;/div&gt;
  
                              &lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot; style='display: inline-table; background-color: #eaeaea; color: #000; border-bottom: 0; line-height: 100%; vertical-align: middle; font-family: &quot;Helvetica Neue&quot;, Helvetica, Roboto, Arial, sans-serif; border-radius: 3px; margin-top: 10px;'&gt;
                                  &lt;tr&gt;
                                      &lt;td style=&quot;padding: 15px;&quot;&gt;
                                          &lt;strong&gt;Ritenute nel mese:&lt;/strong&gt; ?
                                      &lt;/td&gt;
                                      &lt;td style=&quot;padding: 15px;&quot;&gt;
                                          &lt;strong&gt;Riattivazioni:&lt;/strong&gt; ?
                                      &lt;/td&gt;
                                      &lt;td style=&quot;padding: 15px;&quot;&gt;
                                          &lt;strong&gt;Cessazioni:&lt;/strong&gt; ?
                                      &lt;/td&gt;
                                  &lt;/tr&gt;
                              &lt;/table&gt;
  
                              &lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot; style='display: inline-table; background-color: #ffffff; color: #000; border-bottom: 0; line-height: 100%; vertical-align: middle; font-family: &quot;Helvetica Neue&quot;, Helvetica, Roboto, Arial, sans-serif; border-radius: 3px; margin-top: 10px;'&gt;
                                  &lt;tr&gt;
                                      &lt;td style=&quot;width: 70%; padding-right: 5px;&quot;&gt;
                                          &lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot; style='display: inline-table; background-color: #eaeaea; color: #000; border-bottom: 0; line-height: 100%; vertical-align: middle; font-family: &quot;Helvetica Neue&quot;, Helvetica, Roboto, Arial, sans-serif; border-radius: 3px; padding: 15px;'&gt;
                                              &lt;tr&gt;
                                                  &lt;td style=&quot;padding: 0 0 10px;&quot;&gt;
                                                      &lt;strong&gt;TOTALE (ritenute rimborsi):&lt;/strong&gt;
                                                  &lt;/td&gt;
                                                  &lt;td style=&quot;padding: 0 0 10px; text-align: right;&quot;&gt;
                                                      {{$summary['amount']}}
                                                  &lt;/td&gt;
                                              &lt;/tr&gt;
                                              &lt;tr&gt;
                                                  &lt;td style=&quot;padding: 0 0 10px;&quot;&gt;
                                                      &lt;strong&gt;Quota al Nazionale:&lt;/strong&gt;
                                                  &lt;/td&gt;
                                                  &lt;td style=&quot;padding: 0 0 10px; text-align: right;&quot;&gt;
                                                      ?
                                                  &lt;/td&gt;
                                              &lt;/tr&gt;
                                              &lt;tr&gt;
                                                  &lt;td style=&quot;padding: 0 0 10px;&quot;&gt;
                                                      &lt;strong&gt;NETTO per la Sede prov.:&lt;/strong&gt;
                                                  &lt;/td&gt;
                                                  &lt;td style=&quot;padding: 0 0 10px; text-align: right;&quot;&gt;
                                                      €355,62
                                                  &lt;/td&gt;
                                              &lt;/tr&gt;
                                              
                                          &lt;/table&gt;
                                      &lt;/td&gt;
                                      &lt;td style=&quot;width: 30%; padding-left: 5px; vertical-align: top;&quot;&gt;
                                          &lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot; style='display: inline-table; background-color: #eaeaea; color: #000; border-bottom: 0; line-height: 100%; vertical-align: middle; font-family: &quot;Helvetica Neue&quot;, Helvetica, Roboto, Arial, sans-serif; border-radius: 3px; padding: 15px;'&gt;
                                              &lt;tr&gt;
                                                  &lt;td style=&quot;padding: 0 0 10px;&quot;&gt;
                                                      &lt;strong&gt;ATA:&lt;/strong&gt;
                                                  &lt;/td&gt;
                                                  &lt;td style=&quot;padding: 0 0 10px; text-align: right;&quot;&gt;
                                                      16
                                                  &lt;/td&gt;
                                              &lt;/tr&gt;
                                              &lt;tr&gt;
                                                  &lt;td style=&quot;padding: 0 0 10px;&quot;&gt;
                                                      &lt;strong&gt;DOCENTI:&lt;/strong&gt;
                                                  &lt;/td&gt;
                                                  &lt;td style=&quot;padding: 0 0 10px; text-align: right;&quot;&gt;
                                                      33
                                                  &lt;/td&gt;
                                              &lt;/tr&gt;
                                              &lt;tr&gt;
                                                  &lt;td style=&quot;padding: 0 0 10px;&quot;&gt;
                                                      &lt;strong&gt;ITP:&lt;/strong&gt;
                                                  &lt;/td&gt;
                                                  &lt;td style=&quot;padding: 0; text-align: right;&quot;&gt;
                                                      0
                                                  &lt;/td&gt;
                                              &lt;/tr&gt;
                                          &lt;/table&gt;
                                          &lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot; style='display: inline-table; background-color: #eaeaea; color: #000; border-bottom: 0; line-height: 100%; vertical-align: middle; font-family: &quot;Helvetica Neue&quot;, Helvetica, Roboto, Arial, sans-serif; border-radius: 3px; padding: 15px; margin-top: 10px;'&gt;
                                              &lt;tr&gt;
                                                  &lt;td style=&quot;padding: 0 0 10px;&quot;&gt;
                                                      &lt;strong&gt;Precari:&lt;/strong&gt;
                                                  &lt;/td&gt;
                                                  &lt;td style=&quot;padding: 0 0 10px; text-align: right;&quot;&gt;
                                                      5
                                                  &lt;/td&gt;
                                              &lt;/tr&gt;
                                              &lt;tr&gt;
                                                  &lt;td style=&quot;padding: 0 0 10px;&quot;&gt;
                                                      &lt;strong&gt;Pensionandi:&lt;/strong&gt;
                                                  &lt;/td&gt;
                                                  &lt;td style=&quot;padding: 0 0 10px; text-align: right;&quot;&gt;
                                                      0
                                                  &lt;/td&gt;
                                              &lt;/tr&gt;
                                          &lt;/table&gt;
                                      &lt;/td&gt;
                                  &lt;/tr&gt;
                              &lt;/table&gt;
                          &lt;/td&gt;
                          &lt;td style=&quot;padding: 25px; width: 50%; vertical-align: top;&quot;&gt;
                              &lt;div style='font-family: &quot;Helvetica Neue&quot;, 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;'&gt;
                                  &lt;p style=&quot;margin: 0 0 20px;&quot;&gt;
                                      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.
                                  &lt;/p&gt;
                                  &lt;p style=&quot;margin: 0 0 20px;&quot;&gt;
                                      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.
                                  &lt;/p&gt;
                              &lt;/div&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/table&gt;
                  &lt;!-- End Middle --&gt;
  
  
                      &lt;!-- Righe nominativi --&gt;
                      &lt;div style='font-family: &quot;Helvetica Neue&quot;, Helvetica, Roboto, Arial, sans-serif; font-size: 20px; line-height: 100%; margin: 0; text-align: left; color: #000; background-color: inherit;'&gt;
                      &lt;strong&gt;1 - RITENUTE NEL Mese&lt;/strong&gt;
                      &lt;/div&gt;
                              &lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot; id=&quot;template_middle&quot; style='display: inline-table; background-color: #ffffff; color: #000; line-height: 100%; vertical-align: middle; font-family: &quot;Helvetica Neue&quot;, Helvetica, Roboto, Arial, sans-serif; border-radius: 3px; font-size: 11px; text-transform: uppercase; text-align: left;'&gt;
                                  &lt;tr style=&quot;background-color: #ffffff; font-style: italic;&quot;&gt;
                                      &lt;th style=&quot;padding: 10px; border-bottom: 1px solid;  width: 10px;&quot;&gt;
                                          Partita
                                      &lt;/th&gt;
                                      &lt;th style=&quot;padding: 10px; border-bottom: 1px solid;&quot;&gt;
                                          SS
                                      &lt;/th&gt;
                                      &lt;th style=&quot;padding: 10px; border-bottom: 1px solid;&quot;&gt;
                                          Eta'
                                      &lt;/th&gt;
                                      &lt;th style=&quot;padding: 10px; border-bottom: 1px solid;&quot;&gt;
                                          Nominativo
                                      &lt;/th&gt;
                                      &lt;th style=&quot;padding: 10px; border-bottom: 1px solid;&quot;&gt;
                                          Qualifica
                                      &lt;/th&gt;
                                      &lt;th style=&quot;padding: 10px; border-bottom: 1px solid;&quot;&gt;
                                          Sede di servizio
                                      &lt;/th&gt;
                                      &lt;th style=&quot;padding: 10px; border-bottom: 1px solid;&quot;&gt;
                                          Tel sede
                                      &lt;/th&gt;
                                      &lt;th style=&quot;padding: 10px; border-bottom: 1px solid;&quot;&gt;
                                          Fax sede
                                      &lt;/th&gt;
                                      &lt;th style=&quot;padding: 10px; border-bottom: 1px solid;&quot;&gt;
                                          Scadenze
                                      &lt;/th&gt;
                                  &lt;/tr&gt;
                                  @foreach ($movements as $movement)
                                              &lt;tr style=&quot;background-color: #ffffff;&quot;&gt;
                                                  &lt;td style=&quot;padding: 10px;&quot;&gt;
                                                      {{$movement['receipt_id']}}
                                                  &lt;/td&gt;
                                                  &lt;td style=&quot;padding: 10px;&quot;&gt;
                                                      M
                                                  &lt;/td&gt;
                                                  &lt;td style=&quot;padding: 10px;&quot;&gt;
                                                      54
                                                  &lt;/td&gt;
                                                  &lt;td style=&quot;padding: 10px;&quot;&gt;
                                                      {{$movement['name_lastname']}}
                                                  &lt;/td&gt;
                                                  &lt;td style=&quot;padding: 10px;&quot;&gt;
                                                      {{$movement['position']}}
                                                  &lt;/td&gt;
                                                  &lt;td style=&quot;padding: 10px;&quot;&gt;
                                                      ANIC83300cc Senigallia Marchetti
                                                  &lt;/td&gt;
                                                  &lt;td style=&quot;padding: 10px;&quot;&gt;
                                                      0717922289
                                                  &lt;/td&gt;
                                                  &lt;td style=&quot;padding: 10px;&quot;&gt;
                                                      07165487
                                                  &lt;/td&gt;
                                                  &lt;td style=&quot;padding: 10px;&quot;&gt;
                                                      202106 scadenza contratto
                                                  &lt;/td&gt;
                                              &lt;/tr&gt;
                                              @endforeach
                              &lt;/table&gt;
                      &lt;!-- End Righe nominativi --&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  </code></pre>