Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasperreports not working on Linux server without X11: "Error initializing graphic environment."

I have a huge problem. I was coding an app which is using Jasperreports. Everything was perfectly fine when I was working locally. But then, when I have dockerized my app, when I run the method that should produce an PDF file, I get an error:

Request processing failed; nested exception is net.sf.jasperreports.engine.JRRuntimeException: Error initializing graphic environment.

I have found out that running my app with

-DJava.awt.headless=true

the problem would solve itself. But it is not.

This is how it looks like in the Dockerfile:

ENTRYPOINT [ "java", "-Djava.awt.headless=true", "-jar", "/var/generator/generator-0.0.1-SNAPSHOT.jar" ]

To be sure that I havent forgotten anything, here is the code of the method which is generating the PDF files, maybe there is something wrong there...

The error is coming from this line:

mainReport = JasperFillManager.fillReport(report, invoiceMap, new JREmptyDataSource());

And here is the method:

public byte[] generateInvoicePDF(Invoice invoice) {

    /* Map to fill in main JasperReport */
    Map<String, Object> invoiceMap = new HashMap<String, Object>();
    invoiceMap.put("FIRST_NAME", invoice.getFirstName());
    invoiceMap.put("LAST_NAME", invoice.getLastName());
    invoiceMap.put("REGON", invoice.getRegon());
    invoiceMap.put("NIP", invoice.getNip());
    invoiceMap.put("BUSINESS_NAME", invoice.getBusinessName());
    invoiceMap.put("BUSINESS_LOCATION", invoice.getBusinessLocation());
    invoiceMap.put("ORDER_DATE", invoice.getOrderDate());
    invoiceMap.put("RECEPTION_DATE", invoice.getReceptionDate());
    invoiceMap.put("CONTRACT_DATE", invoice.getContractDate());
    invoiceMap.put("CONTRACTOR_COMMENT", invoice.getContractorComment());
    invoiceMap.put("INVOICE_NUMBER", invoice.getInvoiceNumber());
    invoiceMap.put("INVOICE_ISSUE_DATE", invoice.getInvoiceIssueDate());

    JasperPrint mainReport;
    JasperPrint tableReport;
    try {
        InputStream invoiceJasper = InvoiceGeneratorServiceImpl.class.getResourceAsStream("/invoices/invoice.jrxml");
        InputStream taskTableJasper = InvoiceGeneratorServiceImpl.class.getResourceAsStream("/invoices/invoice_sub.jrxml");
        JasperDesign mainReportDesign = JRXmlLoader.load(invoiceJasper);
        JasperDesign tableReportDesign = JRXmlLoader.load(taskTableJasper);
        JasperReport report = JasperCompileManager.compileReport(mainReportDesign);
        JasperReport table = JasperCompileManager.compileReport(tableReportDesign);

        /* List to hold Items */
        List<InvoiceTask> taskList = new ArrayList<>();

        /* Add Items to List */
        taskList.addAll(invoice.getInvoiceTasks());

        /* Convert List to JRBeanCollectionDataSource */
        JRBeanCollectionDataSource itemsJRBean = new JRBeanCollectionDataSource(taskList);

        /* Map to hold Jasper report Parameters */
        Map<String, Object> taskListMap = new HashMap<String, Object>();
        taskListMap.put("TasksDataSource", itemsJRBean);
        taskListMap.put("ADDITIONAL_COSTS", invoice.getAdditionalCosts());

        mainReport = JasperFillManager.fillReport(report, invoiceMap, new JREmptyDataSource());
        tableReport = JasperFillManager.fillReport(table, taskListMap, new JREmptyDataSource());
        List<JasperPrint> printList = new ArrayList<>();
        printList.add(mainReport);
        printList.add(tableReport);

        JRPdfExporter exporter = new JRPdfExporter();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        exporter.setExporterInput(SimpleExporterInput.getInstance(printList)); //Set as export input my list with JasperPrint s
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(byteArrayOutputStream));
        SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
        configuration.setCreatingBatchModeBookmarks(true); //add this so your bookmarks work, you may set other parameters
        exporter.setConfiguration(configuration);
        exporter.exportReport();

        return byteArrayOutputStream.toByteArray();
    } catch (JRException e) {
        e.printStackTrace();
    }

    return new byte[0];
}

