HtmlRendererCore and PdfSharpCore do not render emojis

243 Views Asked by At

I am using the latest stable release (1.3.47) of PdfSharpCore (PdfSharp for .NET core projects). Recently, a customer put an emoji into a field that populated into the PDF receipt as blank boxes (like when you try to ctrl + backspace on some Windows applications).

After much research, it seems that PdfSharpCore and HtmlRendererCore only go up to UTF-8 and do not support surrogate pairs for emojis.

I have looked at this SO post and the GitHub link in that post. The GitHub link attached to that is for PdfSharp, not PdfSharpCore. I have looked at a few other stack overflow posts, but I cannot find them right now, and none of them worked. One post said to pass a Unicode setting to the XGraphics object and the XFont object, but neither rendered the emoji.

Here is what I have tried so far:

PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XFont m_font = new XFont("Verdana", 14, XFontStyle.Regular, new XPdfFontOptions(PdfFontEncoding.Unicode));
XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.MUH = PdfFontEncoding.Unicode;
gfx.DrawString("( ͡° ͜ʖ ͡°)", m_font, new XSolidBrush(XColor.Black), 100, 100);

This outputs: a bunch of empty boxes when it was supposed to be ( ͡° ͜ʖ ͡°) and .

If anyone has a workaround for this, it would be much appreciated. Or, if anyone knows of another free Pdf library or an Html to Pdf renderer, that would be much appreciated. I haave tried HtmlRendererdCore.PdfSharpCore, but that also has the same problems.

Note: This will be for commercial use, so any free libraries would be helpful, not paid libraries or "free" libraries that set a limit on the number of PDFs I can print.

Also, I am thinking of putting an issue to the GitHub here because of my problems.

1

There are 1 best solutions below

0
K J On

No need to raise a fresh issue there is an unanswered one here https://github.com/ststeiger/PdfSharpCore/issues/105

Why are these questions always not answered? Well, it's a problem that has no single answer.

You are expecting HTML emojis would convert to PDF but those are machine/application specific, as you will see below, thus no two will look "correct" as PDF by definition should not render as different from another PDF with same characters!

Emojis as text in a PDF need definition, here we can see the user input the string above as comments but the PDF render cannot translate other than as MojiBake

enter image description here

So if we query the file there is nothing, nothing at all ! no fonts no text:

enter image description here

But we know that's incorrect:

enter image description here

So we need to store the font for the symbols and here they are now the page has nothing but the symbol font is embedded.


 

enter image description here

However we still get Mojibake as those should be the same symbols as used in the other application, the misshaping of symbols depends vastly on the skill of the PDF writer application as Emojis are way after PDF was designed for Desktop Publishing to Laser Printers.

So the best way to save as pdf is use the machines web fonts as images in a PDF by here using Save as if PDF.

enter image description here

enter image description here