Create PDF with Emoji utf-8 Character Using PHP

I am trying to create pdf with text including emoji character.

using PHP > 7 and mysql

‘char_set’ => utf8mb4

‘dbcollat’ => utf8mb4_unicode_ci

I have create PDF from html, is echo html text all emoji shown good. but if its create pdf its not convert.

Please help to solve this problem

exampple : “One \ud83d\ude02”

thanks

Here is more explanation for simplify my question.

$fileName = $source_path.'example.pdf';

//using php codeignitor 3.1.2

$htmlContent = $this->load->view('view/view_pdf', $data, TRUE); 
$htmlContent = <<<EOD
 $htmlContent 
EOD;

// call pdf create function 
$this->createPDF($fileName, $htmlContent);

// these character working correct as echo output on browser and send in email subject n body.
 echo decodeEmoticons("One \ud83d\ude02"); 
 echo "One \u{1F602}"; 

/*******************************/ 
// helper functions
function checkJson($title){
    if (json_decode($title, true) !== null) {
        $title =  str_replace('"', '', json_decode($title));
    }else{
         $title = str_replace('"', '', $title);
    }
    return $title;
 }

 function decodeEmoticons($src) {
    $replaced = preg_replace("/\\\\u([0-9A-F]{1,4})/i", "&#x$1;", $src);
    $result = mb_convert_encoding($replaced, "UTF-16", "HTML-ENTITIES");
    $result = mb_convert_encoding($result, 'utf-8', 'utf-16');
    $result = checkJson($result);
    return $result;
} 

/*******************************/ 

$htmlContent output html code as follows

<div class="row">   
    <div class="col-lg-12">

    <table class="table dataTable" border="0" cellpadding="0" cellspacing="0" width="100%">
        <tbody>

            <tr>
                <td colspan="3"> </td>
            </tr>
            <tr>
                <td width="60%"><b>Event Name:</b> <?php echo decodeEmoticons("One \ud83d\ude02"); ?> </td>
                <td width="60%"><b>Event Name:</b> <?php echo "One \u{1F602}"; ?> </td>
                <td><b>Start Time:</b> <?php echo date('F j, Y, g:i A', time()); ?></td>
            </tr>

            <tr>
              <td colspan="3"></td>
            </tr>
        </tbody>
    </table>
    </div>
</div>

here is my php function call TCpdf library

public function createPDF($fileName, $htmlContent) {
        /*
        http://www.tcpdf.org
// File name   : tcpdf.php
// Version     : 6.2.13
// Begin       : 2002-08-03
// Last Update : 2015-06-18
       */
    ob_start(); 
    // Include the main TCPDF library (search for installation path).
    $this->load->library(array('Tcpdf'));

    // create new PDF document
    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    //$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);

    // set document information 
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('auth');

    $pdf->SetTitle('Event ');
    $pdf->SetSubject('Event ');
    $pdf->SetKeywords('Event ');

    // set default header data
    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

    // 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));

    $pdf->SetPrintHeader(false);
    $pdf->SetPrintFooter(false);

    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

    // set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, 10, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(10);
    $pdf->SetFooterMargin(30);

    // set auto page breaks
    //$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    $pdf->SetAutoPageBreak(TRUE, 30);

    // 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);
    }       


    $pdf->SetFont('dejavusans', '', 10);

    //$pdf->SetFont('halvatica', '', 10); // this is also trying but fail

    // add a page
    $pdf->AddPage();    

    //$htmlContent = utf8_encode($htmlContent);// this is also trying but fail

    // output the HTML content
    $pdf->writeHTML($htmlContent, true, false, true, false, '');

    // reset pointer to the last pagend output PDF document
    $pdf->lastPage();  

    ob_end_clean();
    //Close and output PDF document
    $pdf->Output($fileName, 'F');        
}

This Post Has One Comment

  1. No Fault

    I presume that dejavusans doesn’t have emojis. The browser replaces missing glyphs on the fly with a fallback font. TCPDF probably doesn’t

Leave a Reply