Display Unicode characters in converting Html to Pdf

前端 未结 5 1507
孤城傲影
孤城傲影 2020-11-28 10:33

I am using itextsharp dll to convert HTML to PDF.

The HTML has some Unicode characters like α, β... when I try to convert HTML to PDF, Unicode characters are not sho

5条回答
  •  庸人自扰
    2020-11-28 11:27

    This has to be one of the most difficult problems that I've had to figure out to date. The answers on the web, including stack overflow has either poor or outdated information. The answer from Gregor is very close. I wanted to give back to this community because I spent many hours to get to this answer.

    Here's a very simple program I wrote in c# as an example for my own notes.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using iTextSharp.tool.xml;
    
    namespace ExampleOfExportingPDF
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Build HTML document
                StringBuilder sb = new StringBuilder();
                sb.Append("");
                sb.Append("

    これは日本語のテキストの例です。

    "); sb.Append(""); //Create our document object Document Doc = new Document(PageSize.A4); //Create our file stream using (FileStream fs = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf"), FileMode.Create, FileAccess.Write, FileShare.Read)) { //Bind PDF writer to document and stream PdfWriter writer = PdfWriter.GetInstance(Doc, fs); //Open document for writing Doc.Open(); //Add a page Doc.NewPage(); MemoryStream msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(sb.ToString())); XMLWorkerHelper.GetInstance().ParseXHtml(writer, Doc, msHtml, null, Encoding.UTF8, new UnicodeFontFactory()); //Close the PDF Doc.Close(); } } public class UnicodeFontFactory : FontFactoryImp { private static readonly string FontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "arialuni.ttf"); private readonly BaseFont _baseFont; public UnicodeFontFactory() { _baseFont = BaseFont.CreateFont(FontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); } public override Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached) { return new Font(_baseFont, size, style, color); } } } }

    Hopefully this will save someone some time in the future.

提交回复
热议问题