Jump to content
JasperReports Library 7.0 is now available ×

ilyademidow

Members
  • Posts

    1
  • Joined

  • Last visited

ilyademidow's Achievements

Newbie

Newbie (1/14)

  • Week One Done
  • One Month Later
  • One Year In
  • First Post Rare
  • Conversation Starter Rare

Recent Badges

0

Reputation

  1. I build reports dynamicaly with Java. My report is just a compilation of a subreports. Everything works, but when I add subreport to report's pageHeader - it appears on the first page only. When I place some static text, instead of subreport, to pageHeader - everything works fine. Is it Jasper bug or is it my mistake somewhere? Here is jrxml of subreport jsp_sub_header_test.jrxml: <?xml version="1.0" encoding="UTF-8"?><!-- Created with Jaspersoft Studio version 6.18.1.final using JasperReports Library version 6.18.1-9d75d1969e774d4f179fb3be8401e98a0e6d1611 --><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="jsp_sub_header_test" pageWidth="612" pageHeight="792" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" whenResourceMissingType="Error" uuid="b9273db7-6a89-414a-b01e-7c9cecf2fe0d"> <queryString> <![CDATA[]]> </queryString> <detail> <band height="60" splitType="Stretch"> <staticText> <reportElement x="10" y="0" width="100" height="20" uuid="1f330eb5-7c26-4d49-bda9-f86599bb953a"/> <text><![CDATA[subreport static test]]></text> </staticText> </band> </detail></jasperReport> Here is my master report template.jrxml <?xml version="1.0" encoding="UTF-8"?><!-- Created with Jaspersoft Studio version 6.18.1.final using JasperReports Library version 6.18.1-9d75d1969e774d4f179fb3be8401e98a0e6d1611 --><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="Blank_A4_1" pageWidth="595" pageHeight="842" columnWidth="565" leftMargin="15" rightMargin="15" topMargin="0" bottomMargin="0" uuid="a10e6fdc-78ab-408a-aa6d-22884e585a7e"> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/> <property name="com.jaspersoft.studio.unit." value="pixel"/> <property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/> <property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/> <property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/> <property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/> <property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/> <property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/> <property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/> <property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/> <property name="com.jaspersoft.studio.unit.height" value="px"/> <property name="com.jaspersoft.studio.unit.width" value="px"/> <property name="com.jaspersoft.studio.unit.x" value="px"/> <property name="com.jaspersoft.studio.unit.y" value="px"/> <property name="com.jaspersoft.studio.unit.leftIndent" value="px"/> <property name="com.jaspersoft.studio.unit.spacingBefore" value="px"/> <property name="com.jaspersoft.studio.unit.firstLineIndent" value="px"/> <property name="net.sf.jasperreports.json.source" value="northwind.json"/> <style name="Right_padding"> <box rightPadding="3"/> </style> <parameter name="list" class="ru.alfastrah.productstream.printformdesigner.dto.Block"/> <queryString language="json"> <![CDATA[]]> </queryString></jasperReport> Here how I generate PDF: public byte[] generatePdf(List<Block> blocks) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { // All magic happens here JasperPrint jasperPrint = buildJasper(blocks); JRPdfExporter exporter = new JRPdfExporter(); exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream)); exporter.exportReport(); } catch (JRRuntimeException | JRException e) { throw new ReportProviderException(e.getMessage(), e); } return outputStream.toByteArray(); } All magic happens in buildJasper(blocks): private JasperPrint buildJasper(List<Block> blocks) throws ReportProviderException, JRException { // read master report jrxml InputStream templateInputStream = getClass().getClassLoader().getResourceAsStream("template.jrxml"); if (templateInputStream == null) { throw new ReportProviderException("Can't find classpath:template.jrxml"); } JasperDesign jasperDesign = JRXmlLoader.load(templateInputStream); Map<String, Object> params = new HashMap<>(); // here is code for inserting Bloks, nevermind ..... // create a header band JRDesignBand headerBand = new JRDesignBand(); headerBand.setHeight(60); try { // set our subreports location JRDesignParameter subrepDir = new JRDesignParameter(); subrepDir.setName("SUBREPORT_DIR"); subrepDir.setValueClassName("java.lang.String"); jasperDesign.addParameter(subrepDir); params.put("SUBREPORT_DIR", jasperSubreportsPath + "/testStatic/"); // create dummy data source otherwise it doesn't work JRDesignParameter dsParameter = new JRDesignParameter(); dsParameter.setName("111"); dsParameter.setValueClassName("net.sf.jasperreports.engine.JREmptyDataSource"); jasperDesign.addParameter(dsParameter); params.put("111", new JREmptyDataSource()); JRDesignSubreport jSubreport = new JRDesignSubreport(jasperDesign); jSubreport.setWidth(31); jSubreport.setHeight(5); jSubreport.setX(0); jSubreport.setY(0); // add our dummy data source to report JRDesignExpression dataSourceExpression = new JRDesignExpression(); dataSourceExpression.setText(String.format("$P{%s}", "111")); jSubreport.setDataSourceExpression(dataSourceExpression); // this lines to see a preview in JasperSoft Studio (it isn't shown in preview without this lines) JRDesignExpression paramMapExpr = new JRDesignExpression(); paramMapExpr.setText("$P{REPORT_PARAMETERS_MAP}"); jSubreport.setParametersMapExpression(paramMapExpr); // add our subreport expression JRDesignExpression expression1 = new JRDesignExpression(); expression1.setText("$P{SUBREPORT_DIR} + "jsp_sub_header_test.jasper""); jSubreport.setExpression(expression1); headerBand.addElement(jSubreport); // let's add dummy static text to header to check if it works JRDesignStaticText staticText = new JRDesignStaticText(); staticText.setText("header created in Java"); staticText.setX(180); staticText.setY(10); staticText.setWidth(170); staticText.setHeight(40); headerBand.addElement(staticText); jasperDesign.setPageHeader(headerBand); } catch (Exception e) { System.err.println(e); } JasperReport report = JasperCompileManager.compileReport(jasperDesign); // let's save result to check thru JasperSoft Studio JRXmlWriter.writeReport(report, "output.jrxml", "utf-8"); return JasperFillManager.fillReport(report, params); } The result in my SprintBoot application... What we see (Pic 1)... Our static text "header created in Java" presents at all pages. But our subreport text presents at 1st page only :-(( Here is a result in my SpringBoot application. Our static text "header created in Java" presents at all pages. But our subreport text presents at 1st page only Pic 1. Subreport isn't shown at 2nd page Here is compiled jrxml output.jrxml: <?xml version="1.0" encoding="UTF-8"?><!-- Created with Jaspersoft Studio version 6.18.1.final using JasperReports Library version 6.18.1-9d75d1969e774d4f179fb3be8401e98a0e6d1611 --><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="Blank_A4_1" pageWidth="595" pageHeight="842" columnWidth="565" leftMargin="15" rightMargin="15" topMargin="0" bottomMargin="0" uuid="a10e6fdc-78ab-408a-aa6d-22884e585a7e"> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="New Data Adapter "/> <property name="com.jaspersoft.studio.unit." value="pixel"/> <property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/> <property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/> <property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/> <property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/> <property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/> <property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/> <property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/> <property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/> <property name="com.jaspersoft.studio.unit.height" value="px"/> <property name="com.jaspersoft.studio.unit.width" value="px"/> <property name="com.jaspersoft.studio.unit.x" value="px"/> <property name="com.jaspersoft.studio.unit.y" value="px"/> <property name="com.jaspersoft.studio.unit.leftIndent" value="px"/> <property name="com.jaspersoft.studio.unit.spacingBefore" value="px"/> <property name="com.jaspersoft.studio.unit.firstLineIndent" value="px"/> <property name="net.sf.jasperreports.json.source" value="northwind.json"/> <style name="Right_padding"> <box rightPadding="3"/> </style> <parameter name="list" class="ru.alfastrah.productstream.printformdesigner.dto.Block"/> <parameter name="documentName" class="net.sf.jasperreports.engine.JasperReport"/> <parameter name="breakPage" class="net.sf.jasperreports.engine.JasperReport"/> <parameter name="documentValue" class="net.sf.jasperreports.engine.JasperReport"/> <parameter name="bonus" class="net.sf.jasperreports.engine.JasperReport"/> <parameter name="528080403" class="net.sf.jasperreports.engine.data.JsonDataSource"/> <parameter name="1634309175" class="net.sf.jasperreports.engine.data.JsonDataSource"/> <parameter name="805823154" class="net.sf.jasperreports.engine.data.JsonDataSource"/> <parameter name="1504099648" class="net.sf.jasperreports.engine.data.JsonDataSource"/> <parameter name="SUBREPORT_DIR" class="java.lang.String"/> <parameter name="111" class="net.sf.jasperreports.engine.JREmptyDataSource"/> <queryString language="json"> <![CDATA[Northwind.Customers]]> </queryString> <pageHeader> <band height="60"> <subreport> <reportElement x="0" y="0" width="31" height="5" uuid="2170b9e2-6acc-4669-b5ce-0123ef401973"/> <parametersMapExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}]]></parametersMapExpression> <dataSourceExpression><![CDATA[$P{111}]]></dataSourceExpression> <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "jsp_sub_header_test.jasper"]]></subreportExpression> </subreport> <staticText> <reportElement x="180" y="10" width="170" height="40" uuid="94cae0d3-0797-4e73-ad03-4f9d48cb552c"/> <text><![CDATA[header created in Java]]></text> </staticText> </band> </pageHeader> <detail> <band height="10"> <subreport> <reportElement x="0" y="0" width="31" height="5" uuid="9c3ade74-091a-4ae5-96c9-ce2089410819"/> <dataSourceExpression><![CDATA[$P{528080403}]]></dataSourceExpression> <subreportExpression><![CDATA[$P{documentName}]]></subreportExpression> </subreport> </band> <band height="10"> <subreport> <reportElement x="0" y="0" width="31" height="5" uuid="ac5c29d5-41c4-404b-bdcc-a425a458b628"/> <dataSourceExpression><![CDATA[$P{1634309175}]]></dataSourceExpression> <subreportExpression><![CDATA[$P{breakPage}]]></subreportExpression> </subreport> </band> <band height="10"> <subreport> <reportElement x="0" y="0" width="31" height="5" uuid="c3b0be66-7b6f-4c42-a964-7807afc29507"/> <dataSourceExpression><![CDATA[$P{805823154}]]></dataSourceExpression> <subreportExpression><![CDATA[$P{documentValue}]]></subreportExpression> </subreport> </band> <band height="10"> <subreport> <reportElement x="0" y="0" width="31" height="5" uuid="f5597aff-187f-4006-8f27-3cbe484dd2be"/> <dataSourceExpression><![CDATA[$P{1504099648}]]></dataSourceExpression> <subreportExpression><![CDATA[$P{bonus}]]></subreportExpression> </subreport> </band> </detail></jasperReport> Interesting things. When I run this output.jrxml in JasperSoft Studio everything works correctly (Pic 2) O_O Pic 2. It works in JasperSoft Studio According that I doubt if the problem in JasperFillManager or JasperCompileManager... Does anyone understand what's wrong with me or with my code?)) UPDATE: I use Java OpenJDK 11 and <dependency><groupId>net.sf.jasperreports</groupId><artifactId>jasperreports</artifactId><version>6.17.0</version></dependency>
×
×
  • Create New...