Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Itext PDF do not display correctly Myanmar Unicode Font

Tags:

java

pdf

itext

Itext 5 do not display correctly at generated pdf file for Myanmar Unicode fonts.

Itext version : 5.5.13.1

Expectation Result : သီဟိုဠ်မှ ဉာဏ်ကြီးရှင်သည်အာယုဝဎ္ဍနဆေးညွှန်းစာကို ဇလွန်ဈေးဘေးဗာဒံပင်ထက် အဓိဋ္ဌာန်လျက် ဂဃနဏဖတ်ခဲ့သည်။

Actual Result :

enter image description here

Google Drive Link for generated PDF.

My test string is similar with "The quick brown fox jump over the lazy dog" in English. It contains most of Myanmar alphabets.

Java program that I used to product above pdf

    String fileName = "sample.pdf";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        Document doc = new Document();
        PdfWriter writer = PdfWriter.getInstance(doc, baos);
        writer.setCloseStream(false);

        BaseFont unicode = BaseFont.createFont("/fonts/NotoSansMyanmar-Regular.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font myanmarUniCodeFont = new Font(unicode, 11, Font.NORMAL, BaseColor.BLACK);
        Rectangle pageSize = new Rectangle(PageSize.A4);
        doc.setPageSize(pageSize);
        doc.open();
        String textStr = "သီဟိုဠ်မှ ဉာဏ်ကြီးရှင်သည်အာယုဝဎ္ဍနဆေးညွှန်းစာကို ဇလွန်ဈေးဘေးဗာဒံပင်ထက် အဓိဋ္ဌာန်လျက် ဂဃနဏဖတ်ခဲ့သည်။";
        doc.add(new Paragraph(textStr, myanmarUniCodeFont));
        doc.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    response.setCharacterEncoding(StandardCharsets.UTF_8.name());
    response.setHeader("Cache-Control", "no-cache,no-store,max-age=0");
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Content-Disposition", "inline; filename=" + fileName);
    response.setContentType("application/pdf");
    response.setContentLength(baos.size());
    OutputStream os = response.getOutputStream();
    baos.writeTo(os);
    os.flush();
    os.close();
    baos.close();

Ouput texts are correct (you can copy and paste into a text editors like Notepad++ and see the result) but wrong display at pdf file.

What should I do to display correctly Myanmar Unicode Font by using itext-pdf-5 ?

Now I'm using dirty way to see the fonts readable. I converted all unicode strings to "Zawgyi Font" (This is another Myanmar font and we should never use this.) and embeded into pdf. This is not good solution and we can't promise all unicodes are correctly converted to Zawgyi-One font string and I don't want to convert unicode texts to non-standard texts. That's why I don't want to use this way.

Edited about ZawGyi Font with Itext

Some texts also do not render correctly with itext . Eg : သိန္နီ ၊ ဂွ

like image 604
Cataclysm Avatar asked May 15 '20 08:05

Cataclysm


1 Answers

(Full disclosure: I work for iText Software.)

iText 5 does not support proper Unicode based processing of the Myanmar writing system. Although iText 5 has a specific implementation for Arabic, the inherent limitations of its font infrastructure prevent support for font features that are needed for various other writing systems.

iText 7 improves on this with a new font implementation and an optional module (pdfCalligraph, not open source) to support different writing systems. However, Myanmar is not (yet) supported.

The corresponding iText 7 code looks like this:

PdfWriter writer = new PdfWriter(baos);
PdfDocument pdfdoc = new PdfDocument(writer);
Document doc = new Document(pdfdoc);

PdfFont f = PdfFontFactory.createFont("/fonts/NotoSansMyanmar-Regular.ttf",
    PdfEncodings.IDENTITY_H, true);

String textStr =
    "သီဟိုဠ်မှ ဉာဏ်ကြီးရှင်သည်အာယုဝဎ္ဍနဆေးညွှန်းစာကို ဇလွန်ဈေးဘေးဗာဒံပင်ထက် အဓိဋ္ဌာန်လျက် ဂဃနဏဖတ်ခဲ့သည်။";
// Explicit writing system
//doc.add(new Paragraph(textStr).setFont(f).setFontScript(Character.UnicodeScript.MYANMAR));
// Rely on autodetection
doc.add(new Paragraph(textStr).setFont(f));
doc.close();

Regardless whether pdfCalligraph is used or not, the rendering is still wrong:

Wrong Myanmar rendering

If a commercial license is an option for you, please submit this feature request. Additional writing systems are still actively added. If not, I'm afraid this will not be possible with iText and you'll have to find another solution.

like image 97
rhens Avatar answered Nov 12 '22 05:11

rhens