Jump to content
We've recently updated our Privacy Statement, available here ×

ัMy .jrPrint file doens't show Thai text when I convert to .pdf file.


NightsoulCC4
Go to solution Solved by Teodor Danciu - Staff,

Recommended Posts

Java version: 8 (1.8)

Spring boot version: 2.4.0

jasper report dependency version: 5.1.2

 

I have a problem When I tried to convert .jrPrint file with Thai text to .pdf file here's my file when I opened on JasperSoft. My Thai text show the result is □□□ (Square), And the English text show it usually. Here is my code in Java Spring Boot Project
 

package com.nattatat.jrPrintToPdf.Service;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRPdfExporterParameter;
import net.sf.jasperreports.engine.util.JRLoader;

import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfAConformanceLevel;
import com.itextpdf.kernel.pdf.PdfOutputIntent;
import com.itextpdf.pdfa.PdfADocument;

import java.io.*;

@Service
public class JasperReportService{

    protected final static Logger log = LogManager.getLogger(JasperReportService.class);

    public void convertJrprintToPdf(String jrPrintFilePath, String pdfOutputPath) {
        log.info("\noutput: " + pdfOutputPath);
        try {
            JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObjectFromFile(jrPrintFilePath);

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

            // Set the export font map for PDF
            JRPdfExporter exporter = new JRPdfExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
            exporter.setParameter(JRPdfExporterParameter.CHARACTER_ENCODING, "UTF-8");
                        
            exporter.exportReport();

            JasperExportManager.exportReportToPdfFile(jasperPrint, pdfOutputPath);

            byte[] byteArray = outputStream.toByteArray();

            // Save the byte array to a file (optional)
            try (FileOutputStream fileOutputStream = new FileOutputStream(pdfOutputPath)) {
                fileOutputStream.write(byteArray);
                System.out.println("PDF file saved successfully.");
            } catch (IOException e) {
                e.printStackTrace();
            }

            System.out.println("PDF exported successfully.");
        } catch (JRException e) {
            e.printStackTrace();
            System.out.println("Error exporting to PDF.");
        }
    }

    public void convertJrprintToPdfa(String jrPrintFilePath, String pdfOutputPath) throws FileNotFoundException {
        try {
            // Load .jrprint file
            JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(new File(jrPrintFilePath));
            FileOutputStream fileOutputStream = new FileOutputStream(pdfOutputPath);

            // Create PdfAWriter
            PdfWriter pdfWriter = new PdfWriter(fileOutputStream);
            PdfADocument pdfADocument = new PdfADocument(pdfWriter, PdfAConformanceLevel.PDF_A_1B,
                    new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", null));

            // Create JasperReports exporter
            JRPdfExporter exporter = new JRPdfExporter();

            // Configure exporter
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fileOutputStream);
            exporter.setParameter(JRPdfExporterParameter.CHARACTER_ENCODING, "UTF-8");

            // Export the report to PDF/A
            exporter.exportReport();

            // Close the PdfADocument
            pdfADocument.close();

            System.out.println("PDF exported successfully.");
        } catch (JRException | IOException e) {
            e.printStackTrace();
        }
    }
}

And this is my fonts.xml for embedded font
 

<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>

    <!-- The jrprint file use this font families. -->
    <fontFamily name="TH Sarabun New">
        <normal><![CDATA[fonts/TH Sarabun New/THSarabunNew.ttf]]></normal>
        <bold><![CDATA[fonts/TH Sarabun New/THSarabunNewBold.ttf]]></bold>
        <italic><![CDATA[fonts/TH Sarabun New/THSarabunNewItalic.ttf]]></italic>
        <boldItalic><![CDATA[fonts/TH Sarabun New/THSarabunNewBoldItalic.ttf]]></boldItalic>
        <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
    </fontFamily>

    <fontFamily name="TH Sarabun PSK">
        <normal><![CDATA[fonts/TH Sarabun PSK/THSarabun.ttf]]></normal>
        <bold><![CDATA[fonts/TH Sarabun PSK/THSarabun Bold.ttf]]></bold>
        <italic><![CDATA[fonts/TH Sarabun PSK/THSarabun Italic.ttf]]></italic>
        <boldItalic><![CDATA[fonts/TH Sarabun PSK/THSarabun Bold Italic.ttf]]></boldItalic>
        <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
    </fontFamily>

    <fontFamily name="Angsana New">
        <normal><![CDATA[fonts/Angsana New/ANGSA.ttf]]></normal>
        <bold><![CDATA[fonts/Angsana New/angsab.ttf]]></bold>
        <italic><![CDATA[fonts/Angsana New/ANGSAI.ttf]]></italic>
        <boldItalic><![CDATA[fonts/Angsana New/AngsanaNewBoldItalic.ttf]]></boldItalic>
        <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
    </fontFamily>

</fontFamilies>

From these code I used. The .pdf result didn't show Thai text (Show only English text).

Link to comment
Share on other sites

Thank you for posting to the Jaspersoft Community. Our team of experts has read your question and we are working to get you an answer as quickly as we can. If you have a Jaspersoft Professional Subscription plan, please visit https://support.tibco.com/s/ for direct access to our technical support teams offering guaranteed response times.
 

Link to comment
Share on other sites

Dear Teodor danciu

I tried to add <pdfEncoding> tag but not sure it correct?, Or I need to edit somethings else. Here is it.
 

<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>

    <!-- The jrprint file use this font families. -->
    <fontFamily name="TH Sarabun New">
        <normal><![CDATA[fonts/TH Sarabun New/THSarabunNew.ttf]]></normal>
        <bold><![CDATA[fonts/TH Sarabun New/THSarabunNewBold.ttf]]></bold>
        <italic><![CDATA[fonts/TH Sarabun New/THSarabunNewItalic.ttf]]></italic>
        <boldItalic><![CDATA[fonts/TH Sarabun New/THSarabunNewBoldItalic.ttf]]></boldItalic>
        <pdfEmbedded><![CDATA[true]]></pdfEmbedded>
        <pdfEncoding>ISO-8859-1</pdfEncoding>
    </fontFamily>

</fontFamilies>

and my pdf file show me ????? text. The ? is a Thai text.

result.pdf

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...