Arabic Font Not Displaying Properly in pdf
I found some developers facing issues when generating PDF for Invoice or Doc files in PHP, In the Arabic Language. This blog all about generates PDF with Arabic font In PHP hasslefree. Here I will show you the example and some points so that you can easily convert HTML to PDF with the Arabic Language.
For Generating PDF in the Arabic Language, we have to Use TCPDF.
Step-1: download TCPDF library files.
Click here to Download tcpdf files.
Add this files to you PHP file, like below
1 |
include_once('TCPDF/tcpdf.php'); |
Step-2: Set font which is going to supports Arabic.
1 |
$pdf->SetFont('dejavusans', '', 10); |
Here Below Example, I am able to Generate an Invoice for My Client In the Arabic Language.
Code Goes Here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
<?php // Include the main TCPDF library (search for installation path). include_once('TCPDF/tcpdf.php'); $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // set document information $pdf->SetCreator(PDF_CREATOR); //$pdf->SetAuthor('Nicola Asuni'); $pdf->SetTitle('QabasTech Invoice'); //$pdf->SetSubject('TCPDF Tutorial'); //$pdf->SetKeywords('TCPDF, PDF, example, test, guide'); $pdf->setPrintHeader(false); $pdf->setPrintFooter(false); // set default monospaced font $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); // set margins $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $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); // set some language-dependent strings (optional) if (@file_exists(dirname(__FILE__).'/lang/eng.php')) { require_once(dirname(__FILE__).'/lang/eng.php'); $pdf->setLanguageArray($l); } // --------------------------------------------------------- $lg = Array(); // set some language-dependent strings (optional) $pdf->setLanguageArray($lg); // --------------------------------------------------------- // set font // set font // add a page $pdf->AddPage(); //$pdf->Write(0, 'Tax Invoice <br/>', '', 0, 'C', true, 0, false, false, 0); $pdf->SetFont('dejavusans', '', 14); // ----------------------------------------------------------------------------- $tbl = <<<EOD <table cellspacing="0" cellpadding="1" border="0"> <tr> <td rowspan="" align="center">Tax Invoice<br/>فاتورة ضريبية</td> </tr> </table> <table cellspacing="0" cellpadding="1" border="1"> <tr> <td rowspan="" align="right"><img src="images/logo.png" /></td> </tr> </table> EOD; $pdf->writeHTML($tbl, true, false, false, false, ''); // ----------------------------------------------------------------------------- $lg = Array(); $lg['a_meta_charset'] = 'UTF-8'; $lg['a_meta_language'] = 'fa'; $pdf->SetFont('dejavusans', '', 10); $pname ="Sorur"; $date = date('d/m/Y'); $invoice_no = 23343434; $po_ref = 45565656; $cname = "FreeOnlineprojects"; $due_date = date('d/m/Y'); $tbl = <<<EOD <table cellspacing="0" cellpadding="1" border="1"> <tr> <td rowspan="3">Project Name:$pname</td> </tr> </table> EOD; $pdf->writeHTML($tbl, true, false, false, false, ''); // ----------------------------------------------------------------------------- $tbl = '<table cellspacing="0" cellpadding="1" border="1"> <tr> <td> <div style="width:400px;"> <div style="float:left">المركز الرئيسي <br/>Head Office Address: </div><div style="float:right"> Free Online Projects, Zubli hills P.O.Box:500084,Seculhort-11642,Saudi Arabia.</div> </div> <br/> Tell تلفون : 966112111896<br/> FAX فاكس : 1524515251245<br/> Email الايميل : freeonlineprojectz@gmail.com <br/> VAT reg الرقم الضريبي #: 454545454545 </td> <td> <div style="width:400px;"> <div style="float:left">سم الشركة <br/> Company Name :</div><div style="float:right">Some Technology for Trading Co.</div> </div> <br/> Bank Name اسم البنك : Bank Al Jazira <br/> Location المدينة : Riyadh<br/> Account رقم الحساب : 5657575757<br/> IBAN رقم الايبان #: SA67676776700787 </td> </tr> <tr> <td>Issue Date تاريخ الإصدار : '.$date.' <br/> Invoice رقم الفاتورة : '.$invoice_no.'<br/> Po Ref رقم امر الشراء : '.$po_ref.'<br/></td> <td>Bill To الى : '.$cname.' <br/> Due Date تاريخ الإستحقاق : '.$due_date.'<br/> </td> </tr> </table> <table cellspacing="0" cellpadding="1" border="1"> <tr> <td>SL No. م #</td> <td>Item Description<br/> وصف الأعمال</td> <td>Quantity <br/>الكمية</td> <td>Unit Price<br/> سعر الوحدة (ر.س)</td> <td>Total Price<br/> السعر الإجمالي (ر.س) </td> </tr>'; $tbl .='<tr> <td>Laptop</td> <td>HP- 5th Generation</td> <td>50000</td> <td>4</td> <td>200000</td> </tr>'; $s_total = 200000; $vat_amount =(12/100)*200000; $vat = 12; $paid =$vat_amount + $s_total; $tbl .='</table> <table cellspacing="0" cellpadding="1" border="1"> <tr> <td align="right">Sub Total السعر الإجمالي: SAR. '.$s_total.'<br/> VAT('.$vat.'%) ضريبة القيمة المضافة: SAR. '.$vat_amount.'<br/> Total with VAT السعر الإجمالي بعد الضريبة: SAR.'.$paid.' </td> </tr> </table> <table cellspacing="0" cellpadding="1" border="1"> <tr> <td> </td> </tr> </table> <table cellspacing="0" cellpadding="1" border="1"> <tr> <td>This is an electronically generated invoice and does not require signature </td> </tr> </table> <table cellspacing="0" cellpadding="1" border="1"> <tr> <td>تم اصدار هذه الفاتورة الكترونيا ولا تتطلب الى توقيع </td> </tr> </table>'; $pdf->writeHTML($tbl, true, false, false, false, ''); // ----------------------------------------------------------------------------- // NON-BREAKING TABLE (nobr="true") // ----------------------------------------------------------------------------- //Close and output PDF document $pdf->Output('example_arabic.pdf', 'I'); |
Here You have to download the TCPDF zip file and unzip in your root folder, then run this PHP file, you will get the pdf in the Arabic language. Any Issues Leave a comment below, I will try to fix your problem.
Recommended Posts
Read Also: Hindi Font Not Displaying Properly in pdf
I hope this blog helps you a lot.