Reputation: 649
Have: - a report generator class (Test.class). This class populates an JRBeanArrayDataSource with dummy non-empty values.
All i get is a blank pdf.
Thanks
The classes:
public class Test {
public static void main(String[] args) {
Test t = new Test();
try {
t.dostuff();
} catch (JRException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void dostuff() throws JRException, IOException {
Map<String,Object> params = new HashMap<String, Object>();
File f = new File("C:\\report1.jrxml");
InputStream fis = new FileInputStream(f);
JasperReport jrsub = JasperCompileManager.compileReport(fis);
List<OficioSipoData> l = new ArrayList<OficioSipoData>();
for (int i = 0; i < 5; i++) {
addDummyOSD(l,i);
}
Object[] os = l.toArray();
JRDataSource ds = new JRBeanArrayDataSource(os);
JasperRunManager.runReportToPdf(jrsub, params, ds);
byte[] bytes = JasperRunManager.runReportToPdf(jrsub, params, ds);
FileOutputStream fos = new FileOutputStream(new File("C:\\report1.pdf"));
fos.write(bytes);
fos.close();
System.out.println("fine");
}
private void addDummyOSD(List<OficioSipoData> lista, int i) {
OficioSipoData osd = new OficioSipoData();
osd.setDireccion("asasa " + i);
osd.setFecha("11/11/2013");
osd.setOperativo("dsdsds" + i);
lista.add(osd);
}
}
public class OficioSipoData {
private String fecha;
private String direccion;
private String operativo;
public String getDireccion() {
return direccion;
}
public void setDireccion(String direccion) {
this.direccion = direccion;
}
public String getFecha() {
return fecha;
}
public void setFecha(String fecha) {
this.fecha = fecha;
}
public String getOperativo() {
return operativo;
}
public void setOperativo(String operativo) {
this.operativo = operativo;
}
}
The JRXML
<?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="report1" language="groovy" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c79c5925-d8ca-40e8-bad2-1eaadb05c38f">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<field name="direccion" class="java.lang.String"/>
<field name="fecha" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="113" splitType="Stretch">
<staticText>
<reportElement uuid="e412065c-5a73-462f-9390-0096e54f80b3" x="226" y="23" width="100" height="20"/>
<textElement/>
<text><![CDATA[wiii]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="58" splitType="Stretch">
<staticText>
<reportElement uuid="ffc0130b-fee2-4861-b33a-a1078456813f" x="216" y="13" width="100" height="20"/>
<textElement/>
<text><![CDATA[waaa]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band splitType="Stretch"/>
</columnHeader>
<detail>
<band height="132" splitType="Stretch">
<staticText>
<reportElement uuid="e34f4c70-8e1c-405b-b3f7-90e11e5e9b7e" x="0" y="4" width="100" height="20"/>
<textElement/>
<text><![CDATA[direccion]]></text>
</staticText>
<textField>
<reportElement uuid="ab77eb85-4dfa-44da-aa5e-3ad98b7b426e" x="100" y="4" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{direccion}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band splitType="Stretch"/>
</pageFooter>
<summary>
<band splitType="Stretch"/>
</summary>
</jasperReport>
Upvotes: 0
Views: 3254
Reputation: 22867
You have called the method JasperRunManager.runReportToPdf(JasperReport , java.util.Map<java.lang.String,java.lang.Object>, JRDataSource)
twice.
And this is wroing because during first call the engine has already iterated the collection. You can check the result of JRDataSource.next()
method before second call of JasperRunManager.runReportToPdf
method.
Your right code will be:
private void dostuff() throws JRException, IOException {
Map<String, Object> params = new HashMap<String, Object>();
File f = new File("C:\\report1.pdf");
InputStream fis = new FileInputStream(f);
JasperReport jrsub = JasperCompileManager.compileReport(fis);
List<OficioSipoData> l = new ArrayList<OficioSipoData>();
for (int i = 0; i < 5; i++) {
addDummyOSD(l, i);
}
byte[] bytes = JasperRunManager.runReportToPdf(jrsub, params,
new JRBeanArrayDataSource(l.toArray()));
FileOutputStream fos = new FileOutputStream(new File(""C:\\report1.pdf""));
fos.write(bytes);
fos.close();
System.out.println("fine");
}
You can see JavaBean Data Sources sample and other samples (distributed with JasperReports library package - jasperreports-x.y.z\demo\samples\
folder) of using Exporters for optimizing your code.
Upvotes: 1