asp.net-core - 从 html 转换为 pdf 时,iTextSharp LGPLv2.Core 无法在 rtl 中显示阿拉伯语
问题描述
我使用iTextSharp LGPLv2.Core将 html 转换为带有阿拉伯内容的 pdf。
转换 pdf 时,阿拉伯文本显示 LTR 而不是 RTL 和分隔字符,如下所示
我已经注册了三种字体,它们都不起作用:
Tahoma:显示 LTR 和分离。
Arabtype:显示 LTR 和分离。
arialuni:根本不显示阿拉伯字符。
这是我所做的代码片段:
try
{
var paymentReceipt = await _appointmentAppService.GetPaymentReceiptAsync(checkOutId);
string invoiceHtml = RenderView("PaymentReceipt", paymentReceipt);
using (MemoryStream stream = new System.IO.MemoryStream())
{
var styleSheet = new StyleSheet();
styleSheet.LoadTagStyle(HtmlTags.BODY, "encoding", "Identity-H");
styleSheet.LoadTagStyle(HtmlTags.BODY, HtmlTags.FONT, "arabic");
string webRootPath = _webHostEnvironment.WebRootPath;
string fontPath = Path.Combine(webRootPath, "fonts", "ARABTYPE.TTF");
FontFactory.Register(fontPath, "arabic");
//FontFactory.Register(TestUtils.GetTahomaFontPath());
//FontFactory.Register(TestUtils.GetArialUnicodeMSFontPath());
var unicodeFontProvider = FontFactoryImp.Instance;
unicodeFontProvider.DefaultEmbedding = BaseFont.EMBEDDED;
unicodeFontProvider.DefaultEncoding = BaseFont.IDENTITY_H;
var props = new Hashtable
{
{ "font_factory", unicodeFontProvider }
};
var document = new Document();
var pdfWriter = PdfWriter.GetInstance(document, stream);
document.AddAuthor(TestUtils.Author);
document.Open();
var objects = HtmlWorker.ParseToList(
new StringReader(invoiceHtml),
styleSheet,
props
);
foreach (IElement element in objects)
{
document.Add(element);
}
document.Close();
return File(stream.ToArray(), "application/pdf");
}
}
catch (Exception ex)
{
throw;
}
解决方案
推荐阅读
- powershell - 从 Windows Powershell 卸载 linux
- c++ - 从文件中读取日期
- java - 问题 - ElasticSearch Baeldung 示例(Maven 依赖项)
- python - 如何在前端与 python 终端交互
- wagtail - 将 wagtail 从 2.11 升级到最新的 2.14 后的 PageChooserBlock UI 管理问题
- ansible - 按需反转库存订单
- mysql - 通过 id mysql 查找具有最新日期的行
- html - 在 CSS 中指定 0px 填充的正确方法是什么?
- amazon-web-services - 如何将 IAM OIDC 身份提供者与带有 CDK 的 EKS 集群相关联?
- python - python脚本运行时如何在弹出窗口中显示消息