How to reduce time to generate PDF from large XML?

Hi,

I m generating PDF file from large XML file using jasper.

Using below code PDF file is generated but taking much time.

So Please suggest some way to reduce this time.

Sample Code

private void generatePDFFromXML(String strJasperFileName, String strXMLContent, String recordPath) {
        // Converting xml content into inputstream so, it can use for pdf generation directly without converting it into file 
        try (InputStream inputStrem = new ByteArrayInputStream(strXMLContent.getBytes("UTF-8"));) {
            
            Locale locale = new Locale("sq");

            Map params = new HashMap<>();
            params.put(JRParameter.REPORT_LOCALE, locale);

            params.put(JRXPathQueryExecuterFactory.XML_INPUT_STREAM, inputStrem);
            
            JRFileVirtualizer virtualizer = new JRFileVirtualizer(2, "tmp");
            params.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);
            
            JRXmlDataSource xmlDataSource = new JRXmlDataSource(inputStrem, recordPath);
            DefaultJasperReportsContext context = DefaultJasperReportsContext.getInstance();
            JRPropertiesUtil.getInstance(context).setProperty(JRXPathExecuterUtils.PROPERTY_XPATH_EXECUTER_FACTORY, "net.sf.jasperreports.engine.util.xml.JaxenXPathExecuterFactory");

            context.setProperty(MaxPagesGovernor.PROPERTY_MAX_PAGES_ENABLED, Boolean.TRUE.toString());
            context.setProperty(MaxPagesGovernor.PROPERTY_MAX_PAGES, String.valueOf(50000));
            context.setProperty(TimeoutGovernor.PROPERTY_TIMEOUT_ENABLED, Boolean.FALSE.toString());

            JasperPrint jasperPrint = JasperFillManager.fillReport(strJasperFileName, params, xmlDataSource);
            
            try(FileOutputStream oStream = new FileOutputStream("/Downloads/" + "GeneratedPDF" + ".pdf")){
                JRPdfExporter exporter = new JRPdfExporter();
                exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
                exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(oStream));
                
                SimplePdfReportConfiguration simplePdfReportConfiguration = new SimplePdfReportConfiguration();
                simplePdfReportConfiguration.setForceSvgShapes(false);
                simplePdfReportConfiguration.setForceLineBreakPolicy(false);
                exporter.setConfiguration(simplePdfReportConfiguration);

                exporter.exportReport();
                byte[] byteOutPut = JasperExportManager.exportReportToPdf(jasperPrint);
                
                oStream.flush();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
 }

 

Thanks in advance.

jay.vaishnav's picture
Joined: Nov 9 2022 - 9:16pm
Last seen: 4 months 2 days ago

0 Answers:

No answers yet
Feedback
randomness