Search code examples
character-encodingmpdf

mpdf php library do not show pound currency sign £


I am creating payment receipt PDF file using mPDF php library, I am using £ pound currency sign along amount.

Once PDF is generated it showed ÂŖ sign instead of £. I have already added UTF-8 characters encoding in document.

Anyone came across same issue please let me know its solution.

Sample code to reproduce it is shown below. You can download or clone mPDF library at GitHub url.

    require_once '/inc/mpdf/vendor/autoload.php';
    $mpdf = new \Mpdf\Mpdf(['tempDir' =>  '/inc/mpdf/tmp']);
    $mpdf->allow_charset_conversion = true;
    $mpdf->charset_in = 'iso-8859-4';


    $mpdf->SetDisplayMode('fullpage');
    $mpdf->list_indent_first_level = 0;
    $mpdf->SetTitle('Transparent hands Tax Receipt');


    $html ="Total Amount £ 45,00";
    $mpdf->WriteHTML($html);
    $mpdf->Output('Tax Receipt.pdf', 'D');
    if (!$mpdf->send()) {
        throw new Exception('Could not create Tax Receipt: '.$mpdf->getError());
    }
    die();

Output of above code is shown below. Expected result should be Total Amount £ 45,00

Total Amount ÂŖ 45,00


Solution

  • Your code is probably in UTF, remove the

    $mpdf->charset_in = 'iso-8859-4';
    

    line. £ character cannot be represented in iso-8859-4.

    With the setting above present, mPDF tries to re-encode characters from iso-8859-4 to UTF, which causes the pound sign to be mangled into ÂŖ.

    Alternatively, use a HTML entity £ instead of the £ character - that should be safe with all encodings.