如何使用Laravel barryvdh / laravel-dompdf停止ERR_CONNECTION_RESET问题

I'm using the barryvdh/laravel-dompdf package with Laravel to generate PDF's for printing.

When I download the PDF I get this error.. (Loads for about 10 sec then error)

This site can’t be reached The connection was reset.

Try: Checking the connection Checking the proxy and the firewall Running Windows Network Diagnostics ERR_CONNECTION_RESET

The URL is a valid link as I was using the same link / view to build the PDF.

Here is my code from the Controller:

use PDF;
...
    public function haitiKidPdf($childId){
        $haitiKid = Kid::findOrFail($childId);
        $pdf = PDF::loadView('admin.cdp.haiti-kid-pdf', compact('haitiKid'));
        return $pdf->download('haiti-kids.pdf');
}

This was what I was using to create the page and pull in the data. (This is how I know the URL is correct.)

    public function haitiKidPdf($childId){
        $haitiKid = Kid::findOrFail($childId);
        return view('admin.cdp.haiti-kid-pdf',compact('haitiKid'));
}

Inside my view (haiti-kid-pdf.blade.php) I'm using a lot of PHP to dynamically populate all the necessary data.

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">

<head>
    <title>PDF</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="{{ asset('/css/app.css') }}" rel="stylesheet">
    <style>
    body {
        font-family: 'Muli', sans-serif;
        background-color: #1E1F26 !important;
    }

    .cutline {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate3d(-50%, -50%, 0);
        border-right: 1px dotted rgba(21, 87, 36, 0.25);
        z-index: 99999;
        width: 5.5;
        height: 8.5in;
....

    @media print {


        * {
            -webkit-print-color-adjust: exact;
        }

        html {
            background: none;
            padding: 0;
        }

        body {
            box-shadow: none;
            margin: 0;
        }
...
    </style>
</head>
...
<img alt="Name Here" src="{{asset("storage/images/cdp/$haitiKid->cin/profile-img/$haitiKid->child_img")}}">
                                </div>
                            </div>
                        </div>
                        <!-- END KID IMAGE -->
                        <div class="kid-name-box relative">
                            <div class="kid-name absolute pin-c-y ml-2 font-black leading-none text-navy-500">
                                <span class="first-name">{{ $haitiKid->first_name }}</span>
                                <span class="last-name">{{ $haitiKid->last_name }}</span>
                            </div>
                        </div>
                        <div class="slot-box absolute bg-red-500">
                            <div class="slot-box-name absolute pin-c-y ml-2">
                                @php
                                $numReceived = $haitiKid->sponsors_received;
                                $numNeeded = $haitiKid->sponsors_needed - $numReceived;
                                @endphp
                                @for($i = 0; $i < $haitiKid->sponsors_needed; $i++ )
                                    @if($numReceived-- > 0)
                                    <span class="slot-circ relative"></span>
                                    @else
                                    <span class="slot-circ-outline relative"></span>
                                    @endif
                                    @endfor
                                    <span class="font-black leading-none text-white uppercase">{{ $numNeeded }} {{ str_plural('Slot', $numNeeded) }} Available</span>
                            </div>
                        </div>
etc...

    }

Basically generating this to print out on a 11in x 8.5in piece of paper.

https://codepen.io/daugaard47/full/pozgdeM

Is my code incorrect or do I need to install something else in my php.ini file?

  • I'm using PHP 7.2.2
  • Currently on localhost
  • I have Enable inline PHP set to true "enable_php" => true, in the dompdf.php config file
  • If I load the view and then select Print > Print to PDF it comes out fine, but that defeats the purpose of what I'm trying to accomplish.

Been at this for days. Any help would be appreciated.

As I can not comment (Yet), I will just leave the answer here.

The package you are using (barryvdh/laravel-dompdf) takes a while to render a view. And by the looks of it, you are trying to render a view. The connection reset is occouring because by default, the max execution time for php is set to 10 seconds. Thus a timeout is occouring. There are some steps you could try.

Edit: After updating php.ini file, be sure to restart you development server (Apache, Nginx etc on WAMP, XAMPP etc)

Step 1: Try changing the PHP Max execution time to more than 10 seconds. You will need to edit you php.ini file for that. Look for the line max_execution_time=. The file is generally located under /php/php.ini. I have set it to 160 meaning it will take 160 seconds before timing out (this was only for testing).

Step 2: This is a debug step. Try limiting your output data. The more data you feed to the render, the more time it takes to execute. To see if it is indeed a timeout, you could try this method. Limit the data you are getting to about 10 and test if you are still getting the error. You can also follow this step after you have finished the 1st step.

You could also try THIS PACKAGEE to see if it helps. It is a wrapper made by the same developers. I haven't personally tested it but you are welcome to try this.

Note: If your app is taking a while to load, then it means a bad user experience and load for the app. I would recommend to use another approach to get some load off and make load times faster

Let me know if this helps. Happy coding :)