Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stever's avatar

DOMPDF - "Maximum function nesting level of '512' reached, aborting!

I am using 'PDF' => Barryvdh\DomPDF\Facade::class, to generate PDFs. In the controller,

             use PDF;
              .........

            $pdf = PDF::loadView('patient.requisition');
            $pdf->save("stevereq.pdf");

I bumped up the nesting level to 16K and am still getting the error.

vagrant@homestead:/etc/php/7.3/mods-available$ more xdebug.ini
zend_extension=xdebug.so
xdebug.remote_enable = 1
xdebug.remote_connect_back = 1
xdebug.remote_port = 9000
xdebug.max_nesting_level = 16384

The error is thrown in:

    /**
     * @return FontMetrics
     */
    public function getFontMetrics()
    {
        return $this->fontMetrics;
    }

I am guessing that there is an infinite loop somewhere but I don't where to look. I don't have a lot invested in this PDF generator so if there is no quick solution can someone recommend a different PDF generator?

thanks steve

0 likes
14 replies
stever's avatar

I went back and read those two posts again more carefully and

set xdebug.max_nesting_level = -1

to disable xdebug. This ran for the same amount of time and errored out with 502 BAD GATEWAY from nginx. It only ran for around 15 seconds so it is not near any of the timeouts on nginx (300 seconds).

laracoft's avatar

@stever

  1. Are you able to run any of the DOMPDF examples?
  2. How are you disabling xdebug? Rename xdebug.ini as xdebug to be certain.
stever's avatar

I changed: xdebug.max_nesting_level = 16384 to xdebug.max_nesting_level = -1 to disable xdebug. Trying your method.

root@homestead:/etc/php/7.3/mods-available# mv xdebug.ini xdebug
root@homestead:/etc/php/7.3/mods-available# systemctl restart php7.3-fpm.service

Good news is that your method of disabling xdebug is better as I progressed further. I then received a memory and an execution timeout message for the PDF generation so I changed the following in php.ini.

vagrant@homestead:/etc/php/7.3/fpm$ sudo vi php.ini
max_execution_time = 120
memory_limit = 768M
root@homestead:/etc/php/7.3/fpm# sudo systemctl restart php7.3-fpm.service

I also changed the execution time in the Controller:

            // Get default limit
            $normalTimeLimit = ini_get('max_execution_time');
            ini_set('max_execution_time', 120);
            $pdf = PDF::loadView('patient.requisition');
            $pdf->save("stevereq.pdf");
            // Restore default limit
            ini_set('max_execution_time', $normalTimeLimit);
            dd($pdf);

The Controller runs for around 40 seconds and then silently dies. The 'dd($pdf)' is never executed. I am suspecting that this is a DOMPDF bug. The 7.3 fpm does not restart as the PID is the same.

laracoft's avatar

@stever

  1. Set memory_limit to - 1 and max_execution_time to - 1 for now.
  2. Can you run any DOMPDF example?
  3. Show full patient.requisition
laracoft's avatar

@stever I used all 3, DOMPDF, FPDF and TCPDF.

Each has its quirks, but it really depends on what you are trying to do.

Right now, I write markdown or HTML and let DOMPDF convert to multilingual PDF, the only quirks it has is mostly CSS issues like table cell alignment etc.

By the way, I use DOMPDF directly. It was a while back, but I recall running into issues with barryvdh's package.

It looks you are trying to do the same, so show the view please.

stever's avatar

I have other PDFs working with DOMPDF. I changed the parameters as outlined:

root@homestead:/etc/php/7.3/fpm# grep max_execution_time php.ini
max_execution_time = -1
root@homestead:/etc/php/7.3/fpm# grep memory_limit php.ini
memory_limit = -1
    

public function requisition($id)
    {
            // Get default limit
            $normalTimeLimit = ini_get('max_execution_time');
            ini_set('max_execution_time', -1);
            $pdf = PDF::loadView('patient.requisition');
            $pdf->save("stevereq.pdf");
            // Restore default limit
            ini_set('max_execution_time', $normalTimeLimit);
            dd($pdf);
            return view('patient.requisition');
    }

This is the requisition.blade.php. Sorry about the mess as I haven't gone through and cleaned it up. Good news is that there is no data in it yet so it is standalone. The layouts.req only has the requirements for jquery and bootstrap.

@extends('layouts.req')

