无法使用itextsharp将希伯来语HTML文件解析为pdf

时间:2019-03-04 12:59:49

标签: c# pdf itext hebrew xmlworker

我正在尝试使用iTEXTsharp和xmlworker 5.5.13在MVC应用程序中将包含希伯来语的HTML字符串解析为pdf。我无法在页面上显示希伯来语。我尝试在此post之后建模,但似乎无济于事。我已经将HTML简化为以下内容:

<!DOCTYPE html>
   <html dir="rtl">
   <head>
    <meta charset="utf-8" /> 
   </head>
   <body>
   <div dir="rtl" style="font-family: David"></div>
    <div class="container body-content">
     <div> שלום עולם  </div>
    <div>hello world</div>
    </div>   
   </body>
   </html>
pdf文件中仅显示“ hello world”。这是我的pdf代码。以下是我生成pdf的代码。我尝试了不同的字体,包括Arial,NotoSansHebrew,但是结果是相同的。删除cssfile也不能解决问题。
        公共MemoryStream mergepdfs(字符串myserverpath,                ControllerContext mycc,字符串Viewname,对象模型)         {             文档mydoc = new Document();             MemoryStream mystream =新的System.IO.MemoryStream();

       PdfWriter writer = PdfWriter.GetInstance(mydoc, mystream);
        writer.CloseStream = false;
        mydoc.Open();
        PdfReader reader;
        PdfContentByte cb = writer.DirectContent;
        PdfImportedPage Pdfim;
        string myxhtml;
        myxhtml = (function that generates HTML shown above);
        this.createpagefromxhtml(mydoc, writer, myxhtml, true);

        mydoc.Close();

        return mystream;
    }
    public bool createpagefromxhtml(Document mydoc, PdfWriter mywriter, string myxhtml, bool isnewpage)
    {
        StringReader sr = new System.IO.StringReader(myxhtml);
        try
        {
            using (mydoc)
            {
                if (isnewpage)
                { mydoc.NewPage();}

                FontFactory.RegisterDirectories();

                // Set factories
                ICSSResolver cssResolver = new StyleAttrCSSResolver();
                XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
                fontProvider.Register("C:\\Windows\\Fonts\\David.ttf");
                CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);

                var htmlContext = new HtmlPipelineContext(null);
                htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());

                // Set css
               cssResolver.AddCssFile(HttpContext.Current.Server.MapPath("~/Content/Site.css"), true);
                cssResolver.AddCssFile(HttpContext.Current.Server.MapPath("~/Content/bootstrap.min.css"), true);
                cssResolver.AddCssFile(HttpContext.Current.Server.MapPath("~/Content/bootstrap-rtl.min.css"), true);

                // Export
                IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(mydoc, mywriter)));
                var worker = new XMLWorker(pipeline, true);
                var xmlParse = new XMLParser(true, worker);
                xmlParse.Parse(sr);
                xmlParse.Flush();
                return true;
            }
        }
        catch (Exception ex)
        {  return false;}
    }

(该流已保存到数据库并以文件形式打开,尽管我尝试将其保存到磁盘并获得相同的结果。)

1 个答案:

答案 0 :(得分:0)

我终于使它起作用了。我必须将html包装在div中

<div dir="rtl" style="font-family:  David"> .... text... </div>
我想关键是要定义页面的字体系列,使其与fontfamily.register方法中的字体相同。