Warm tip: This article is reproduced from serverfault.com, please click

Laravel 6

发布于 2020-12-08 06:48:59

I'm writing a function to create and display a pdf file using Dompdf in a Laravel 6 project using PHP 8. I tried with many tutorials and even the official documentation from GitHub for the normal Dompdf version and it's wrapper for Laravel. Here is my pdf generator function code:

private function generate_pdf(){
    $html = '
        <html>
            <body>
                <h1>Hello Dompdf</h1>
                <h2>MDF</h2>
            </body>
        </html>
    ';
    $pdf = PDF::loadHTML($html);
    return $pdf->stream('welcome.pdf');
}

It works fine but here is the result of the function:

enter image description here

As you can see the elements are overlapping itself on top of each others. I looked for answers but I can't find anything related. Does somebody knows why is this happening?

Questioner
Ale Ortega
Viewed
0
Ale Ortega 2020-12-10 15:49:36

If somebody have this problem again, I found a solution and it is the worst it can be:

private function generate_pdf(){
    $html = '
        <html>
            <body>
                <h1>Hello Dompdf</h1><br/><br/>
                <h2>MDF</h2>
            </body>
        </html>
    ';
    $pdf = PDF::loadHTML($html);
    return $pdf->stream('welcome.pdf');
}

Just add
elements to every element in order to give the correct space. It is a nasty solution but it works.