Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rretzko's avatar

Weird PDF problem

I'm troubleshooting a weird PDF error message & hope you can help. My method is extremely simple:

use Barryvdh\DomPDF\Facade\Pdf;
public function downloadPullSheetPdf()
    {
        $pdf = PDF::loadHTML('<h1>Test PDF</h1><p>Hello World</p>');
        return $pdf->download('test.pdf');
}

But when I run it (Livewire 3 Component), I get the following error message:

Malformed UTF-8 characters, possibly incorrectly encoded

Any thoughts on how to fix this are appreciated!

0 likes
5 replies
LaryAI's avatar

The error Malformed UTF-8 characters, possibly incorrectly encoded typically means that the string you're passing to DomPDF contains invalid UTF-8 characters, or the encoding is not set correctly.

Here are a few things you can try to resolve this:

1. Ensure the HTML is UTF-8 Encoded

Wrap your HTML with a meta tag specifying UTF-8:

$pdf = PDF::loadHTML('
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        </head>
        <body>
            <h1>Test PDF</h1>
            <p>Hello World</p>
        </body>
    </html>
');
return $pdf->download('test.pdf');

2. Use mb_convert_encoding to Force UTF-8

If your HTML is coming from a variable or external source, make sure it's UTF-8 encoded:

$html = '<h1>Test PDF</h1><p>Hello World</p>';
$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
$pdf = PDF::loadHTML($html);
return $pdf->download('test.pdf');

3. Check for Non-UTF-8 Characters

If your HTML contains dynamic data, ensure all variables are UTF-8 encoded:

$html = '<h1>' . htmlspecialchars($title, ENT_QUOTES, 'UTF-8') . '</h1>';

4. If Using Blade Views

If you render a Blade view, ensure the view file is saved as UTF-8 (without BOM).

Summary

  • Always specify UTF-8 in your HTML.
  • Ensure all dynamic content is UTF-8 encoded.
  • Use mb_convert_encoding if needed.

Try the first solution above and see if it resolves your issue. If not, check your data sources for encoding issues.

Rretzko's avatar

Update: I tried #1 above and hit the same error message.

Snapey's avatar

But when I run it (Livewire 3 Component), I get the following error message:

error message where? In Livewire's error modal? In your log files? in the browser console?

Rretzko's avatar

Rather than run this directly from the Laravel component, I've set up a redirect to a Controller:

$this->redirectRoute('pdf.pullSheet', ['model' => 32]);

and then put my pdf routine there. I don't know why it doesn't work directly from Livewire, but this solution is working for me. (Not a slam on Livewire; I'm sure I'm doing something wrong!)

Rretzko's avatar

Hi Snapey - The error message was in the browser console.

Please or to participate in this conversation.