Itext 5.5 Converting HTML to PDF for RTL languages (Arabic) not working on tomcat

你说的曾经没有我的故事 提交于 2019-12-12 04:17:41

问题


I am wiriting HTML to PDF Conversion of Arabic Text from Itext 5.5 and XMLWorker .

This works perfectly fine when run stand alone Java program. (it sprint language in RTL as expected) but always print LTR when run the same program in tomcat. (tried even with hardcoded string, file inside tomcat application code).

Here is the sample code . Taken from http://developers.itextpdf.com/question/how-convert-arabic-html-pdf

public void createPdf(File file)
        throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer =
            PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        // Styles
        CSSResolver cssResolver = new StyleAttrCSSResolver();
        XMLWorkerFontProvider fontProvider =
            new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        fontProvider.register("/Users/ashish/Downloads/NotoNaskhArabicRegular.ttf");
        CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

        // Pipelines
        PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

        // XML Worker
        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);
        String htmlContentAr ="<table><tr><td>String of Arabia</td><td dir=\"rtl\" style=\"font-family: Noto Naskh Arabic\">لورانس العرب</td></tr></table>";

        //p.parse(new FileInputStream(HTML), Charset.forName("UTF-8"));
        p.parse( new ByteArrayInputStream(htmlContentAr.getBytes(StandardCharsets.UTF_8)), Charset.forName("UTF-8"));

        // step 5
        document.close();
    }

回答1:


Sorry. Silly issue. Problem was In my war deployment, 2 version of itext were getting copied which was causing issue.

This works fine with 5.5.5 Jar of Itext and Xmlworker .



来源:https://stackoverflow.com/questions/43758109/itext-5-5-converting-html-to-pdf-for-rtl-languages-arabic-not-working-on-tomca

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