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

jpmg's avatar
Level 13

Problem with livewire and PDF

Hello community. I using livewire 3 and barryvdh/laravel-dompdf v.3 in my livewire method i have this.

public function branchPrint()
{

    $data = [
        'title' => 'My PDF Title',
        'content' => 'This is the content of the PDF.',
    ];

    return Pdf::loadView('livewire.pdf.mypdf', $data)->stream();
}

but it doesn't work, it give me this error "Malformed UTF-8 characters, possibly incorrectly encoded", but if I put the same code in a controller it works just fine, can someone tell me why inside of livewire doesn't work?

1 like
4 replies
LaryAI's avatar
Level 58

The error "Malformed UTF-8 characters, possibly incorrectly encoded" typically occurs when there's an issue with character encoding in the data being processed. When using Livewire with PDF generation, there are a few things to consider that might differ from a standard controller.

Here are some steps to troubleshoot and potentially resolve the issue:

  1. Check Data Encoding: Ensure that all data being passed to the PDF view is properly encoded in UTF-8. You can use PHP's mb_convert_encoding function to ensure this:

    $data = [
        'title' => mb_convert_encoding('My PDF Title', 'UTF-8', 'UTF-8'),
        'content' => mb_convert_encoding('This is the content of the PDF.', 'UTF-8', 'UTF-8'),
    ];
    
  2. Blade Template Encoding: Make sure your Blade template (livewire.pdf.mypdf) is saved with UTF-8 encoding. Some editors might save files in a different encoding, which can cause issues.

  3. Livewire Response Handling: Livewire components are designed to return JSON responses, which might interfere with binary data like PDFs. Instead of returning the PDF directly from the Livewire method, you can trigger a download or open the PDF in a new tab using JavaScript.

    Here's a possible workaround:

    • Modify your Livewire method to return a URL to a route that generates the PDF:
    public function branchPrint()
    {
        return redirect()->route('pdf.download');
    }
    
    • Define a route in your web.php:
    Route::get('/pdf/download', function () {
        $data = [
            'title' => 'My PDF Title',
            'content' => 'This is the content of the PDF.',
        ];
    
        return Pdf::loadView('livewire.pdf.mypdf', $data)->stream('document.pdf');
    })->name('pdf.download');
    
    • In your Livewire component, you can use JavaScript to open the PDF in a new tab:
    window.open('/pdf/download', '_blank');
    
  4. Check for Special Characters: If your data contains special characters, ensure they are properly escaped or encoded.

By following these steps, you should be able to resolve the encoding issue and successfully generate PDFs using Livewire and barryvdh/laravel-dompdf.

vincent15000's avatar

Where is your branchPrint() function ? In the livewire controller ? So it is in a controller.

I don't think that it's a livewire or PDF problem.

Can you show the livewire.pdf.mypdf view please ?

jpmg's avatar
Level 13

@vincent15000 my branchPrint() is a method in my livewire component.

mypdf.blade.php is nothing more than this.

	<!DOCTYPE html>
			<html>
				<head>
    				<title>{{ $title }}</title>
	  		  </head>
		      <body>
                      <h1>{{ $title }}</h1>
                      <p>{{ $content }}</p>
             </body>
         </html>
1 like

Please or to participate in this conversation.