开发者

Jasper Reports empty PDF report problem, using Spring ModelAndView

I am trying to generate PDF report with Jasper Reports using Spring. But report is always empty. I searched a lot and could not find problem. I tried to write byte[] pdfReport to OutputStream but result is same, report is always empty.

I have 2 parameters one o them is testName and one of them is chart. Both can not display at pdf report.

Thanks a lot for your help,

Here is the code that generates report.

//My class extends MultiActionController

DefaultPieDataset dataset = new DefaultPieDataset();

dataset.setValue(String.format("%s, %s", "pie1", "pie1"),20);
dataset.setValue(String.format("%s, %s", "pie2", "pie2"),80);

JFreeChart chart = ChartFactory.createPieChart("testPie", dataset, true, true, false);

Map model = new HashMap(); 
model.put("chart", chart.createBufferedImage(200, 200)); 
model.put("testName", "test report");
model.put("format", "pdf");

AbstractJasperReportsView  view = new JasperReportsMultiFormatView();
view.setUrl("/WEB-INF/classes/reports/"+"test1"+".jasper开发者_开发知识库");
view.setApplicationContext(getApplicationContext());
view.setContentType("application/pdf");

Properties header = new Properties();
view.setHeaders(header);

ModelAndView mv = new ModelAndView(view, model);

JasperReport report =  (JasperReport) JRLoader.loadObject(getServletContext().getResourceAsStream("/WEB-INF/classes/reports/"+"test1"+".jasper"));

JasperPrint prt = JasperFillManager.fillReport(report, model);
byte[] pdfReport = JasperExportManager.exportReportToPdf(prt);

return mv;

And here is jrxml file

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="test1" language="groovy" pageWidth="700" pageHeight="842" columnWidth="660" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="2"/>
    <parameter name="testName" class="java.lang.String"/>
    <parameter name="chart" class="java.awt.Image" isForPrompting="false"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="48" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="14" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="229" splitType="Stretch">
            <image>
                <reportElement x="162" y="13" width="200" height="200"/>
                <imageExpression class="java.awt.Image"><![CDATA[$P{chart}]]></imageExpression>
            </image>
            <textField>
                <reportElement x="37" y="61" width="100" height="20"/>
                <textElement/>
                <textFieldExpression class="java.lang.String"><![CDATA[$P{testName}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="112" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="76" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>


Why are you doing the following?

ModelAndView mv = new ModelAndView(view, model);

JasperReport report =  (JasperReport) JRLoader.loadObject(getServletContext().getResourceAsStream("/WEB-INF/classes/reports/"+"test1"+".jasper"));

JasperPrint prt = JasperFillManager.fillReport(report, model);
byte[] pdfReport = JasperExportManager.exportReportToPdf(prt);

return mv;

Using JasperReportsMultiFormatView() already combines the filling and exporting of the report. No need for the following:

JasperReport report =  (JasperReport) JRLoader.loadObject(getServletContext().getResourceAsStream("/WEB-INF/classes/reports/"+"test1"+".jasper"));

JasperPrint prt = JasperFillManager.fillReport(report, model);
byte[] pdfReport = JasperExportManager.exportReportToPdf(prt);

You must declare the JRXML file and report data key in a resource bundle.

I suggest you check this guide I wrote for Spring 3 MVC - Jasper Integration Tutorial: http://krams915.blogspot.com/2010/12/spring-3-mvc-jasper-integration.html

Just modify the views to use the JasperReportsMultiFormatView() instead.

Check the Tutorials section for more. If I have time later, I'm gonna make a tutorial using JasperReportsMultiFormatView(). If not maybe tomorrow. Let me know if this helps you.


There is a property in jasper report called 'whenNoDataType'. Did you tried setting that to whenNoDataType="AllSectionsNoDetail"

Also the report language is given as 'groovy'. Why?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