我正在尝试使用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>
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;}
}
(该流已保存到数据库并以文件形式打开,尽管我尝试将其保存到磁盘并获得相同的结果。)
答案 0 :(得分:0)
我终于使它起作用了。我必须将html包装在div中
<div dir="rtl" style="font-family: David"> .... text... </div>