@section('content')
<div><h3 style="align:top">EPISIGN - CAN REQUISITION</h2></div>
<div><hr style="border: 1px solid black;" /></div>
<form class="form-inline">
<div class="container">
  <div class="row" style="overflow:hidden">
    <div class="col-sm-6">
        <div><h4 style="background-color:White">LAB USE ONLY</h4></div>
    <div class="col-sm-12" style="background-color:LightGray;margin-bottom: -99999px; padding-bottom: 99999px;">
        <label for="receivedDate" class="col-sm-4 pull-left" >Received date:</label>
        <div class="form-group">
                <div>
                        <input class="form-control"  type="text" name="receivedDate"/>
                </div>
        </div>
        <div class="form-group">
                <label for="notes" class="pull-left">Notes:</label>
                <div class="col-sm-8">
                        <textarea rows="10" cols="50" class="form-control" name="notes"></textarea>

                </div>
        </div>
    </div>
    </div>
        <div class="col-sm-6">
        <div><h4 style="align:top;background-color:White">PATIENT INFORMATION</h4></div>
        <div class="col-sm-12" style="align:top;background-color:LightGray;margin-bottom: -99999px; padding-bottom: 99999px;">
        <div class="container">
        <div class="row" style="background-color:LightGray;margin-bottom: -99999px; padding-bottom: 99999px;">
        <label for="patientName" class="pull-left">Name:</label>
        <div class="form-group">
                <div>
                        <input class="form-control"  type="text" name="patientName"/>
                </div>
        </div><br>
        <label for="dateOfBirth">Date of Birth:</label>
        <div class="form-group">
                <div>
                        <input class="form-control" style="width:60%" type="text" name="dateOfBirth"/>
                </div>
        </div>
        <label for="sex">Sex: </label>
        <div class="form-group">
                <div class="radio-inline">
                        <input type="radio" name="sex">M</>
                </div>
                <div class="radio-inline">
                        <input type="radio" name="sex">F</>
                </div>
        </div><br>
        <label for="patientId" class="pull-left">Patient Study ID*:</label>
        <div class="form-group">
                <div>
                        <input class="form-control"  style="width:100%" type="text" name="patientId"/>
                </div>
        </div><br>
        <label for="pedigre" class="pull-left">Referring Organization Pedigree/Kindred number:</label>
        <div class="form-group">
                <div>
                        <input class="form-control"  style="width:100%" type="text" name="pedigree"/>
                </div>
        </div><br>
        <label for="arm" class="pull-left">Arm of Study: </label>
        <div class="form-group">
                <div>
                        <select class="form-control"  name="arm">
                                <option selected>Select</option>
                                <option>First Tier</option>
                                <option>Episign Complete</option>
                        </select>
                </div>
        </div>
        <div class="text-wrap" style="width: 45rem;"><small>* Please note that this ID will change after patient is added to EpiSign study database.  LHSC will communicate this change to you.</small></div>
        </div>
    </div>
    </div>
    </div>
  </div>
</div>
<div><hr style="border: 1px solid black;" /></div>
        <div class="container">
        <div class="row" style="overflow:hidden">
        <div><h4>SAMPLE INFORMATION</h4></div>
        <div class="col" style="background-color:LightGray;margin-bottom: -99999px; padding-bottom: 99999px;">
        <label for="test" class="pull-left">Test:</label>
        <div class="form-group">
                <div>
                        <input class="form-control" type="text" name="test"/>
                </div>
        </div>
       <label for="test">Sample ID*:</label>
        <div class="form-group">
                <div>
                        <input class="form-control" type="text" name="test"/>
                </div>
        </div><br>
        <label for="age">Age at time fo sample collection (in years, please use decimal places if necessary):</label>
        <div class="form-group">
                <div>
                        <input class="form-control" type="text" name="age"/>
                </div>
        </div><br>
        <label for="sampleType">Sample Type:</label>
        <div class="form-group">
                <div>
                        <input  type="radio" name="sampleType">Blood sample (5 ml EDTA - lavender top)</>
                </div>
                <div>
                        <input  type="radio" name="sampleType">Banked DNA from peripheral blood</>
                </div>
        </div><br>
        <label for="dna">If DNA, specify concentration if possible (ng/ul):</label>
        <div class="form-group">
                <div>
                        <input class="form-control" type="text" name="dna"/>
                </div>
        </div><br>
<div><small>*Please note that this ID will change after patient is added to EpiSign study database.  LHSC will communication this change to you.</small></div>
        </div>
        </div>
        </div>
