Zahidul Hossein Ripon
Zahidul Hossein Ripon

Reputation: 672

How to export emoji to PDF document using PHP?

I am trying to export to PDF using FPDF and TCPDF php library. I found that the emojis like 😁 πŸ˜€ πŸ’ƒπŸ» ❀️ πŸ₯³ where not converted. Only ️️some rectangle box there in generated pdf. I also tried tfpdf.

    $text = "There is my text 😁 , πŸ˜€ and emojis πŸ’ƒπŸ» ❀️ πŸ₯³";
    require('tfpdf/tfpdf.php');
        
    $pdf = new tFPDF();
    $pdf->AddPage();
        
    //Add a Unicode font (uses UTF-8)
    $pdf->AddFont('Segoe UI Symbol','','seguisym.ttf',true); // DejaVuSans.ttf
    $pdf->SetFont('Segoe UI Symbol','',12);
    $pdf->Write(8,$text);
    $pdf->Output();

I also tried different font. But didn't work for me. Can any one help me in this regard?

Upvotes: 1

Views: 1410

Answers (2)

Piyush Sapariya
Piyush Sapariya

Reputation: 538

You can replace your emojis into either png or svg and then you can use in pdf, as png and svg will support and you get your result.

Try below code

Import twitter script for get image

<script type="module">
    import twemoji from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm' 
</script>

This function will convert your emoji into hex code

 function emojiToHexForTwemoji($emoji) {
       $hex = '';
       $emojiLength = mb_strlen($emoji, 'UTF-8');
       for ($i = 0; $i < $emojiLength; $i++) {
           $char = mb_substr($emoji, $i, 1, 'UTF-8');
           $hex .= '-' . strtolower(dechex(mb_ord($char)));
       }
       return trim($hex, '-');
   }

This code will replace your emojis from string to svg or png image

$input_string = "Hello πŸ˜€! Let's grab some πŸ• and πŸ₯€.";
   $output = ''; // Output will store the HTML
   
   // Iterate over each character and convert emojis
   $length = mb_strlen($input_string, 'UTF-8');
   for ($i = 0; $i < $length; $i++) {
       $char = mb_substr($input_string, $i, 1, 'UTF-8');
       
       // If the character is an emoji, convert to Twemoji URL
       if (preg_match('/[\x{1F600}-\x{1F64F}|\x{1F300}-\x{1F5FF}|\x{1F680}-\x{1F6FF}|\x{1F1E0}-\x{1F1FF}|\x{2600}-\x{26FF}|\x{2700}-\x{27BF}|\x{1F900}-\x{1F9FF}]/u', $char)) {
           $hex = emojiToHexForTwemoji($char);
           // $twemojiUrl = "https://twemoji.maxcdn.com/v/latest/72x72/$hex.png"; // Twemoji CDN URL
           $twemojiUrl = "https://cdn.jsdelivr.net/gh/twitter/[email protected]/assets/72x72/$hex.png"; // Twemoji CDN URL
           $output .= "<img src='$twemojiUrl' alt='$char' class='twemoji' style='width: 1em; height: 1em;' />";
       } else {
           $output .= htmlspecialchars($char); // Non-emoji characters stay the same
       }
   }

Full code as below

<script type="module">
     import twemoji from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm'
</script>
$output = ''; // Output will store the HTML
for ($i = 0; $i < $length; $i++) { // Iterate over each character and convert emojis
$char = mb_substr($text, $i, 1, 'UTF-8');
if (isEmoji($char)) {
    $hex = emojiToHexForTwemoji($char);
    /* $twemojiUrl = "https://cdn.jsdelivr.net/gh/twitter/[email protected]/assets/72x72/$hex.png"; // PNG Twemoji CDN URL  */
    $twemojiUrl = "https://cdn.jsdelivr.net/gh/twitter/[email protected]/assets/svg/$hex.svg"; // SVG Twemoji CDN URL
    $output .= "<img src='$twemojiUrl' alt='$char' class='twemoji' style='width: 1em; height: 1em;' />";
} else {
    $output .= htmlspecialchars($char); // Non-emoji characters stay the same
}
}
$text = $output;
function emojiToHexForTwemoji($emoji) {
    $hex = '';
    $emojiLength = mb_strlen($emoji, 'UTF-8');
    for ($i = 0; $i < $emojiLength; $i++) {
        $char = mb_substr($emoji, $i, 1, 'UTF-8');
        $hex .= '-' . strtolower(dechex(mb_ord($char)));
    }
    return trim($hex, '-');
}

Upvotes: -1

micropro.cz
micropro.cz

Reputation: 616

Sadly fPDF, TCPDF nor tFPDF can't print those characters. Issue is, these characters are not part of BMP, they are expressed with surrogate pairs, meaning they behave like multiple characters in UTF-16 (because of that one emoticon is printed as 2 rectangle boxes, not one) and also they have codepoint above 65535. However all mentioned PDF libraries relies on codepoint index being <= 65535 as well as TFontFile class reading TTF files.

You would also need to add TTF file having complete set of Unicode charset, or at least emoticons. Most fonts does not have it. This brings another issue for PDF library, which would probably need to have support for fallback font, which will be used when codepoint is not found in main font (for example you want to print text in Gotham, but since that does not include emoji, use other font for them). Btw for example emoji font "Noto Color Emoji" has 23 MB TTF file. So it gets big easily.

Anyway, all of the above can be added to PDF libraries, but it will require some effort. I am planning to do it for my needs as well sometimes. I think it will take roughly 1 man day.

Alternativelly, you might try something more robust like mPDF, but that library is huge, slow and require complete rewrite of your fPDF code. Also can't guarantee it can print emojis as well.

Upvotes: 1

Related Questions