1

I want to generate pdf file from string with arabic character and html Content .

I used itextpdf-5.5.6.jar and xmlworker-5.5.6.jar.

I try with this code :

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

public class Test1 {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub

        createPdf();

    }


    public static void createPdf() throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("f:\\traditional-arabic\\Test.pdf"));
        // step 3
        document.open();

        // step 4

                String text =   "<p align='center' dir='RTL' style='text-align:center;'>&nbsp;<strong><em><u><span style='background:white;'><span style='color:red;'><span style='font-size:26.0pt;'>تجربة نص</span></span></span></u></em></strong></p>" +

"<p dir='RTL'>&nbsp;</p>" +
                "<table border='1' cellpadding='0' cellspacing='0' style='margin-left:12.75pt;border-collapse:collapse;border:none;'>" +
                    "<thead>" +
                        "<tr style='height:44px;'>" +
                        "   <th style='width:337px;border:solid black 1.0pt;background:#E6E6E6;padding:0in 5.4pt 0in 5.4pt;height:44px;'>" +
                        "   <p align='center' dir='RTL' style='text-align:center;line-height:115%;text-autospace:none;'><strong><span style='font-size:13.0pt;'>الوصف</span></strong></p>" +
                        "   </th>" +
                        "   <th style='width:254px;border:solid black 1.0pt;border-left:none;background:#E6E6E6;padding:0in 5.4pt 0in 5.4pt;height:44px;vertical-align:bottom;'>" +
                        "   <p align='center' dir='RTL' style='text-align:center;line-height:115%;text-autospace:none;'>&nbsp;</p>" +

                        "   <p align='center' dir='RTL' style='text-align:center;line-height:115%;text-autospace:none;'><strong><span style='font-family:Arial,sans-serif;'><span style='font-size:13.0pt;'>العنوان</span></span></strong></p>" +
                        "   </th>" +
                        "</tr>" +
                    "</thead>" +
                    "<tbody>" +
                    "   <tr style='height:32px;'>" +
                    "       <td style='width:337px;border:solid black 1.0pt;border-top:none;padding:0in 5.4pt 0in 5.4pt;height:32px;vertical-align:top;'>" +
                    "       <p dir='RTL' style='margin-top:6.0pt;margin-right:.5in;margin-bottom:0in;margin-left:0in;margin-bottom:.0001pt;line-height:115%;text-autospace:none;'><span style='font-family:Simplified Arabic,serif;'>____/ ____/ _____هـ </span><span dir='LTR'>____/____/______&nbsp; -&nbsp; </span></p>" +
                    "       </td>" +
                    "       <td style='width:254px;border-top:none;border-left:none;border-bottom:solid black 1.0pt;border-right:solid black 1.0pt;padding:0in 5.4pt 0in 5.4pt;height:32px;'>" +
                    "       <p dir='RTL' style='text-align:center;direction:rtl;unicode-bidi:embed;'><span style='font-family:Simplified Arabic,serif;'><span style='font-size:14.0pt;'>التاريخ</span></span></p>" +
                    "       </td>" +
                    "   </tr>" +
                    "</tbody>" +
                "</table>" +

                "<p dir='RTL'>&nbsp;</p>" +

                "<p dir='RTL'>&nbsp;</p>" +

                "<p dir='RTL'>&nbsp;</p>" +

                "<p dir='RTL'>&nbsp;</p>" ;





        XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
        InputStream is = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
        worker.parseXHtml(writer, document, is, Charset.forName("UTF-8"));
        // step 5
        document.close();
    }
}

the text content is ( String text ) :

enter image description here

and the result ( pdf file ) is : enter image description here

I think the problem is related to fonts like arial.ttf

but I don't know where I have to add them in my code

franco
  • 1,829
  • 6
  • 42
  • 75
  • 1
    Please use iText 7 + pdfHTML + pdfCalligraph instead of iText 5.5.6. See https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-7-frequently-asked-questions-about-pdfhtml/how-convert-html-containing-arabichebrew-characters-pdf for an example. Support for Arabic is too limited in iText 5 (too many workarounds are needed when compared to iText 7: some writing system-related problems can't be solved in iText 5). – Bruno Lowagie Jul 06 '18 at 04:36
  • iText 5 is being phased out and it's no longer supported (except for customers who purchased premium support). However, should there be volunteers who want to answer this question, it might be interesting to know that this question is closely related to a question posted two hours before this one: [convert html file with Arabic Characters to pdf using java](https://stackoverflow.com/questions/51199839). I closed that question as a duplicate. I'm keeping this question open, but it's also off-topic since the problem can no longer be reproduced with the newest iText version. – Bruno Lowagie Jul 06 '18 at 06:05
  • 2
    thank you for your response, I solve my problem using itext 7 – franco Jul 08 '18 at 09:28

0 Answers0