Add Emoji support to Dompdf

December 2, 2022 β‰ˆ 45 seconds

Attempting to generate a PDF with πŸš€πŸ‘Œ πŸ˜‰ and getting πˆ‰πˆ‰πˆ‰? Googling has led you there: https://github.com/dompdf/dompdf/issues/1698, where you learned that the problem is hard, not solved yet, and that emojis support should be added to php-font-lib first? What should you do next if you still want emojis in your generated PDF?


A temporary solution (until emoji support is added to php-font-lib) is to replace emojis with PNGs

We'll use the iamcal/php-emoji package, which will replace all emojis with PNG images accordingly. So first run:

composer require iamcal/php-emoji

Then we need to set chroot of Dompdf to the path to the PNG and CSS with emojis.

$options = new Options();
$options->setChroot(base_path('vendor/iamcal/php-emoji/lib'));
$dompdf = new Dompdf($options);

Include php-emoji CSS file in the HTML from which you are creating the PDF

<link href="absolute-path-to/vendor/iamcal/php-emoji/lib/emoji.css" rel="stylesheet" type="text/css" />

Use emoji_unified_to_html() function to replace emojis with PNG images

emoji_unified_to_html("<p>Html code with emojis: ❀️</p>")

Enjoy!


Subscribe to our newsletter

If you provide url of your website, we send you free design concept of one element (by our choice)

Subscribing to our newsletter, you comply with subscription terms and Privacy Policy