DEV Community

It's Just Nifty
It's Just Nifty

Posted on Originally published at niftylittleme.com on

Unable To Open Dompdf PDF File? Here's The Solution

Unsplash Image by Kevin Ku

(Image Source)

To open a PDF made with Dompdf, you might want to add

ob_end_clean();
Enter fullscreen mode Exit fullscreen mode

before

$dompdf->stream();
Enter fullscreen mode Exit fullscreen mode

In your PHP project.

ob_end_clean — Clean (erase) the contents of the active output buffer and turn it off

(Quote Source)

Full Example Code:

    <?php
        require 'vendor/autoload.php';

        // reference the Dompdf namespace
        use Dompdf\Dompdf;

        $content = '<h1>Hello World</h1>';

        // instantiate and use the dompdf class
        $dompdf = new Dompdf();
        $dompdf->loadHtml($content);

        // (Optional) Setup the paper size and orientation
        $dompdf->setPaper('A4', 'landscape');

        // Render the HTML as PDF
        $dompdf->render();

        ob_end_clean();

        // Output the generated PDF to Browser
        $dompdf->stream();
    ?>
Enter fullscreen mode Exit fullscreen mode

For potentially more answers, go here.

Top comments (0)