skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. The problem is compatibility with PHP 8 itself. I had the same issue and downgrading to PHP 7.4 has worked on my server. Not sure where the incompatibility may be, but immediately after updating to PHP 8.0 DOMPDF stopped interpreting the HTML properly and started placing items on top of each other.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search