java从带有阿拉伯字符和html内容的字符串生成pdf文件

时间:2018-07-05 23:04:34

标签: java itext xmlworker

我想从带有阿拉伯字符和html内容的字符串生成pdf文件。

我使用了 itextpdf-5.5.6.jar xmlworker-5.5.6.jar

我尝试使用以下代码:

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

文本内容为(字符串文本):

enter image description here

,结果(pdf文件)为: enter image description here

我认为问题与arial.ttf之类的字体有关

但是我不知道我必须在代码中添加它们

0 个答案:

没有答案