Enable UTF-8 or set custom font with TheArtOfDev's HtmlRenderer.PdfSharp

1.6k views Asked by At

I'm using TheArtOfDev's HtmlRenderer.PdfSharp to convert an HTML string to PDF in a ASP.NET web application. The HTML contains Japanese characters. The Japanese symbols are converted to ☐ ascii characters.

How can I enable UTF-8 or use a custom Japanese font? I tried the following without results:

  • Adding <head><meta charset='utf-8'></head> to the HTML string.
  • Adding <style>@font-face { font-family: myFont; src: url('path/font.ttf'); }</style> to the HTML string and then settings the font style on the element with Japanese text.
  • Try a CSS file with @font-face and calling TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.ParseStyleSheet
2

There are 2 answers

2
Fede G On BEST ANSWER

I gave up on HTMLRenderer and PDFSharp and solved all my issues with TuesPechkin, a wrapper for wkhtmltopdf.

0
Brian Herbert On

Although it is not possible to include custom fonts in HTML with PdfSharp, you can use custom fonts with DrawString if you use IFontResolver to add your .ttf files.

Instead of using HTML and CSS to create a page layout, I used XGraphics.MeasureString and XGraphics.DrawString to create my own layout with PdfSharp. This avoids all of the problems with page breaks which I encountered with CSS.

var gfx = XGraphics.FromPdfPage(page);
var size = gfx.MeasureString(text, pageFont);
gfx.DrawString(text, pageFont, XBrushes.Black,
   new XRect(x, y, width, height),
   XStringFormats.BottomCenter);

I used Microsoft.Toolkit.Parsers.Markdown to parse a markdown string which I rendered to a PDF with MeasureString and DrawString. You may need to create a mechanism for adding line breaks and page breaks if you are not going to use HTML/CSS.