0

I am trying to parse an HTML string which contains Hebrew to pdf in an MVC application using iTEXTsharp and xmlworker 5.5.13. I cannot get the Hebrew to show up on the page. I tried to model after this post but nothing I do seems to help. I have simplified the HTML to the following:

<!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>
only 'hello world' shows up in the pdf file. Here is my code for the pdf. Below is my code to generate the pdf. I have tried different fonts including Arial, NotoSansHebrew but the result is the same. Deleting the cssfile also do not solve the problem.
public MemoryStream mergepdfs(string myserverpath, ControllerContext mycc, string Viewname, Object model) { Document mydoc = new Document(); MemoryStream mystream = new 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;}
    }

(the stream is saved to a database and opened as a file, though I tried saving to disk and got the same result.)

Haim Katz
  • 455
  • 3
  • 15
  • Is you ttf file similar to : "resources/fonts/NotoSansHebrew-Regular.ttf" Try TTF from here : https://github.com/jenskutilek/free-fonts/tree/master/Open/Open%20Sans%20Hebrew/TTF – jdweng Mar 04 '19 at 13:14
  • If I use "resources/fonts/….ttf" I get an error. I have to use the windows font directory. I tried NotoSansHebrew, it gave the same result. – Haim Katz Mar 04 '19 at 14:01

1 Answers1

0

I finally got it to work. I had to wrap my html in a div

<div dir="rtl" style="font-family:  David"> .... text... </div>
I guess the key was defining the font family of the page as the same as the font in the fontfamily.register method.
Haim Katz
  • 455
  • 3
  • 15