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.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more