jasper报告与spring empty pdf

时间:2017-07-28 11:54:09

标签: java mysql spring spring-mvc jasper-reports

我现在已经工作了一个多星期了,我已经使用了两个例子,但没有人为我工作。你能帮我在代码中找到错误吗?

Connection conn = null;
    try {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("Please include Classpath Where your MySQL Driver is located");
            e.printStackTrace();
        }
        conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jwayma_tax2","student","student");
        if (conn != null)
        {
            System.out.println("Database Connected");
        } else
        {
            System.out.println(" connection Failed ");
        }
        //Parameters as Map to be passed to Jasper
        HashMap<String,Object> hmParams=new HashMap<String,Object>();
        hmParams.put("dec_id", new Integer(id));
        InputStream paiementReportStream= getClass().getResourceAsStream("Jasper/Paiement_db.jrxml");
        JasperReport jasperReport= JasperCompileManager.compileReport(paiementReportStream);
        JRSaver.saveObject(jasperReport, "Paiement_db.jasper");
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, hmParams, conn);
        JRPdfExporter exporter = new JRPdfExporter();

        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        SimpleOutputStreamExporterOutput out =new SimpleOutputStreamExporterOutput("Paiement_db.pdf");
        exporter.setExporterOutput(out);

        SimplePdfReportConfiguration reportConfig= new SimplePdfReportConfiguration();
        reportConfig.setSizePageToContent(true);
        reportConfig.setForceLineBreakPolicy(false);
        SimplePdfExporterConfiguration exportConfig = new SimplePdfExporterConfiguration();
        exportConfig.setMetadataAuthor("baeldung");
        exportConfig.setEncrypted(true);
        exportConfig.setAllowedPermissionsHint("PRINTING");
        exporter.setConfiguration(reportConfig);
        exporter.setConfiguration(exportConfig);
        exporter.exportReport();

    } catch (Exception sqlExp) {
        System.out.printf("Exception::");
        sqlExp.printStackTrace();
    } finally {
        try {
            if (conn != null) {
                conn.close();
                conn = null;
            }
        } catch (SQLException expSQL) {
            System.out.println("SQLExp::CLOSING::" + expSQL.toString());
        }

    }

我从这个例子中得到了代码 http://www.baeldung.com/spring-jasper 我还尝试了以下示例代码http://javaonlineguide.net/2015/05/spring-4-jasper-report-integration-example-with-mysql-database-in-eclipse.html

String reportFileName = "Paiement_db";

    Connection conn = null;
    try {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("Please include Classpath Where your MySQL Driver is located");
            e.printStackTrace();
        }
        conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jwayma_tax2","student","student");
        if (conn != null)
        {
            System.out.println("Database Connected");
        } else
        {
            System.out.println(" connection Failed ");
        }
        //Parameters as Map to be passed to Jasper
        HashMap<String,Object> hmParams=new HashMap<String,Object>();
        hmParams.put("dec_id", new Integer(id));
        JasperReport jasperReport = getCompiledFile(reportFileName, request);
        generateReportPDF(response, hmParams, jasperReport, conn);// For PDF report

    } catch (Exception sqlExp) {
        System.out.printf("Exception::");
        sqlExp.printStackTrace();
    } finally {
        try {
            if (conn != null) {
                conn.close();
                conn = null;
            }
        } catch (SQLException expSQL) {
            System.out.println("SQLExp::CLOSING::" + expSQL.toString());
        }

    }

这是template.jrxml

<?xml version="1.0" encoding="UTF-8"?>

http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"命名=&#34; Blank_A4_1&#34;页宽=&#34; 595&#34; pageHeight =&#34; 842&#34; columnWidth时=&#34; 555&#34; LEFTMARGIN =&#34; 20&#34; rightMargin =&#34; 20&#34; TOPMARGIN =&#34; 20&#34; bottomMargin =&#34; 20&#34; UUID =#&34; 31c3a6bf-74A-D-4135-99dc-c0a601aa63e1&#34;&GT;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

1 个答案:

答案 0 :(得分:0)

过时了:我似乎你把文件作为String提供,而jasper期望一个URL。创建一个File对象并从中创建一个URL。

InputStream paiementReportStream= getClass().getResourceAsStream("Jasper/Paiement_db.jrxml");

找不到资源?检查paiementReportStream是否为空。