Itext 5.5 Converting HTML to PDF for RTL languages

2019-09-15 08:29发布

问题:

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 .