OpenPDF
OpenPDF copied to clipboard
Arabic characters are not getting rendered properly when tried with FOP
Describe the bug The arabic characters not being rendered properly in the PDF when using below font with FOP. If I do not use the FOP, the characters appears properly.
- majalla
Same issue is coming with Arial and Noto as well.
To Reproduce Code to reproduce the issue
- Refer below Sample Code for Unit test
public class MultipleDirTest2 {
@Test
public void multiDirTest(){
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = null;
FontFactory.register("D:/workspace/windows/fonts/majalla.ttf");
try {
writer = PdfWriter.getInstance(document, new FileOutputStream("NotWorking_UT_arab__majalla_with_FOP.pdf"));
try {
Class.forName("org.apache.fop.complexscripts.util.GlyphSequence");
} catch (ClassNotFoundException e) {
System.out.println("DO");
}
document.open();
PdfContentByte cb = writer.getDirectContent();
BaseFont bf = BaseFont.createFont("D:\\workspace\\windows\\fonts\\majalla.ttf", BaseFont.IDENTITY_H, true);
Font font1 = new Font(bf, 24, Font.NORMAL, Color.BLUE);
float llx = 100;
float lly = 100;
float urx = 500;
float ury = 800;
Paragraph element1 = new Paragraph();
Paragraph element2 = new Paragraph();
String ar9 = "مبلغ الإيصال المستلم"; //the input arabic string(word)
element1.add(new Chunk(ar9 ,font1));
element1.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
element2.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(llx,lly,urx,ury);
ct.addElement(element1);
ct.go();
document.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
For getting the correct output I have updated the else block of method "convertToBytes" in class com.lowagie.text.pdf.FontDetails.java . New method else block will look like below. Once I use below updated FontDetails class, the above test case results in correct output.
else {
/*String fileName = ((TrueTypeFontUnicode)getBaseFont()).fileName;
if (FopGlyphProcessor.isFopSupported() && (fileName!=null && fileName.length()>0
&&( fileName.contains(".ttf") || fileName.contains(".TTF")))){
return FopGlyphProcessor.convertToBytesWithGlyphs(ttu,text,fileName,longTag);
}else {
return convertToBytesWithGlyphs(text);
}*/
return convertToBytesWithGlyphs(text);
}
Actual Output
Expected behavior
Screenshots screenshots added above.
System (please complete the following information):
- OS: Windows 10 Enterprise
- Used Font: sakkal majalla
Additional context Add any other context about the problem here.
Hello, can you please submit a pull request to solve this bug? It seems like you have described the solution here.
@nucleussoftwareopen
Yes, please submit a pull request. Also because it is probably easier to solve this, if you can understand the given alphabet (I can't, sorry)
I had the same issue before, the issue is with the used font. Some fonts for some reason will pick a wrong glyph. Although the issue was not there in itext older versions, but changing the font to this one fixed the issue for me. https://github.com/LibrePDF/OpenPDF/blob/master/pdf-toolbox/src/test/resources/com/lowagie/examples/fonts/noto/NotoSansArabic-Regular.ttf