And a little bit more of the stack trace:

2019-04-24 08:43:57.338 ERROR 1 --- [io-8080-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is net.sf.jasperreports.engine.JRRuntimeException: Error initializing graphic environment.] with root cause

java.lang.NullPointerException: null
    at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264) ~[na:1.8.0_201]
    at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219) ~[na:1.8.0_201]
    at sun.awt.FontConfiguration.init(FontConfiguration.java:107) ~[na:1.8.0_201]
    at sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:774) ~[na:1.8.0_201]
    at sun.font.SunFontManager$2.run(SunFontManager.java:431) ~[na:1.8.0_201]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_201]
    at sun.font.SunFontManager.<init>(SunFontManager.java:376) ~[na:1.8.0_201]
    at sun.awt.FcFontManager.<init>(FcFontManager.java:35) ~[na:1.8.0_201]
    at sun.awt.X11FontManager.<init>(X11FontManager.java:57) ~[na:1.8.0_201]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_201]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_201]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_201]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_201]
    at java.lang.Class.newInstance(Class.java:442) ~[na:1.8.0_201]
    at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:83) ~[na:1.8.0_201]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_201]
    at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74) ~[na:1.8.0_201]
    at sun.java2d.SunGraphicsEnvironment.getFontManagerForSGE(SunGraphicsEnvironment.java:201) ~[na:1.8.0_201]
    at sun.java2d.SunGraphicsEnvironment.getAvailableFontFamilyNames(SunGraphicsEnvironment.java:235) ~[na:1.8.0_201]
    at sun.java2d.SunGraphicsEnvironment.getAvailableFontFamilyNames(SunGraphicsEnvironment.java:263) ~[na:1.8.0_201]
    at sun.java2d.HeadlessGraphicsEnvironment.getAvailableFontFamilyNames(HeadlessGraphicsEnvironment.java:94) ~[na:1.8.0_201]
    at net.sf.jasperreports.engine.util.JRGraphEnvInitializer.initializeGraphEnv(JRGraphEnvInitializer.java:58) ~[jasperreports-6.7.1.jar!/:6.7.1]
    at net.sf.jasperreports.engine.fill.BaseReportFiller.<init>(BaseReportFiller.java:136) ~[jasperreports-6.7.1.jar!/:6.7.1]
    at net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:273) ~[jasperreports-6.7.1.jar!/:6.7.1]
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:79) ~[jasperreports-6.7.1.jar!/:6.7.1]
    at net.sf.jasperreports.engine.fill.JRFiller.createBandReportFiller(JRFiller.java:251) ~[jasperreports-6.7.1.jar!/:6.7.1]
    at net.sf.jasperreports.engine.fill.JRFiller.createReportFiller(JRFiller.java:272) ~[jasperreports-6.7.1.jar!/:6.7.1]
    at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:156) ~[jasperreports-6.7.1.jar!/:6.7.1]
    at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:145) ~[jasperreports-6.7.1.jar!/:6.7.1]
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:758) ~[jasperreports-6.7.1.jar!/:6.7.1]
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:1074) ~[jasperreports-6.7.1.jar!/:6.7.1]
    at com.generator.service.impl.InvoiceGeneratorServiceImpl.generateInvoicePDF(InvoiceGeneratorServiceImpl.java:68) ~[classes!/:na]
    at com.generator.rest.ContractorController.getPdf(ContractorController.java:165) ~[classes!/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
like image 957
dziki Avatar asked Apr 24 '19 09:04

dziki


2 Answers

This looks like an issue with a missing font.

If you happen to be using Alpine then you might be seeing issue #73, which has the same relevant stack trace. According to that issue, installing the ttf-dejavu package should fix your problem.

Add this to your dockerfile:

RUN apk add ttf-dejavu
like image 185
Marvin Avatar answered Nov 15 '22 16:11

Marvin


I was able to fix this by installing fontconfig and urw-fonts packages.

yum install fontconfig
yum install urw-fonts

see github issue

like image 21
Ruchira Nawarathna Avatar answered Nov 15 '22 14:11

Ruchira Nawarathna