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.
Please take a look at the ParseHtml10 example. In this example, we have take the file hebrew.html:
And we convert it to PDF using this code:
The result looks like hebrew.pdf:
What are the hurdles you need to take?
<div>
or a<td>
.dir="rtl"
to define the direction.I can't read Hebrew, but I hope the resulting PDF is correct and that this solves your problem.
Important: this solution requires at least iText and XML Worker 5.5.5, because support for the
dir
attribute was introduced in 5.5.4 and improved in 5.5.5.