java generate pdf file from string with arabic character and html Content

馋奶兔 提交于 2019-12-25 03:29:15

问题


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 ) :

and the result ( pdf file ) is :

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

来源:https://stackoverflow.com/questions/51200970/java-generate-pdf-file-from-string-with-arabic-character-and-html-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!