</div>
<div><hr style="border: 1px solid black;" /></div>
<div class="container">
        <div><h4>REFERRING PHYSICIAN</h4></div>
        <div class="row" style="overflow:hidden">
        <div class="col-sm-6" style="background-color:LightGray;margin-bottom: -99999px; padding-bottom: 99999px;">
        <div class="form-group">
        <label for="physicianName" class="col-2 ull-left" >Physician Name:<label>
                <div>
                        <input class="form-control" type="text" name="physicianName"/>
                </div>
        </div>
        <div class="form-group">
        <label for="phone" class="col-2 pull-left" >Phone Number:<label>
                <div>
                        <input class="form-control col-4" type="text" name="phone"/>
                </div>
        </div>
        <div class="form-group">
        <label for="fax" class="pull-left col-4" >Fax Number:<label>
                <div>
                        <input class="form-control" type="text" name="fax"/>
                </div>
        </div>
        <div class="form-group">
        <label for="address" class="pull-left col-4" >Address:<label>
                <div>
                        <textarea class="form-control" rows="4" name="address"></textarea>
                </div>
        </div>
        <div class="form-group">
        <label for="signature" class="pull-left col-4" >Signature:<label>
                <div>
                        <textarea class="form-control" rows="2" name="signature"></textarea>
                </div>
        </div>
        </div>
        <div class="col-6">
                <br><br><br><br>
                <div><img src="/img/episign_logo.png" style="margin:auto; display:block;"></div>
        </div>
</div>
</div>
</form>
<div><hr style="border: 1px solid black;" /></div>
<div class="container">
        <div class="row">
                <div class="col-sm-6">
                        <div>Molecular Genetics Laboratory</div>
                        <div>Victoria Hospital, Room B10-128</div>
                        <div>800 Commissioners Rd. E.</div>
                        <div>London, Ontario | N6A 5W9</div>
                        <div>Ph: 519-685-8122 | Fax: 519-685-8279</div>
                </div>
                <div class="col-sm-6">
                        <div><img src="/img/episign_logo.png" class="pull-right img-responsive"></div>
                </div>
        </div>
</div>
</div>
@endsection
laracoft's avatar

@stever DOMPDF is not going to handle javascripts. It might cause issues. Why not start with just plain data first and slowly build up the HTML.

laracoft's avatar

@stever Your HTML looks ok to me. My suggestion is to use DOMPDF directly. Either that or strip out sections of your HTML to narrow down the cause.

I use TCPDF for doing direct drawing. DOMPDF was my winner among the 3 for HTML, so I'm not sure if you are going to find a better candidate.

stever's avatar

Sorry for the delay. My workstation was swapping as the php process was using 5g.

What do you mean by using dompdf directly? Do you have a link that you could provide?

laracoft's avatar
laracoft
Best Answer
Level 27

use Dompdf\Dompdf;
use Dompdf\Options;

class markdown_controller
{

$options = new Options();                                          $options->set($nvpair);                                            $dompdf = new Dompdf($options);                                    $dompdf->loadHtml(view($view_name, $data));                        if (isset($data["title"])) {                                           $dompdf->add_info('Title', $data["title"]);                    }


} 

stever's avatar

Success. I still have to work on the options to get it to render properly but that will be some playing around for the morning.

This is what I did in my project directory:

vagrant@homestead:~/projects/laravel$ COMPOSER_MEMORY_LIMIT=-1 composer require dompdf/dompdf
vagrant@homestead:~/projects/laravel$ COMPOSER_MEMORY_LIMIT=-1 composer update

In the Controller:

use Dompdf\Dompdf;
    public function requisition($id)
    {
            // Get default limit
             //$normalTimeLimit = ini_get('max_execution_time');
             //ini_set('max_execution_time', -1);
            // $pdf = PDF::loadView('patient.requisition');
            // $pdf->save("stevereq.pdf");
            // Restore default limit
            // ini_set('max_execution_time', $normalTimeLimit);
            // dd($pdf);
            // return view('patient.requisition');
            $html = View('patient.requisition');
            $dompdf = new DOMPDF();
            $dompdf->loadHtml($html);
            $dompdf->render();
            $output = $dompdf->output();
            file_put_contents ('stevereq.pdf', $output);
    }
laracoft's avatar

@stever great happy to hear that, sorry my code was copied and pasted on my mobile's SSH earlier. Do pick a best answer. Thank you.

FrankClark's avatar

For me there was a really baffling cause for this. My code creates a PDF of a project in Asana (project management software) giving an overview of completed tasks etc. At some point they changed their system to wrap all task comments in <body></body> tags. Removing these tags with a simple str_replace resolved the nesting level error i was getting!

Please or to participate in this conversation.