I'm trying to convert *.xhtml with Hebrew characters (UTF-8) to PDF by using iText library but I getting all letter in reverse order.
As far I understand from this question I can set RTL only for ColumnText
and PdfCell
objects:
Arabic (and Hebrew) can only be rendered correctly in the context of ColumnText and PdfPCell.
So I doubt is it possible to convert whole *.xhtml page to PDF?
This is an *.xhtml file which I try to import:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title of document</title>
</head>
<body style="font-size:12.0pt; font-family:Arial">
שלום עולם
</body>
</html>
And this is Java code which I use:
public static void convert() throws Exception{
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("import.pdf"));
writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
document.open();
String str = null;
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("import.xhtml"), "UTF8"));
StringBuilder sb = new StringBuilder();
while ((str = in.readLine()) != null) {
System.out.println(str);
sb.append(str);
}
in.close();
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
InputStream is = new ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8));
worker.parseXHtml(writer, document, is, Charset.forName("UTF-8"));
document.close();
}
}
This is what I get until now:
Thank you for any help.
答案 0 :(得分:1)
请查看ParseHtml10示例。在此示例中,我们将文件hebrew.html:
<html>
<head>
<title>Hebrew text</title>
</head>
<body style="font-size:12.0pt; font-family:Arial">
<div dir="rtl" style="font-family: Noto Sans Hebrew">שלום עולם</div>
</body>
</html>
我们使用以下代码将其转换为PDF:
public void createPdf(String 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("resources/fonts/NotoSansHebrew-Regular.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);
p.parse(new FileInputStream(HTML), Charset.forName("UTF-8"));;
// step 5
document.close();
}
结果如hebrew.pdf:
您需要采取哪些障碍?
<div>
或<td>
等元素。dir="rtl"
来定义方向。我无法阅读希伯来语,但我希望生成的PDF文件正确无误,这样可以解决您的问题。
重要提示:此解决方案至少需要iText和XML Worker 5.5.5,因为5.5.4中引入了对dir
属性的支持,并在5.5.5中进行了改进}。