0

I have this method in my controller which export a PDF

    public function export($semester)
    {
        // some code

        $pdf = \PDF::loadView('entities.match.pdf', ['profiles' => $profiles, 'semester' => $semester]);

        return $pdf->download('Matches_' . $semester->name . '.pdf');
    }

and my "match.pdf" view

<h3>Matches for {{ $semester->name }}</h3>

@if (count($profiles) == 0)
    No Matches for this semester yet...
@else
    <table border="1" border-spacing: "5px" style="width:100%">
    <thead>
    <tr>
        <td align="center" colspan="2">Local Student</td>
        <td align="center" colspan="2">Incoming Student</td>
    </tr>
    <tr>
        <th> Last, first name</th>
        <th> Course</th>
        <th> Last, first name</th>
        <th> Course</th>
    </tr>
    </thead>
    <tbody>
    @foreach($profiles as $item)
        <tr>
            <td width="20%">
                {{ $item['local']->user->last_name }}, {{ $item['local']->user->first_name }}<br>
                {{ $item['local']->user->email }}
            </td>
            <td width="30%">{{ $item['local']->course->name }}</td>
            <td width="20%">
                {{ $item['incoming']->user->last_name }}, {{ $item['incoming']->user->first_name }}<br>
                {{ $item['incoming']->user->email }}
            </td>
            <td width="30%">{{ $item['incoming']->course->name }}</td>
        </tr>
    @endforeach
    </tbody>
    </table>
@endif

The code is correct and works but if only if the amount of "profiles" is less than about 110. If the "profile" amount is above that number, I get "failed" as the download status in Firefox and Chrome.

Is this a DOMPDF bug? Is my code not right? Any workaround?

Sebastian M
  • 471
  • 1
  • 4
  • 20

1 Answers1

0

Your code looks fine, I would assume that either the server is running out of memory whilst creating the PDF's or, it's timing out.

What I would do, is chunk the PDF up into sections. I.e. the head (So the table head + headers), the body (Profile Items split into more manageable chunks, so 50 or so), and the footer.

If you generate all of these as different PDF's you can use the following code to merge them:

$fileArray= array("name1.pdf","name2.pdf","name3.pdf","name4.pdf");

$datadir = "save_path/";
$outputName = $datadir."merged.pdf";

$cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outputName ";
//Add each pdf file to the end of the command
foreach($fileArray as $file) {
    $cmd .= $file." ";
}
$result = shell_exec($cmd);

The code is from this question: Merge PDF files with PHP

Community
  • 1
  • 1
Benjamin
  • 347
  • 1
  • 12