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>
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.)