0
我正在將HTML轉換爲PDF從Itext 5.5和XMLWorker轉換阿拉伯文文本。Itext 5.5將HTML轉換爲RTL語言的PDF(阿拉伯語)不能在tomcat上工作
運行獨立的Java程序時,此功能非常好。 (它按預期在RTL中衝刺語言),但當在tomcat中運行相同的程序時總是打印LTR。 (即使使用硬編碼字符串,tomcat應用程序代碼中的文件也是如此)。
以下是示例代碼。取自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();
}