Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

Arabic characters from html content to pdf using iText

I am having trouble to display the Arabic Characters from HTML Content in PDF Generation as "?"

I am able to display the Arabic text from String variable. At the same time I am not able to generate the Arabic Text from the HTML String.

I want to display the PDF with two column, left side English and the right side Arabic Text.

when I use the following program to convert into pdf. Please help me in this regard.

try
{
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PdfWriter writer = PdfWriter.getInstance(document, out);
    BaseFont bf = BaseFont.createFont("C:\arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    Font font = new Font(bf, 8);
    document.open();

    BufferedReader br = new BufferedReader(new FileReader("C:\style.css"));
    StringBuffer fileContents = new StringBuffer();
    String line = br.readLine();
    while (line != null)
    {
        fileContents.append(line);
        line = br.readLine();
    }
    br.close();
    String styles = fileContents.toString(); //"p { font-family: Arial;}"; 

    Paragraph cirNoEn = null;
    Paragraph cirNoAr = null;

    String htmlContentEn = null;
    String htmlContentAr = null;

    PdfPCell contentEnCell = new PdfPCell();
    PdfPCell contentArCell = new PdfPCell();

    cirNoEn = new Paragraph("Circular No. (" + cirEnNo + ")", new Font(bf, 14, Font.BOLD | Font.UNDERLINE));
    cirNoAr = new Paragraph("??? ??????? (" + cirArNo + ")", new Font(bf, 14, Font.BOLD | Font.UNDERLINE));

    htmlContentEn = “< p >< span > Dear….</ span ></ p >”;
    htmlContentAr = “< p >< span > ??? ??????? ??? ??????? </ p >< p > ??? ??????? ….</ span ></ p >”;
    for (Element e : XMLWorkerHelper.parseToElementList(htmlContentEn, styles))
    {
        for (Chunk c : e.getChunks())
        {
            c.setFont(new Font(bf));
        }
        contentEnCell.addElement(e);
    }
    for (Element e : XMLWorkerHelper.parseToElementList(htmlContentAr, styles))
    {
        for (Chunk c:e.getChunks())
        {
            c.setFont(new Font(bf));
        }
        contentArCell.addElement(e);
    }

    PdfPCell emptyCell = new PdfPCell();
    PdfPCell cirNoEnCell = new PdfPCell(cirNoEn);
    PdfPCell cirNoArCell = new PdfPCell(cirNoAr);

    cirNoEnCell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cirNoArCell.setHorizontalAlignment(Element.ALIGN_CENTER);

    emptyCell.setBorder(Rectangle.NO_BORDER);
    emptyCell.setFixedHeight(15);

    cirNoEnCell.setBorder(Rectangle.NO_BORDER);
    cirNoArCell.setBorder(Rectangle.NO_BORDER);
    contentEnCell.setBorder(Rectangle.NO_BORDER);
    contentArCell.setBorder(Rectangle.NO_BORDER);

    cirNoArCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
    contentArCell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);

    contentEnCell.setNoWrap(false);
    contentArCell.setNoWrap(false);

    PdfPTable circularInfoTable = null;

    emptyCell.setColspan(2);
    circularInfoTable = new PdfPTable(2);
    circularInfoTable.addCell(cirNoEnCell);
    circularInfoTable.addCell(cirNoArCell);
    circularInfoTable.addCell(emptyCell);
    circularInfoTable.addCell(emptyCell);
    circularInfoTable.addCell(emptyCell);
    circularInfoTable.addCell(contentEnCell);
    circularInfoTable.addCell(contentArCell);
    circularInfoTable.addCell(emptyCell);

    circularInfoTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
    circularInfoTable.setWidthPercentage(100);
    document.add(circularInfoTable);

    document.close();

}
catch (Exception e)
{

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please take a look at the ParseHtml7 and ParseHtml8 examples. They take HTML input with Arabic characters and they create a PDF with the same Arabic text:

A PDF table with HTML content An HTML table in PDF

Before we look at the code, allow me to explain that it's not a good idea to use non-ASCII characters in source code. For instance: this is not done:

 htmlContentAr = “<p><span> ??? ??????? ??? ???????</p><p>??? ??????? ….</span></p>”;

You never know how a Java file containing these glyphs will be stored. If it's not stored as UTF-8, the characters may end up looking like something completely different. Versioning systems are known to have problems with non-ASCII characters and even compilers can get the encoding wrong. If you really want to stored hard-coded String values in your code, use the UNICODE notation. Part of your problem is an encoding problem, and you can read more about this here: Can't get Czech characters while generating a PDF

For the examples shown in the screen shots, I saved the following files using UTF-8 encoding:

This is what you'll find in the file arabic.html:

<html>
<body style="font-family: Noto Naskh Arabic">
<p>??? ??????? ??? ???????</p>
<p>??? ???????</p>
</body>
</html>

This is what you'll find in the file arabic2.html:

<html>
<body style="font-family: Noto Naskh Arabic">
<table>
<tr>
<td dir="rtl">??? ??????? ??? ???????</td>
<td dir="rtl">??? ???????</td>
</tr>
</table>
</body>
</html>

The second part of your problem concerns the font. It is important that you use a font that knows how to draw Arabic glyphs. It is hard to believe that you have arial.ttf right at the root of your C: drive. That's not a good idea. I would expect you to use C:/windows/fonts/arialuni.ttf which certainly knows Arabic glyphs.

Selecting the font isn't sufficient. Your HTML needs to know which font family to use. Because most of the examples in the documentation use Arial, I decided to use a NOTO font. I discovered these fonts by reading this question: iText pdf not displaying Chinese characters when using NOTO fonts or Source Hans. I really like these fonts because they are nice and (almost) every language is supported. For instance, I used NotoNaskhArabic-Regular.ttf which means that I need to define the font familie like this:

style="font-family: Noto Naskh Arabic"

I defined the style in the body tag of my XML, it's obvious that you can choose where to define it: in an external CSS file, in the styles section of the <head>, at the level of a <td> tag,... That choice is entirely yours, but you have to define somewhere which font to use.

Of course: when XML Worker encounters font-family: Noto Naskh Arabic, iText doesn't know where to find the corresponding NotoNaskhArabic-Regular.ttf unless we register that font. We can do this, by creating an instance of the FontProvider interface. I chose to use the XMLWorkerFontProvider, but you're free to write your own FontProvider implementation:

XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.register("resources/fonts/NotoNaskhArabic-Regular.ttf");

There is one more hurdle to take: Arabic is written from right to left. I see that you want to define the run direction at the level of the PdfPCell and that you add the HTML content to this cell using an ElementList. That's why I first wrote a similar example, named ParseHtml7:

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/NotoNaskhArabic-Regular.ttf");
    CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
    // HTML
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
    // Pipelines
    ElementList elements = new ElementList();
    ElementHandlerPipeline pdf = new ElementHandlerPipeline(elements, null);
    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"));

    PdfPTable table = new PdfPTable(1);
    PdfPCell cell = new PdfPCell();
    cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
    for (Element e : elements) {
        cell.addElement(e);
    }
    table.addCell(cell);
    document.add(table);
    // step 5
    document.close();
}

There is no table in the HTML, but we create our own PdfPTable, we add the content from the HTML to a PdfPCell with run direction LTR, and we add this cell to the table, and the table to the document.

Maybe that's your actual requirement, but why would you do this in such a convoluted way? If you need a table, why don't you create that table in HTML and define some cells are RTL like this:

<td dir="rtl">...</td>

That way, you don't have to create an ElementList, you can just parse the HTML to PDF as is done in the ParseHtml8 example:

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/NotoNaskhArabic-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();
}

There is less code needed in this example, and when you want to change the layout, it's sufficient to change the HTML. You don't need to change your Java code.

One more example: in ParseHtml9, I create a table with an English name in one column ("Lawrence of Arabia") and the Arabic translation in the other column ("?????? ?????"). Because I need different fonts for English and Arabic, I define the font at the <td> level:

<table>
<tr>
<td>Lawrence of Arabia</td>
<td dir="rtl" style="font-family: Noto Naskh Arabic">?????? ?????</td>
</tr>
</table>

For the first column, the default font is used and no special settings are needed to write from left to right. For the second column, I define an Arabic font and I set the run direction to "rtl".

The result looks like this:

English next to Arabic

That's much easier than what you're trying to do in your code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...