1

I'm using Laravel 9 and Dompdf to generate some pdf files.

And the blade that I'm calling for the pdf files, contains persian/farsi/arabic character languages and therefore the final result will be shown like ????????.

So how can I generate utf-8 fiendly pdf files properly?

public function convertToPdf(Request $request, Order $order)
    {       
        $dompdf = new Dompdf();
        $dompdf->loadHtml(view('admin.shop.orders.order_details_pdf'));
        
        $dompdf->setPaper('A4','landscape');
        
        $dompdf->render();
        
        $dompdf->stream('demo.pdf',['Attachment' => false]);
    }
3

1 Answer 1

0

For DomPdf to display non-English languages, the following is necessary:

  • set the html charset to utf8
  • use a font that supports the language in question

Here's an example on how to do it:

$html = <<<HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Arabic:[email protected]&family=Noto+Sans+JP:[email protected]&family=Noto+Sans+KR:[email protected]&family=Noto+Sans+SC:[email protected]&family=Noto+Sans:[email protected]&display=swap" rel="stylesheet">
<style>

body {
    font-family: 'Noto Sans', 'Noto Sans JP', 'Noto Sans SC', 'Noto Sans KR', 'Noto Sans Arabic', 'Tangerine', sans-serif;
}

</style>
</head>
<body>
    Thank you for shopping with us!<br>
    Merci d'avoir fait vos achats chez nous!<br>
    Vielen Dank für Ihren Einkauf!<br>
    Спасибо что покупаете у нас!<br>
    Ευχαριστούμε που αγοράσατε από εμάς!<br>
    ご利用いただきありがとうございます!<br>
    感謝您與我們購物!<br>
    우리 가게를 찾아주셔서 감사합니다!<br>
    را لتسوقك معنا!<br>
</body>
</html>
HTML;

$dompdf = new Dompdf();
$dompdf->loadHtml($html);            
$dompdf->render();

...
Sign up to request clarification or add additional context in comments.

3 Comments

Unsure what the linked github has to do with the actual answer? Self-promotion is not allowed and could be considered as SPAM
@DarkBee The link you removed links to a complete implementation that is open source (MIT license) and absolutely relevant. In addition, there were no answers to this question since it was asked more than 6 months ago. The only thing I should have mentioned is that I'm the author.
Nowhere in the repo is this "template" to be found. So as stated before, how is the repo relevant here? Furthermore what has this snippet actually to do with the answer? It's just bloat attributing to nothing really. Even wondering how'd u would license this code as MIT... It's just an embed, widely open from Google Fonts and some text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.