How can I create a report with a large summary in JasperReports

I am trying to fulfill some requirements with a report that I am developing for a desktop application. I will try to explain myself as clearly as possible for me. The situation is as follows:

I have a report with a simple table in a range <detail>, and then a very large section of some static text and 2 or 3 expressions that must match the range <summary>. At first, I tried to put all this information in <summary>, but then I figured out a 656px height limit for JasperReports ranges.

My second attempt to solve this problem was to put this static summary in a subheading. Thus, I was able to use stripes <title>and <summary>to place the fields in both stripes and I did not have to worry about limiting the height of the strip. The problem with this solution is that I could not show the page numbers (from the first report) in the sub-report section (which has 2 or 3 pages). I found the page footer and header option in the resume (checkbox in JasperStudio, which is a isSummaryWithPageHeaderAndFooter="true"property in the element <jasperReport>), but then my report gave me a compilation error when previewing; I don’t know if this is an error in JasperStudio: I tried in the last two versions, and the error was the same.

Finally, I tried to add a second group <detail>with another query that returns only one value. The problem is that I could not put the second strip of details “below” the first: in the preview, I see one row in a row from each group, and not the way I need to. After a long search, I found out that this is impossible.

Problem Summary Requirements

The summary has the following requirements

  • It has separate static text fields and some other expressions with calculated fields and parameters (formatted dates, etc.).
  • These pages should be paginated (page [current] of [total] in the footer)
  • The data source and query are the same for the main report and summary

, 656 , . . .

isSummaryWithPageHeaderAndFooter="true"

Jaspersoft Studio , ( , IDE ):

The first report state

,

Status of the second report

, . : 6.3.1 6.4.0 Windows 7, Mac OS. , IDE

compile report parameter

( , .jasper). PDF ( JasperViewer),

. Java .

.

+4
2

, , , , , . , , , JasperReports.

1.-

, 2 , . PAGE_COUNT_OFFSET Integer. , , :

<pageFooter>
    <band height="54" splitType="Stretch">
        <textField>
            <reportElement x="146" y="2" width="100" height="30" uuid="1314c392-e24a-47bd-a0aa-6b19803be36a"/>
            <textElement textAlignment="Right"/>
            <textFieldExpression><![CDATA["- Page " + $V{PAGE_NUMBER}]]></textFieldExpression>
        </textField>
        <textField evaluationTime="Report">
            <reportElement x="246" y="2" width="100" height="30" uuid="230a6e2d-3e6d-4d52-9228-aab519122537"/>
            <textElement textAlignment="Left"/>
            <textFieldExpression><![CDATA[" of " + ($V{PAGE_NUMBER} + $P{PAGE_COUNT_OFFSET}) + " -"]]></textFieldExpression>
        </textField>
    </band>
</pageFooter>

<pageFooter>
    <band height="50">
        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
        <textField>
            <reportElement x="146" y="2" width="100" height="30" uuid="b6a836b2-41f5-4a61-af64-50720544cef2"/>
            <textElement textAlignment="Right"/>
            <textFieldExpression><![CDATA["- Page " + ($V{PAGE_NUMBER} + $P{PAGE_COUNT_OFFSET})]]></textFieldExpression>
        </textField>
        <textField evaluationTime="Report">
            <reportElement x="246" y="2" width="100" height="30" uuid="be5469d3-10ed-4deb-964d-c1f9c9b7337a"/>
            <textElement textAlignment="Left"/>
            <textFieldExpression><![CDATA[" of " + ($V{PAGE_NUMBER} + $P{PAGE_COUNT_OFFSET}) + " -"]]></textFieldExpression>
        </textField>
    </band>
</pageFooter>

: , .

2.-

, , .jasper . Java-. main, - .

. , .jasper JAR .

public static void main(String[] args) {
    try {
        // Build parameters map with fake offset
        Map<String, Object> subreportParams = new HashMap<>();
        // PARAM_PAGE_COUNT_OFFSET is a convenient String constant with the parameter name for the report
        subreportParams.put(PARAM_PAGE_COUNT_OFFSET, 10);

        Map<String, Object> mainParams = new HashMap<>(subreportParams);
        mainParams.put(...); // Some other parameters

        // Fill the report the first time and get the real page count for every report
        ClassLoader classLoader = getClass().getClassLoader();
        // Again, MAIN_REPORT_FILE and SUBREPORT_FILE are String constants containing jasper files names
        // JdbcManager.getConnection() is a utility method which gets a connection with predefined parameters
        JasperPrint main = JasperFillManager.fillReport(classLoader.getResourceAsStream(MAIN_REPORT_FILE), mainParams, JdbcManager.getConnection());
        JasperPrint subreport = JasperFillManager.fillReport(classLoader.getResourceAsStream(SUBREPORT_FILE), subreportParams, JdbcManager.getConnection());

        // Get the page count for every report and reinsert it in the parameters map
        int mainPageCount = main.getPages().size();
        int subreportPageCount = subreport.getPages().size();
        // The offset for the given report should be the count from the other
        mainParams.put(PARAM_PAGE_COUNT_OFFSET, subreportPageCount);
        subreportParams.put(PARAM_PAGE_COUNT_OFFSET, mainPageCount);

        // Fill with the final parameters and generates a JpList object
        main = JasperFillManager.fillReport(classLoader.getResourceAsStream(MAIN_REPORT_FILE), mainParams, JdbcManager.getConnection());
        subreport = JasperFillManager.fillReport(classLoader.getResourceAsStream(SUBREPORT_FILE), subreportParams, JdbcManager.getConnection());
        List<JasperPrint> finalReport = new ArrayList<>();
        finalReport.add(main);
        finalReport.add(subreport);

        // Export the report and save it to a given path
        JRPdfExporter exporter = new JRPdfExporter();
        exporter.setExporterInput(SimpleExporterInput.getInstance(finalReport));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(
                new FileOutputStream("path/to/report.pdf")));
        exporter.exportReport();
    } catch (JRException ex) {
        LOGGER.log(Level.SEVERE, "Error generating report", ex);
    } catch (FileNotFoundException ex) {
        LOGGER.log(Level.SEVERE, "Error saving file", ex);
    }
}

, PDF . , . .

0

, .

0

Source: https://habr.com/ru/post/1685050/


All Articles