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

vincent15000's avatar

laravel-dompdf - Malformed UTF-8 characters, possibly incorrectly encoded

Hello,

I'm regularly using laravel-dompdf, but it's the first time that I get this error.

Malformed UTF-8 characters, possibly incorrectly encoded

I have tried with loading the view, with loading HTML, ... I always get this error.

Here is my code.

public function export()
{
    $selectedDate = Carbon::create($this->selectedYear, $this->selectedMonth)->format('Ym');

    $name = 'Report - '.$selectedDate.'.pdf';

    return Pdf::
        loadView('pdf.report', [
            'month' => $this->months[$this->selectedMonth],
            'year' => $this->selectedYear,
            'reports' => $this->reports,
        ])
        ->setPaper('a4', 'landscape')
        ->stream();
}

What am I doing wrong ?

Thanks for your help.

V

0 likes
3 replies
RemiM's avatar
RemiM
Best Answer
Level 16

There are few things that can cause this error to happen, but I found this issue and the solution offer by TigselemaAlex could help you.

1 like
vincent15000's avatar

@RemiM Thank you, it works.

But why ?

public function export()
{
    $selectedDate = Carbon::create($this->selectedYear, $this->selectedMonth)->format('Ym');

    $name = 'OC - Rapport financier - '.$selectedDate.'.pdf';

    $pdf = Pdf::
        loadView('pdf.report', [
            'month' => $this->months[$this->selectedMonth],
            'year' => $this->selectedYear,
            'reports' => $this->reports,
        ])
        ->setPaper('a4', 'landscape');

    return response()->streamDownload(function () use ($pdf) {
        echo $pdf->stream();
    }, $name);
}
RemiM's avatar

@vincent15000 Referencing TigselemaAlex, because the question was asked:

[...] I think it's because it's trying to show in a view the pdf data stream and that's why this error occurs.

People are asking for a proper library fix though.

1 like

Please or to participate in this conversation.