0

When i am converting html to pdf in C# the arabic text is broken and also seems to be reversed. I have tried direction rtl, adding arabic fonts.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 properties = new ConverterProperties();

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

fontProvider.AddFont("C:/Users/fahad.a/Downloads/NotoNaskhArabic-Regular.ttf"); // Ensure the path is correct fontProvider.AddFont("C:/Users/fahad.a/Downloads/NotoKufiArabic-Regular.ttf"); // For bold text, if needed properties.SetFontProvider(fontProvider); properties.SetCharset("UTF-8"); // Ensure UTF-8 charset to handle Arabic script

// Add default styles properties.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, properties);

pdfDocument.Close(); enter image description here

10
  • 1
    you also need to install itext.pdfcalligraph that is listed on your screenshot (this add-on is not dual licensed, like itext core, or pdfhtml). Commented Jun 24, 2024 at 10:55
  • so i have to buy it? Commented Jun 24, 2024 at 11:03
  • itext.pdfcalligraph is already installed Commented Jun 24, 2024 at 11:05
  • @AndréLemos what should i do after buying the licensed of it)? can you please help me Commented Jun 24, 2024 at 12:08
  • with pdfCalligraph loaded, then things should work automagically. there's no special API that needs to get used. You can get a trial license to see if it works for you. Commented Jun 25, 2024 at 9:24

1 Answer 1

2

when using the following iText code to convert from HTML to PDF I get the exepected result:

 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); } 

It is important that you also load the pdfCalligraph Add On and that it is included in your license. The C# project file should contain at least:

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

For testing, I use the following sample 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> 

And the result is:

enter image description here

hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.