FlyingSaucer LTR/RTL/BiDi issue with arabic text

我的未来我决定 提交于 2019-12-09 11:24:19

问题


I'm using flying saucer xhtmlrenderer for building pdf documents. Everything worked fine until now - now we should generate arabic text inside pdf. Xhtmlrenderer is rendering Arabic text in reverse order.

I've read somewhere on internet (maybe on their own site) that xhtmlrenderer does not support bidi/rtl. But IText itself contains examples to work with arabic and hebrew via ColumnText and PdfPTable (sources can be found here: http://sourceforge.net/projects/itextpdf/files/Examples/examples-155/examples-155.zip/download - arabic_hebrew.java), and those work fine.

I tried to use itext api in xhtmlrenderer's ReplacedElementFactory/ITextReplacedElement, but could not find good examples for positioning elements. Does anyone tried to do this? Or maybe there is a simplier (or at least working) solution?


回答1:


Same issue I was facing, only solution i can find out was using arial fonts import/add arial.ttf and arialbold.ttf files in resources folder of your project.

            OutputStream outputStream = response.getOutputStream();
        ITextRenderer renderer = new ITextRenderer();
        // renderer.getFontResolver().addFont("/fonts/arialbold.ttf",
        // BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
        renderer.getFontResolver().addFont("/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        renderer.getFontResolver().addFont("/fonts/arialbold.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        // SharedContext sharedContext = renderer.getSharedContext();
        // sharedContext.setPrint(true);
        // sharedContext.setInteractive(false);
        // sharedContext.setReplacedElementFactory(new B64ImgReplacedElementFactory());
        // sharedContext.getTextRenderer().setSmoothingThreshold(0);

        renderer.setDocumentFromString(content);
        renderer.layout();
        renderer.createPDF(outputStream);
        renderer.finishPDF();
        outputStream.close();

in your css use

html, body {
 margin: 0;
 padding: 0;
 font-family: Arial, Arial Bold;
 font-size: 10px;
 line-height: 14px;
}



回答2:


Finally I'm able to print arabic text in rtl/ltr using flying saucer. In my code I'm giving width and alignment for every arabic text block, but in general it works fine. (Edited) Code is large to print it down here, please find the code on Google groups, the links are in the comments.



来源:https://stackoverflow.com/questions/6288622/flyingsaucer-ltr-rtl-bidi-issue-with-arabic-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!