'Generating a pdf report using JasperRunManager
I want to generate a pdf report using ejbql ,but I dont know how to do that. I have tried to do thye following but an error occure:
parameterMap.put(JRJpaQueryExecuterFactory.PARAMETER_JPA_ENTITY_MANAGER, entityManager);
parameterMap.put("priority", "3");
try
{
JasperFillManager.fillReportToFile("C:/Documents and Settings/MyDocuments/NetBeansProjects/JiraMap/src/java/Reports/Test.jasper", parameterMap);
JasperRunManager.runReportToPdfFile("C:/Documents and Settings/MyDocuments/NetBeansProjects/JiraMap/src/java/Reports/Test.pdf", parameterMap);
}
catch (JRException ex)
{
ex.printStackTrace();
}
Tehb error is:
net.sf.jasperreports.engine.JRException: Error loading object from file : C:\Documents and Settings\My Documents\NetBeansProjects\JiraMap\src\java\Reports\Test.pdf
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:101)
at net.sf.jasperreports.engine.JasperRunManager.runReportToPdfFile(JasperRunManager.java:93)
at Reporting.ReportBean.main(ReportBean.java:38)
Caused by: java.io.EOFException
UPdate
I have use this code (I haven't use any parameter ) but always the report is empty:
JasperReport report = JasperCompileManager.compileReport("C:/Documents and Settings/My Documents/NetBeansProjects/JiraMap/src/java/Reports/Test.jrxml");
JasperPrint print = JasperFillManager.fillReport(report,null);
JasperExportManager.exportReportToPdfFile(print,"C:/Documents and Settings/My Documents/NetBeansProjects/JiraMap/src/java/Reports/Test.pdf");
My report with the Aggregate Function count(I have tried to use the alias as numebr but it didn't work):
<?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="Test" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" 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="0"/>
<parameter name="priority" class="java.lang.String">
<defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
<queryString language="ejbql">
<![CDATA[select count(j) from Jiraissue j where j.priority='3']]>
</queryString>
<field name="count(j)" class="java.lang.Long"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="84" splitType="Stretch">
<staticText>
<reportElement x="28" y="0" width="100" height="20"/>
<textElement/>
<text><![CDATA[count(j)]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="50" y="11" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.Long"> <![CDATA[$F{count(j)}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
The error is :
net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : count(j)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDat aSource.java:123)
at net.sf.jasperreports.engine.data.JRJpaDataSource$PropertyReader.getValue(JRJpaDataSource.java:206)
at net.sf.jasperreports.engine.data.JRJpaDataSource.getFieldValue(JRJpaDataSource.java:131)
at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:821)
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:785)
at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1482)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:126)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:946)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:118)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:435)
at Reporting.ReportBean.main(ReportBean.java:41)
Caused by: java.lang.NoSuchMethodException: Unknown property 'count'+ on bean class 'class java.lang.Long'
[EL Info]: 2011-06-29 11:05:12.606--ServerSession(27055962)--file:/C:/Documents and Settings/a497165/My Documents/NetBeansProjects/JiraMap/build/web/WEB-INF/classes/_JiraMapPU logout successful
at org.apache.commons.beanutils.PropertyUtilsBean.getMappedProperty(PropertyUtilsBean.java:624)
at org.apache.commons.beanutils.PropertyUtilsBean.getMappedProperty(PropertyUtilsBean.java:570)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:758)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:837)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)
... 10 more
Solution 1:[1]
This is part of the code I used for generating with JasperPrint; and sending the file back to a browser response. This using JDBC though, not EJBQL. But the JasperReports part of the code is the same.
FileResolver fileResolver = new FileResolver() {
@Override
public File resolveFile(String fileName) {
return new File(getServletContext().getRealPath("/") + fileName);
}
};
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("REPORT_FILE_RESOLVER", fileResolver);
...... // blah blah
JasperPrint jrprint = JasperFillManager.fillReport(input, parameters, conn);
byte[] reportBytes;
jrprint.setName("MyFile." + fileFormat);
response.setContentType("APPLICATION/OCTET-STREAM");
String disHeader = "Attachment;Filename=\"MyFile.";
ServletOutputStream os = response.getOutputStream();
response.setHeader("Content-Disposition", disHeader);
reportBytes = JasperExportManager.exportReportToPdf(jrprint);
os.write(reportBytes);
Solution 2:[2]
You need to pass all the parameters needed as below:
JasperPrint jrprint = JasperFillManager.fillReport(input, parameters, connections);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | user3249098 |