javaitext

iText 8.0.5 managing french accentuation in Java


I'm using iTextPdf 8.0.5 to generate a PDF. I have an issue with french characters like é, à, â, ç. For example the word 'défunt'. I either get 'dfunt' or 'd©funt' or 'défunt'

I tried using a standard font, using a local ttf font, using UTF8, using other encoding from this page : https://api.itextpdf.com/iText/java/latest/com/itextpdf/io/font/PdfEncodings.html

But nothing works.

Here is my code (I removed some content to just keep the cell with accentuation)

import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy; 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfVersion;
import com.itextpdf.kernel.pdf.WriterProperties;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.borders.SolidBorder; 


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class EtiquettesServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        String fontPath = "E:/Tomcat 9.0/webapps/Thanact/calibri.ttf"; 
        
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=etiquettes.pdf");
        
        try {

            PdfWriter writer = new PdfWriter(response.getOutputStream(), new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0));
//          writer.setPdfVersion(PdfVersion.PDF_2_0);

            PdfDocument pdfDoc = new PdfDocument(writer);

            Document document = new Document(pdfDoc);

            PdfFont font = PdfFontFactory.createFont(fontPath, "UTF-8");

            Table table = new Table(UnitValue.createPercentArray(4)).useAllAvailableWidth(); // 4 colonnes

            Text    label = new Text("Nom défunt : ").setBold();
            Text    value = new Text("MyValue");                    
            cell.add(new Paragraph().add(label).add(value).setFont(font).setFontSize(8));
            
            table.addCell(cell);
            
            document.add(table);
            document.close();

        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Erreur lors de la génération du PDF : " + e.getMessage());
            System.out.println(e.getMessage());
        }
    }
}

I found some info here : UTF-8 characters in a pdfcell itext

But it looks like it's from iText 7

Thanks for your help


