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

It's Just Nifty @nifty-little-me

About: Just a programmer and blogger sharing their journey. 😍 PHP, ❤️ React Native, and ❤️ React!

Location:
Nevada
Joined:
Sep 16, 2024

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

Publish Date: Sep 16 '24
0 0

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.

Comments 0 total

    Add comment