我正在使用HTML格式转换PDF文件来自Itext 5.5.11和ITextRenderer的阿拉伯语文本转换,但这些字母是颠倒的
我试着按照这个问题的答案, Arabic characters from html content to pdf using iText
但问题总是摆出来
我的功能就像:
public void createPdf(String templateName, Map map) throws Exception {
Assert.notNull(templateName, "The templateName can not be null");
Context ctx = new Context();
if (map != null) {
Iterator itMap = map.entrySet().iterator();
while (itMap.hasNext()) {
Map.Entry pair = (Map.Entry) itMap.next();
ctx.setVariable(pair.getKey().toString(), pair.getValue());
}
}
String processedHtml = templateEngine.process(templateName, ctx);
FileOutputStream os = null;
//String fileName = UUID.randomUUID().toString();
try {
// final File outputFile = File.createTempFile(fileName, ".pdf");
os = new FileOutputStream(new File(directoryPathConfig.getPathDefaultDirectory("pdfOutput.path")).getAbsolutePath() + "/World.pdf");
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(processedHtml,new ClassPathResource("/static/").getURL().toExternalForm());
renderer.getFontResolver().addFont("fonts/NotoNaskhArabic-Regular.ttf", "Noto Naskh Arabic", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, null);
renderer.layout();
renderer.createPDF(os, false);
renderer.finishPDF();
System.out.println("PDF created successfully");
}
finally {
if (os != null) {
try {
os.close();
} catch (IOException e) { /*ignore*/ }
}
}
}
在我的模板中,我这样做了:
<p dir="rtl" style="font-family: Noto Naskh Arabic;text-align: center;text-decoration: underline; font-size: 18px;">الشباك الوحيد للجماعة</p>