Python app to run JasperReport libraries - i.e. no JasperServer

前端 未结 2 595
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-24 08:30

I\'m looking to try and run Jasper reports (that have been written in iReports and exported to xml) from within a python application, without having to communicate with a Jasper

2条回答
  •  时光取名叫无心
    2021-01-24 08:55

    I used py4j. I had to write a small program in java. Using this as an example, it was simple.

    It was more difficult to configure the build environment and put all the dependencies for printing qr-codes.

    Python example:

        from py4j.java_gateway import JavaGateway
        gateway = JavaGateway()
        gateway.entry_point.pdf_from_json('e:/test.jasper', 'e:/test.json', 'e:/test.pdf')
    

    Java example:

    package jasper4py;
    
    import py4j.GatewayServer;
    
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    
    import net.sf.jasperreports.engine.JRException;
    import net.sf.jasperreports.engine.JasperCompileManager;
    import net.sf.jasperreports.engine.JasperExportManager;
    import net.sf.jasperreports.engine.JasperFillManager;
    import net.sf.jasperreports.engine.JasperPrint;
    import net.sf.jasperreports.engine.data.JsonDataSource;
    import net.sf.jasperreports.engine.util.JRLoader;
    
    public class JasperEntryPoint {
    
        public static void main(String[] args) {
            GatewayServer gatewayServer = new GatewayServer(new JasperEntryPoint());
            gatewayServer.start();
            System.out.println("Gateway Server Started");
        }
    
        public void pdf_from_json(String report, String data, String result) throws JRException, IOException {
            Map parameters = new HashMap();
            JsonDataSource dataSource = new JsonDataSource(JRLoader.getLocationInputStream(data));
            JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, dataSource);
            JasperExportManager.exportReportToPdfFile(jasperPrint, result);
        }
    }
    

提交回复
热议问题