使用iTextSharp 5.5.8将html转换为PDF时,未显示希伯来语内容?

时间:2015-12-18 11:32:46

标签: c# .net itextsharp html-to-pdf

我使用以下代码使用iTextSharp将Html文件转换为Pdf

    Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 20, 5, 35);
    var writer = PdfWriter.GetInstance(doc, new FileStream(savePath, FileMode.Create));

    var xmlWorkerFontProvider = new XMLWorkerFontProvider();
    var cssAppliers = new CssAppliersImpl(new MyFontProvider());
    CssFilesImpl cssFiles = new CssFilesImpl();
    StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);

    HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
    htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
    htmlContext.SetImageProvider(new ITextImageHandler());

    IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(doc, writer)));
    XMLWorker worker = new XMLWorker(pipeline, true);
    XMLParser xmlParser = new XMLParser(true, worker, Encoding.Unicode);

    doc.Open();
    doc.NewPage();
    xmlParser.Parse(new StringReader(htmlString.ToString()));
    doc.Close();

对于英文内容,此功能正常。但如果内容是希伯来语,则文本不会显示在PDF中。

我已经在Stack-overflow上检查了与此相关的其他答案,但它们似乎使用了不推荐使用的HtmlParser。所以我不想用它。

如果有任何其他要求,请告诉我。谢谢你的时间。

编辑:阅读完评论后,我也尝试设置字体。但仍然没有运气。以下是更新后的代码。

 Document document = new Document();

        PdfWriter writer =
            PdfWriter.GetInstance(document, new FileStream(savePath, FileMode.Create));

        document.Open();

        var cssResolver = new StyleAttrCSSResolver();
        XMLWorkerFontProvider fontProvider =
            new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        fontProvider.Register(@"E:\fonts\NotoSansHebrew-Regular.ttf");


        CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
        htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
        htmlContext.SetImageProvider(new ITextImageHandler());


        PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);


        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);

        p.Parse(new StringReader(htmlString.ToString()));

        document.Close();

1 个答案:

答案 0 :(得分:2)

以下是Bruno的代码与一些实际HTML的改编。要运行它,您只需下载字体Noto Sans Hebrew并将其放在桌面上即可。没有任何修改(可能文件路径除外)尝试运行这个适用于我的代码。 (我对5.5.5进行了测试,所以5.5.8应该绝对有效。)

var file = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
var fontFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "NotoSansHebrew-Regular.ttf");
var htmlText = @"<div dir=""rtl"" style=""font-family: Noto Sans Hebrew;"">שלום עולם</div>";

using (var FS = new System.IO.FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None)) {
    using (var document = new Document()) {
        using (var writer = PdfWriter.GetInstance(document, FS)) {
            document.Open();

            var cssResolver = new StyleAttrCSSResolver();
            var fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
            fontProvider.Register(fontFile);
            var cssAppliers = new CssAppliersImpl(fontProvider);
            var htmlContext = new HtmlPipelineContext(cssAppliers);
            htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());

            var pdf = new PdfWriterPipeline(document, writer);
            var html = new HtmlPipeline(htmlContext, pdf);
            var css = new CssResolverPipeline(cssResolver, html);


            var worker = new XMLWorker(css, true);
            var p = new XMLParser(worker);

            using (var ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlText))) {
                using (var sr = new StreamReader(ms)) {
                    p.Parse(sr);
                }
            }

            document.Close();
        }
    }
}

这一切的诀窍是获取HTML中字体的确切名称,就像在字体文件中一样。令人困惑的是,字体实际上可以在其中包含一堆名称。字体越旧,它就越有可能拥有这些字体。如果我没记错的话,iText有一些启发式来确定字体名称,但是如果你想安全地玩它,你也可以使用别名并随意调用它。例如,您可以将HTML更改为:

var htmlText = @"<div dir=""rtl"" style=""font-family: Gerp;"">שלום עולם</div>";

只要您在注册字体时使用别名,一切都会正常工作:

fontProvider.Register(fontFile, "Gerp");