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

0

Hi!

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.

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];
    }

zbigniew.nitecki's picture
Joined: Mar 21 2019 - 2:21am
Last seen: 3 months 1 week ago

0 Answers:

No answers yet
Feedback