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

hsnch41's avatar

DOMPDF arabic problem

DOMPDF is giving ??? error for arabic language but when i solved that is not showing correct words.

0 likes
16 replies
Emtized's avatar

You must use this codes in your header

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
  body { font-family: DejaVu Sans, sans-serif; }
</style>
1 like
ekhlas's avatar

Hi Try this

$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);        
        //$html2pdf = new HTML2PDF('P', 'A4', 'en', true, 'UTF-8',array(6, 3, 9, 3));
        // set default header data
        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 006', '');       
        // set header and footer fonts
        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));     
        // set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);        
        // set margins
        //$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetMargins(5,5,5);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);       
        // set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);        
        // set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);     
        
        $pdf->setPrintHeader(false);
        // set Display
        $pdf->SetDisplayMode('fullpage');
        // add a page
        $pdf->AddPage();    
        
            // set some text to print
        $html=<<<EOD
        $newhtml
EOD;
        // print a block of text using Write()
        //print_r($html);exit;
        //$pdf->Write(0, $html, '', 0, 'C', true, 0, false, false, 0);
        $pdf->writeHTML($html, true, false, true, false, '');
        
        $pdf->lastPage();
        // ---------------------------------------------------------
        //ob_end_clean();       
        //Close and output PDF document     
        $magazineDir = getcwd().'/pdf/'.$id;
        //chmod($magazineDir.'/Magazine_'.$curr_language.'.pdf', 0755);
        if(!is_dir($magazineDir))
        {
            mkdir($magazineDir);
        }               
        //Store pdf 
        $pdf->Output($magazineDir.'/Magazine'.pdf',"F");
ekhlas's avatar

@HSNCH41 Pate your Controller code here in discussion. so i can help you further.

ekhlas's avatar

Tip: UTF-8 support

In your templates(invoice.blade.php), set the UTF-8 Metatag:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
$pdf = PDF::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');

Hope this will help.

for further assistance check this : https://github.com/barryvdh/laravel-dompdf

hsnch41's avatar

@ekhlas if ($fee) { $pdf = PDF::loadView('admin.templates.fee-new', compact('fee','days')); return $pdf->stream('fee-receipt-' . $fee->student->last_name . '.pdf'); }

ekhlas's avatar

hi @hsnch41

in <head> section add this<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> in admin.templates.fee-new.blade.php

Nash's avatar

@hsnch41 You also need to make sure that the used font supports Arabic characters. Make sure to define a font you know will work in the pdf template CSS or change "default_font" in config/dompdf.php

Nash's avatar

@hsnch41 It could be that dompdf uses a default font that does not support Arabic characters. You can set the font with CSS in your pdf template (like you would do for any other html page) or you could change the default font in the dompdf config. See "configuration" in docs:

https://github.com/barryvdh/laravel-dompdf

hsnch41's avatar

@Nash i have changed my font to body { font-family: DejaVu Sans, sans-serif; } but then can't fixed arabic font direction.

hsnch41's avatar

@nash tell me best package for pdf reports that support all languages display.

Nash's avatar

@hsnch41 I haven't tried to use Arabic in a pdf myself, but a quick Google search suggests that for example mPDF or TCPDF should be able to deal with it (as opposed to dompdf that appears to have problems with it based on a quick search).

Please or to participate in this conversation.