【问题标题】:jasper report with spring empty pdf碧玉报告与春天空pdf
【发布时间】:2017-07-28 11:54:09
【问题描述】:

我已经为此工作了一个多星期,我已经使用了 2 个示例,但没有人为我工作。您能帮我找出代码中的错误吗?

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" name="Blank_A4_1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20 " bottomMargin="20" uuid="31c3a6bf-74ad-4135-99dc-c0a601aa63e1">

【问题讨论】:

  • 你有关于 stacktrace 的实际错误消息吗?
  • 在第二个例子中,我首先得到一个空白的 pdf 页面,我在这一行 JasperReport jasperReport= JasperCompileManager.compileReport( paiementReportStream);

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


【解决方案1】:

过时:我似乎您将文件作为字符串提供,而 jasper 需要一个 URL。创建一个 File 对象并从中创建一个 URL。

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

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

【讨论】:

  • 是的,它是空的,我不知道为什么,但是文件路径是正确的
  • 我猜不是,因为您的变量为空。 getResourceXXX 总是很难让代码按预期工作。
  • 找不到方法getResourceXXX
  • 如果我使用的有任何错误,你可以检查第二个代码吗?方法你可以在第二个示例中找到主题
  • 毕竟我必须使用 jaspesoft studio 编译 .jasper 文件,右键单击该文件并单击编译报告