Itext Html 到 PDF 阿拉伯语文本损坏

问题描述 投票:0回答:1

当我在 C# 中将 html 转换为 pdf 时,阿拉伯文本被破坏,而且似乎也被颠倒了。 我尝试过方向rtl,添加阿拉伯字体。enter image description here

enter image description here

.arabic {
      direction: rtl;
      unicode-bidi: isolate;
      font-family: 'NotoNaskhArabic', sans-serif;
  }

  @@font-face {
      font-family: 'NotoNaskhArabic';
      src: url('C:/Users/fahad.a/Downloads/NotoNaskhArabic-Regular.ttf') format('truetype');
  }


 <td class="tdh" style="font-family: 'NotoNaskhArabic', sans-serif;">Date<span class="arabic" id="arabic" style="display:none; direction: rtl !important; font-family: 'NotoNaskhArabic', sans-serif;"><br>تاريخ الطلب</span></td>

ConverterProperties 属性 = new ConverterProperties();

FontProvider fontProvider = new FontProvider(); fontProvider.AddStandardPdfFonts();

fontProvider.AddFont("C:/Users/fahad.a/Downloads/NotoNaskhArabic-Regular.ttf"); // 确保路径正确 fontProvider.AddFont("C:/Users/fahad.a/Downloads/NotoKufiArabic-Regular.ttf"); // 对于粗体文本(如果需要) 属性.SetFontProvider(fontProvider); 属性.SetCharset("UTF-8"); // 确保 UTF-8 字符集来处理阿拉伯文字

//添加默认样式 属性.SetCssApplierFactory(new DefaultCssApplierFactory());

iText.Kernel.Pdf.PdfWriter writer = new iText.Kernel.Pdf.PdfWriter(destination, new WriterProperties().SetFullCompressionMode(true)); iText.Kernel.Pdf.PdfDocument pdfDocument = new iText.Kernel.Pdf.PdfDocument(writer);

//iText.Kernel.Font.PdfFont f = PdfFontFactory.CreateFont("/fonts/NotoSansMyanmar-Regular.ttf", PdfEncodings.IDENTITY_H, true);

HtmlConverter.ConvertToPdf(htmltrimmed, pdfDocument, 属性);

pdfDocument.Close(); enter image description here

c# html itext arabic html2pdf
1个回答
0
投票

当使用以下 iText 代码从 HTML 转换为 PDF 时,我得到了预期的结果:

    LicenseKey.LoadLicenseFile(new FileInfo("licensefile.json"));
    using (FileStream htmlSource = File.Open("input.html", FileMode.Open))
    using (FileStream pdfDest = File.Open("output.pdf", FileMode.Create))
    {
      ConverterProperties properties = new ConverterProperties();
      FontProvider fontProvider = new FontProvider();
      fontProvider.AddStandardPdfFonts();
      fontProvider.AddFont("NotoNaskhArabic-Regular.ttf");
      properties.SetFontProvider(fontProvider);
      properties.SetCharset("UTF-8");
      HtmlConverter.ConvertToPdf(htmlSource, pdfDest, properties);
    }

重要的是,您还需要加载 pdfCalligraph Add On,并且它包含在您的许可证中。 C# 项目文件应至少包含:

  <ItemGroup>
    <PackageReference Include="itext7" Version="8.0.4" />
    <PackageReference Include="itext7.bouncy-castle-adapter" Version="8.0.4" />
    <PackageReference Include="itext.licensing.base" Version="4.1.4" />
    <PackageReference Include="itext.licensing.remote" Version="4.1.4" />
    <PackageReference Include="itext7.pdfcalligraph" Version="4.0.2" />
    <PackageReference Include="itext7.pdfhtml" Version="5.0.4" />
  </ItemGroup>

为了进行测试,我使用以下示例 HTML (input.html`)

<html>
<style>
    .rabic {
        direction: rtl;
        unicode-bidi: isolate;
        font-family: 'NotoNaskhArabic', sans-serif;
    }
  
    @font-face {
        font-family: 'NotoNaskhArabic';
        src: url('NotoNaskhArabic-Regular.ttf') format('truetype');
    }
</style>
<body>
    <div style="font-family: 'NotoNaskhArabic', sans-serif;">Date<span class="arabic" id="arabic" style="direction: rtl !important; font-family: 'NotoNaskhArabic', sans-serif;"><br>تاريخ الطلب</span></div>
</body> 
  
</html>

结果是:

enter image description here

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.