Solution

  • Web

    EtiquettesServlet.java

    change

    PdfFont font = PdfFontFactory.createFont(fontPath, "UTF-8");
    

    to

    import com.itextpdf.io.font.PdfEncodings;
    ....
    PdfFont font = PdfFontFactory.createFont(fontPath, PdfEncodings.IDENTITY_H);
    

    use PdfEncodings.IDENTITY_H not UTF-8.

    package com.example;
    
    import com.itextpdf.io.font.constants.StandardFonts;
    import com.itextpdf.io.font.PdfEncodings;
    import com.itextpdf.kernel.font.PdfFont;
    import com.itextpdf.kernel.font.PdfFontFactory;
    import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy; 
    import com.itextpdf.kernel.pdf.PdfDocument;
    import com.itextpdf.kernel.pdf.PdfWriter;
    import com.itextpdf.kernel.pdf.PdfVersion;
    import com.itextpdf.kernel.pdf.WriterProperties;
    import com.itextpdf.kernel.colors.ColorConstants;
    import com.itextpdf.layout.Document;
    import com.itextpdf.layout.element.Paragraph;
    import com.itextpdf.layout.element.Text;
    import com.itextpdf.layout.element.Table;
    import com.itextpdf.layout.properties.UnitValue;
    import com.itextpdf.layout.element.Cell;
    import com.itextpdf.layout.properties.TextAlignment;
    import com.itextpdf.layout.borders.SolidBorder; 
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet("/EtiquettesServlet")
    public class EtiquettesServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        public EtiquettesServlet() {
            super();
        }
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //String fontPath = "E:/Tomcat 9.0/webapps/Thanact/calibri.ttf"; 
            String fontPath = "fonts/calibri.ttf";
            
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "attachment; filename=etiquettes.pdf");
            
            try {
    
                PdfWriter writer = new PdfWriter(response.getOutputStream(), new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0));
    //          writer.setPdfVersion(PdfVersion.PDF_2_0);
    
                PdfDocument pdfDoc = new PdfDocument(writer);
    
                Document document = new Document(pdfDoc);
    
                PdfFont fontUTF8 = PdfFontFactory.createFont(fontPath, "UTF-8");
                PdfFont fontIDENTITY_H = PdfFontFactory.createFont(fontPath, PdfEncodings.IDENTITY_H);
    
                Table table = new Table(UnitValue.createPercentArray(4)).useAllAvailableWidth(); // 4 colonnes
                
                String  frenchString = "Nom défunt (é, à, â, ç): ";
                //Text    label = new Text("Nom défunt : ").setBold();
                Text    label = new Text(frenchString);
                Text    labelBold = new Text(frenchString).setBold();
                //Text    value = new Text("MyValue");                    
                //Cell.add(new Paragraph().add(label).add(value).setFont(font).setFontSize(8));
                Cell cell = new Cell();            
                //cell.add(new Paragraph().add(label).add(value).setFont(fontUTF8).setFontSize(8));
                cell.add(new Paragraph().add(label).setFont(fontUTF8).setFontSize(24));
                table.addCell(cell);
    
                Cell cellBold = new Cell();            
                //cell.add(new Paragraph().add(label).add(value).setFont(fontUTF8).setFontSize(8));
                cellBold.add(new Paragraph().add(labelBold).setFont(fontUTF8).setFontSize(24));
                table.addCell(cellBold);
                
                Cell cell2 = new Cell();            
                //cell2.add(new Paragraph().add(label).add(value).setFont(fontUTF8).setFontSize(8));
                cell2.add(new Paragraph().add(label).setFont(fontIDENTITY_H).setFontSize(24));
                table.addCell(cell2);
    
                Cell cell2Bold = new Cell();            
                //cell2.add(new Paragraph().add(label).add(value).setFont(fontUTF8).setFontSize(8));
                cell2Bold.add(new Paragraph().add(labelBold).setFont(fontIDENTITY_H).setFontSize(24));
                table.addCell(cell2Bold);
                
                document.add(table);
                document.close();
    
            } catch (Exception e) {
                e.printStackTrace();
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Erreur lors de la génération du PDF : " + e.getMessage());
                System.out.println(e.getMessage());
            }
        }
    
    }
    
    apache-tomcat-9.0.96/webapps/Thanact
    └── WEB-INF
        ├── classes
        │   ├── com
        │   │   └── example
        │   │       └── EtiquettesServlet.class
        │   ├── fonts
        │   │   ├── calibrib.ttf
        │   │   ├── calibrii.ttf
        │   │   ├── calibrili.ttf
        │   │   ├── calibril.ttf
        │   │   ├── calibri.ttf
        │   │   └── calibriz.ttf
        │   └── logback.xml
        ├── lib
        │   ├── barcodes-8.0.5.jar
        │   ├── bc-fips-1.0.2.4.jar
        │   ├── bcpkix-fips-1.0.7.jar
        │   ├── bcpkix-jdk18on-1.78.jar
        │   ├── bcprov-jdk18on-1.78.jar
        │   ├── bcutil-jdk18on-1.78.jar
        │   ├── bouncy-castle-adapter-8.0.5.jar
        │   ├── bouncy-castle-connector-8.0.5.jar
        │   ├── bouncy-castle-fips-adapter-8.0.5.jar
        │   ├── commons-8.0.5.jar
        │   ├── font-asian-8.0.5.jar
        │   ├── forms-8.0.5.jar
        │   ├── hyph-8.0.5.jar
        │   ├── io-8.0.5.jar
        │   ├── kernel-8.0.5.jar
        │   ├── layout-8.0.5.jar
        │   ├── logback-classic-1.4.5.jar
        │   ├── logback-core-1.4.5.jar
        │   ├── pdfa-8.0.5.jar
        │   ├── pdfua-8.0.5.jar
        │   ├── sign-8.0.5.jar
        │   ├── slf4j-api-2.0.13.jar
        │   ├── styled-xml-parser-8.0.5.jar
        │   └── svg-8.0.5.jar
        └── web.xml
    

    Test URL: http://localhost:8080/Thanact/EtiquettesServlet

    PDF Result:

    002.png

    Windows Code Page

    open CMD.exe or PowerShell ,

    run command: chcp

    Language:

    002.png

    change

    PdfFont font = PdfFontFactory.createFont(fontPath, "UTF-8");
    

    to

    import com.itextpdf.io.font.PdfEncodings;
    ....
    PdfFont font = PdfFontFactory.createFont(fontPath, PdfEncodings.IDENTITY_H);
    

    003.png

    My Test Environment

    DocumentWithFrench.java

    package com.example;
    
    import com.itextpdf.io.font.PdfEncodings;
    import com.itextpdf.kernel.font.PdfFont;
    import com.itextpdf.kernel.font.PdfFontFactory;
    import com.itextpdf.kernel.pdf.PdfDocument;
    import com.itextpdf.kernel.pdf.PdfWriter;
    import com.itextpdf.layout.element.Paragraph;
    import com.itextpdf.layout.Document;
    
    
    import java.io.FileNotFoundException;
    
    public class DocumentWithFrench {
        public static void main(String[] args) throws FileNotFoundException {
            //String fontPath = "E:/Tomcat 9.0/webapps/Thanact/calibri.ttf";
            String fontPath = "fonts/calibri.ttf";
            String  frenchString = "Nom défunt (é, à, â, ç): ";
            PdfWriter writer = new PdfWriter("etiquettes.pdf");
            PdfDocument pdf = new PdfDocument(writer);
    
            try (Document document = new Document(pdf)) {
                PdfFont fontUTF8      = PdfFontFactory.createFont(fontPath, "UTF-8");
                PdfFont fontIDENTITY_H    = PdfFontFactory.createFont(fontPath, PdfEncodings.IDENTITY_H);
                //Text    label = new Text("Nom défunt : ").setBold();
                //Text    value = new Text("MyValue");
                Paragraph paragraph = new Paragraph()
                        .add(new Paragraph("Hello 1 >>>> "))
                        .add(new Paragraph(frenchString).setFont(fontUTF8).setFontSize(20) //FAIL
                        .add(new Paragraph("Hello 2 >>>> "))
                        .add(new Paragraph(frenchString).setFont(fontIDENTITY_H).setFontSize(20))
                        );
                document.add(paragraph);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }