phppdffpdf

How to add emoji to pdf which is using FPDF?


I want to include heart emoji, gift emoji and smily emoji in to a pdf file. Here I have used FPDF library.

$pdf->AddFont('NotoEmoji', '', 'NotoEmoji-Regular.ttf');
 $pdf->SetFont('NotoEmoji', '', 12);

// insert emojis
 $pdf->MultiCell(10, 'ā¤ļø ', 0, 'L'); // Heart emoji
 $pdf->MultiCell(10, 'šŸ˜Š', 0, 'L'); // smily emoji

But the above is not working. Can anyone help me to fix this issue?


Solution

  • PDF components cannot support embedding fonts in more than 2 colours

    Thus a workaround is needed to support HTML or SVG coloured fonts.

    Such fonts are often tied to the underlying operating system, so we can have Android, Apple or Windows variations.

    For NotoColorEmoji

    "NotoColorEmoji uses the CBDT/CBLC color font format, which is supported by Android and Chrome/Chromium OS. Windows supports it starting with Windows 10 Anniversary Update in Chrome and Edge. On macOS, only Chrome supports it,"

    What a HTML browser editor does is write both the monochrome symbols 2764 (Black Heart) & FE0F (invisible plane) and overlaid with either a PNG image import (PDF also does not use raw PNG but a modified overlaid pair of images RGB & Grey), or a set of colour vectors.

    Thus in a PDF we will find the 2 tone symbol and 2 other objects.

    ā¤ļø on this page is seen as "Arial" ? because that is the underlying default symbol, but in reality since generated in Edge on Windows the PDF contains Segoe UI Emoji

    17 0 obj
    <</CapHeight 1434/Flags 4/FontFamily(Segoe UI Emoji)/FontName/FAAAAA+SegoeUIEmoji/FontStretch/Normal/FontWeight 400/ItalicAngle 0/StemV 109/Type/FontDescriptor/XHeight 1024>>
    endobj
    

    enter image description here

    However if you try to query such fonts they are not really "named" fonts just their embedment of characters.

    enter image description here

    and the characters are just binary numbers so the heart is a number that looks like ASCII (Q) whilst the smile is the hex number seen as <C0> between BeginText BT and EndText ET

    BT
    /F0 12 Tf
    1 0 0 -1 225.98828 267 Tm
    (Q) Tj
    ET
    EMC
    EMC
    /P <</MCID 64>> BDC
    BT
    /F1 12 Tf
    1 0 0 -1 225.98828 282 Tm
    <c0> Tj
    ET
    

    The in page <c0> will be converted later into Unicode equivalence for display beginbfchar <C0> <D83DDE0A> endbfchar endcmap

    If you try copy and paste the font graphics you will see they are just the underlying mono.

    enter image description here

    Some PDFwriters may simply discard the fonts and just use the font supplied colour components so 3 for the heart and 4 for the face. enter image description here

    One "Answer"

    To the FPDF (or other non HTML libraries) problem is simply use a monochrome symbol font for the character layout/spacing then overlay a small iconic image (lossless jpg or png are good).

    Much the same as a browser may do it. this does make the task more complex than desirable, but may be more space efficient, than import a massive graphical font with all its mapping problems.

    Other methods such as using the Mozilla "browsers" supplied CBDT/CBLC color font format at https://github.com/mozilla/twemoji-colr or icons from Font Awesome etc may make the task much easier.