Search code examples
dockerjasper-reportsexport-to-pdf

Jasper Reports bold style does not work with PDF Exporter


I create a report with JasperSoft Studio using default font (Sans Serif), my project has the following dependencies in the build.gradle file:

implementation "com.lowagie:itext:2.1.7.js8"
implementation "net.sf.jasperreports:jasperreports:6.17.0"

The jrxml template is compiled with Jasper Reports 6.17.0 and it contains static text and textfields (markup styled) with bold style. When I run the project in my computer (macOS Big Sur) the bold style is not appearing in pdf output, so I add the following dependency:

implementation "net.sf.jasperreports:jasperreports-fonts:6.17.0"

It works in macOS, but when I deploy my project in Docker, the bold style is not appearing. My Dockerfile is as follow:

FROM openjdk:11.0.11-jre-buster

COPY customer-microservice*.jar /usr/app/service.jar
ENTRYPOINT ["java", "-jar", "-D=java.awt.headless=true", "/usr/app/service.jar"]

The java code to export the PDF:

private JasperReport report(String jasperFilePath) throws Exception {
    var inputStream = resourceResolver
                       .getResourceAsStream(jasperFilePath)
                       .orElseThrow(() -> new FileNotFoundException(jasperFilePath));
    return (JasperReport) JRLoader.loadObject(inputStream);
}

public void exportPdf(String reportName, String customerData) throws Exception {
    var jsonDataStream = new ByteArrayInputStream(customerData.getBytes());
    JsonDataSource ds = new JsonDataSource(jsonDataStream);
    JRFileVirtualizer fileVisualizer = new JRFileVirtualizer(100, "test");

    Map<String, Object> m = new HashMap<>();
    m.put(JRParameter.REPORT_VIRTUALIZER, fileVisualizer);

    String jasperFilePath = "classpath:jasper/customer-report.jasper";
    JasperPrint p = JasperFillManager.fillReport(report(jasperFilePath), m, ds);

    // I use a docker volume to open the file in my computer
    String destFinal = "/tmp/" + reportName;
    JasperExportManager.exportReportToPdfFile(p, destFinal);
}

Jasper Reports use Helvetica font to export PDF file, so I execute fc-match inside docker container:

# fc-match -s Helvetica
DejaVuSans.ttf: "DejaVu Sans" "Book"
DejaVuSans-Bold.ttf: "DejaVu Sans" "Bold"
DejaVuSansMono.ttf: "DejaVu Sans Mono" "Book"
DejaVuSerif.ttf: "DejaVu Serif" "Book"
DejaVuSansMono-Bold.ttf: "DejaVu Sans Mono" "Bold"
DejaVuSerif-Bold.ttf: "DejaVu Serif" "Bold"

The bold style problem is only when my project runs in Docker, any idea why it happens?


Solution

  • The problem is because I'm using shadow gradle plugin to generate a shadow jar, the jasperreports and jasperreports-fonts libraries has jasperreports_extension.properties file in the same path.

    I add the following configuration in shadowJar task:

    dependencies {
       implementation "com.lowagie:itext:2.1.7.js8"
       implementation "net.sf.jasperreports:jasperreports:6.17.0"
       implementation "net.sf.jasperreports:jasperreports-fonts:6.17.0"
    }
    
    shadowJar {
        // Add content from jasperreports and jasperreports-fonts libraries
        append 'jasperreports_extension.properties'
    }
    

    This question was useful to find the solution: Exception in net.sf.jasperreports.engine.JRRuntimeException UncaughtExceptionHandler