Display Unicode characters in converting Html to Pdf

前端 未结 5 1510
孤城傲影
孤城傲影 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:39

    You can also use the new XMLWorkerHelper (from library itextsharp.xmlworker), you need to override the default FontFactory implementation however.

    void GeneratePdfFromHtml()
    {
      const string outputFilename = @".\Files\report.pdf";
      const string inputFilename = @".\Files\report.html";
    
      using (var input = new FileStream(inputFilename, FileMode.Open))
      using (var output = new FileStream(outputFilename, FileMode.Create))
      {
        CreatePdf(input, output);
      }
    }
    
    void CreatePdf(Stream htmlInput, Stream pdfOutput)
    {
      using (var document = new Document(PageSize.A4, 30, 30, 30, 30))
      {
        var writer = PdfWriter.GetInstance(document, pdfOutput);
        var worker = XMLWorkerHelper.GetInstance();
    
        document.Open();
        worker.ParseXHtml(writer, document, htmlInput, null, Encoding.UTF8, new UnicodeFontFactory());
    
        document.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);
        }
    }
    

提交回复
热议问题