Reputation: 15379
I tried to fill a report with Java.
My main report has some subreports with different levels.
When I try to show my report, I see only the first page, I don't know where I'm wrong:
Follow structure and Java code:
Report structure
Main report: rpt_accesso_storico.jrml First level subreport: rpt_accesso_storico_sub.jrxml Second level subreports (called by rpt_accesso_storico_sub.jrxml)
The Java code
Step 1:
In parameter hashmap I add all compiled subreports so in the .jrxml file I've added all subreports as Parameters as follow:
InputStream myIS = new ByteArrayInputStream(outByteArrayStream.toByteArray());
JasperDesign jasperDesign = JRXmlLoader.load(myIS);
JasperReport jr = JasperCompileManager.compileReport(jasperDesign);
if (entryName.equals("rpt_generale_sub.jrxml")) {
parameters.put("subreportGen", jr);
} else if (entryName.equals("rpt_ostetrico_sub.jrxml")) {
parameters.put("subreportOst", jr);
} else if (entryName.equals("rpt_infermieristico_sub.jrxml")) {
parameters.put("subreportInf", jr);
} else if (entryName.equals("rpt_sociale_sub.jrxml")) {
parameters.put("subreportSoc", jr);
} else if (entryName.equals("rpt_ginecologico_sub.jrxml")) {
parameters.put("subreportGin", jr);
} else if (entryName.equals("rpt_psicologico_sub.jrxml")) {
parameters.put("subreportPsi", jr);
} else if (entryName.equals("rpt_accesso_storico_sub.jrxml")) {
parameters.put("subreportA", jr);
} else if (entryName.equals("rpt_gen_primocontatto_sub.jrxml")) {
parameters.put("subGenPrimoContatto", jr);
}
Obviously that snippet is inside a for loop
Step 2:
I have a JasperDesign object as follow where inputStream is the main report file:
JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
Then I compile the jasperDesign in JasperReport object and finally I try to fill report with parameters where in that object I've all compiled subreports and the list of object to fill them.
But when I see the final result I've this:
My report has only one page, and the other informations are cut.
After that, I add the parameters:
Map<String, Object> myParameters = getJasperParameterMapsForAccessoStorico(lstDatiRichiesta, listPrintData, idUtente, idEnteUtente, hmListeValori);
In the pom I've used jasperreports 6.20.0
JRXML snippet code about subreport
In the main report:
<subreport>
<reportElement positionType="Float" stretchType="ContainerHeight" x="20" y="6" width="550" height="40" uuid="d114b45a-18f1-41c2-96f6-92b4c507ebd5"/>
<subreportParameter name="subreportGen">
<subreportParameterExpression><![CDATA[$P{subreportGen}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="subreportSoc">
<subreportParameterExpression><![CDATA[$P{subreportSoc}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="subreportInf">
<subreportParameterExpression><![CDATA[$P{subreportInf}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="subreportGin">
<subreportParameterExpression><![CDATA[$P{subreportGin}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="subreportOst">
<subreportParameterExpression><![CDATA[$P{subreportOst}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="subreportPsi">
<subreportParameterExpression><![CDATA[$P{subreportPsi}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="subGenPrimoContatto">
<subreportParameterExpression><![CDATA[$P{subGenPrimoContatto}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{listaEventi})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{subreportA}]]></subreportExpression>
</subreport>
In the first level subreport:
<groupHeader>
<band height="55">
<printWhenExpression><![CDATA[$F{idEventoClinico}=="EV_CLI_GEN"]]></printWhenExpression>
<staticText>
<reportElement x="0" y="0" width="130" height="20" uuid="0d05dbc4-3928-4d79-b633-e679eb7e9ea3"/>
<textElement>
<font fontName="SansSerif" size="14" isBold="true"/>
</textElement>
<text><![CDATA[Generale]]></text>
</staticText>
<subreport>
<reportElement positionType="Float" x="10" y="20" width="490" height="21" uuid="77df01e2-1477-4352-92ae-57c7a3ef56d1"/>
<subreportParameter name="subGenPrimoContatto">
<subreportParameterExpression><![CDATA[$P{subGenPrimoContatto}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{lstRichieste})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{subreportGen}]]></subreportExpression>
</subreport>
</band>
<band height="50">
<printWhenExpression><![CDATA[$F{idEventoClinico}=="EV_CLI_SOC"]]></printWhenExpression>
<staticText>
<reportElement x="0" y="0" width="130" height="20" uuid="be5d447e-7098-4073-9aca-6b7f8b368543"/>
<textElement>
<font fontName="SansSerif" size="14" isBold="true"/>
</textElement>
<text><![CDATA[Sociale]]></text>
</staticText>
<subreport>
<reportElement positionType="Float" stretchType="ContainerHeight" x="10" y="20" width="490" height="21" uuid="d4f897e4-a38f-483d-aa56-de83baec648a"/>
<subreportParameter name="subGenPrimoContatto">
<subreportParameterExpression><![CDATA[$P{subGenPrimoContatto}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{lstRichieste})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{subreportSoc}]]></subreportExpression>
</subreport>
</band>
<band height="50">
<printWhenExpression><![CDATA[$F{idEventoClinico}=="EV_CLI_INF"]]></printWhenExpression>
<staticText>
<reportElement x="0" y="0" width="130" height="20" uuid="66cca17d-f41a-4a53-9cdc-d1f47563ec70"/>
<textElement>
<font fontName="SansSerif" size="14" isBold="true"/>
</textElement>
<text><![CDATA[Infermieristico]]></text>
</staticText>
<subreport>
<reportElement positionType="Float" x="10" y="20" width="490" height="21" uuid="fed4f140-3f7c-49f0-8aca-9228559db184"/>
<subreportParameter name="subGenPrimoContatto">
<subreportParameterExpression><![CDATA[$P{subGenPrimoContatto}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{lstRichieste})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{subreportInf}]]></subreportExpression>
</subreport>
</band>
<band height="50">
<printWhenExpression><![CDATA[$F{idEventoClinico}=="EV_CLI_OST"]]></printWhenExpression>
<staticText>
<reportElement x="0" y="0" width="130" height="20" uuid="90dee808-1605-42d8-bce3-aa0afaf8d4b5"/>
<textElement>
<font fontName="SansSerif" size="14" isBold="true"/>
</textElement>
<text><![CDATA[Ostetrico]]></text>
</staticText>
<subreport>
<reportElement positionType="Float" x="10" y="20" width="490" height="21" uuid="0fac5612-6938-477d-8f9b-c879349b7008"/>
<subreportParameter name="subGenPrimoContatto">
<subreportParameterExpression><![CDATA[$P{subGenPrimoContatto}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{lstRichieste})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{subreportOst}]]></subreportExpression>
</subreport>
</band>
<band height="50">
<printWhenExpression><![CDATA[$F{idEventoClinico}=="EV_CLI_GIN"]]></printWhenExpression>
<staticText>
<reportElement x="0" y="0" width="130" height="20" uuid="df1c0abb-b7b6-4e5b-8525-83d5455f4fde"/>
<textElement>
<font fontName="SansSerif" size="14" isBold="true"/>
</textElement>
<text><![CDATA[Ginecologico]]></text>
</staticText>
<subreport>
<reportElement positionType="Float" x="10" y="20" width="490" height="21" uuid="d0118f35-1fbf-4a61-9a9b-90bc9c0c8821"/>
<subreportParameter name="subGenPrimoContatto">
<subreportParameterExpression><![CDATA[$P{subGenPrimoContatto}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{lstRichieste})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{subreportGin}]]></subreportExpression>
</subreport>
</band>
<band height="50">
<printWhenExpression><![CDATA[$F{idEventoClinico}=="EV_CLI_PSI"]]></printWhenExpression>
<staticText>
<reportElement x="0" y="0" width="130" height="20" uuid="8a3a7a2f-8920-471f-97c3-4884a2429ff4"/>
<textElement>
<font fontName="SansSerif" size="14" isBold="true"/>
</textElement>
<text><![CDATA[Psicologico]]></text>
</staticText>
<subreport>
<reportElement positionType="Float" x="10" y="20" width="490" height="21" uuid="811c8cc4-8492-4b26-9680-27bff1b97abe"/>
<subreportParameter name="subGenPrimoContatto">
<subreportParameterExpression><![CDATA[$P{subGenPrimoContatto}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{lstRichieste})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{subreportPsi}]]></subreportExpression>
</subreport>
</band>
</groupHeader>
FINAL EDIT
I've found an error in other custom method about post process of PDF file, so Jasper Studio and Jasper library are ok.
Upvotes: 0
Views: 324
Reputation: 37
I'm pretty sure the problem is not in your java itself, but in your jrxml, could you post how your code is there?
I have a very similar example, see how I did it ->
public static Object printJasper(@NonNull final Collection data,
@NonNull String titulo,
String subReport) throws JRException {
Map<String, Object> map = new HashMap<String, Object>();
map.put("SUBREPORT_DIR", new ClassPathResource(String.format("/target/classes/jasper/%s.jasper", subReport)));
JasperPrint print = JasperFillManager.fillReport(String.valueOf(new ClassPathResource(String.format("/target/classes/jasper/%s.jasper"
, titulo))),map, buildDatasource(data));
return exportPdf(print);
}
My exportPdf
private static byte[] exportPdf(final JasperPrint print) throws JRException {
final var exporter = new JRPdfExporter();
final var output = new ByteArrayOutputStream();
exporter.setExporterInput(new SimpleExporterInput(print));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(output));
exporter.exportReport();
return output.toByteArray();
}
My JRXML
<subreport>
<reportElement positionType="Float" x="1" y="0" width="553" height="20" uuid="dcb4de43-b64f-4e50-a4b1-5ad31a1cd9a2"/>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("carga.ordens")]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR}]]></subreportExpression>
</subreport>
Upvotes: 1