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”


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

// 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));
         $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%">

                <td colspan="3"> </td>
                <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>

              <td colspan="3"></td>

here is my php function call TCpdf library

public function createPDF($fileName, $htmlContent) {
// File name   : tcpdf.php
// Version     : 6.2.13
// Begin       : 2002-08-03
// Last Update : 2015-06-18
    // Include the main TCPDF library (search for installation path).

    // 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->SetTitle('Event ');
    $pdf->SetSubject('Event ');
    $pdf->SetKeywords('Event ');

    // set default header data

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

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

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

    // set image scale factor

    // set some language-dependent strings (optional)
    if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {

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

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

    // add a page

    //$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

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