0

I wrote the following code to read the HTML template file, replace few values and converting to PDF. But Arabic values are not showing in the result.

Jar Used are itextpdf-5.5.13 & xmlworker-5.5.13

Java Code

    public static void convertPDF(String profileHTMLPage, String resultPDFFile) {
    try {

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(resultPDFFile));
        document.open();
        String arabic_name =  "حداد";
        //Reading html page
        byte[] fileContent = getFileContent(profileHTMLPage);            
        //Converting it to String
        String strData = new String(fileContent);
        String value = new String(arabic_name.getBytes("UTF-8"));            
        //Replacing the values
        strData = strData.replaceAll("nameEn", "Smith").replaceAll("nameAr", value);            
        Reader strReader = new StringReader(strData);

        XMLWorkerHelper.getInstance().parseXHtml(writer, document, strReader);
        document.close();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(PDFUtilProject.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException | IOException ex) {
        Logger.getLogger(PDFUtilProject.class.getName()).log(Level.SEVERE, null, ex);
    }
}    public static byte[] getFileContent(String filePath) {

    byte[] fileData = null;
    try {
        fileData = Files.readAllBytes(Paths.get(filePath));
    } catch (IOException ex) {
        Logger.getLogger(PDFUtilProject.class.getName()).log(Level.SEVERE, null, ex);
    }
    return fileData;
}

Attached HTML Template & PDF Result.

enter image description here enter image description here

Justin
  • 21
  • 2
  • its not duplicated, the other examples are about to create the PDF file components by using ColumnText, Chunk and Phragraph. My issue is specific to replace the predefined string value with Arabic Value. – Justin Dec 13 '18 at 06:59