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_encodingif needed.
Try the first solution above and see if it resolves your issue. If not, check your data sources for encoding issues.