Jump to content
We've recently updated our Privacy Statement, available here ×

How to generate dynamically jasper report for JAVA


samuel.davidov

Recommended Posts

I have .jrxml with generating the PDF and everthing works fine. But sometimes i need to add dynamically another item of table's items. 

How to do that? I'm new in this field and only once to create this pdf.

Also attached the format of output.

I have this json:

{  "customerName": "derr",  "customerEmail": "sss.dd@gmail.com",  "invoiceNumber": "1",  "invoiceAmount": "180",  "invoiceDate": "2021-08-30",  "poNumber": "234234",  "productName": "Hilton Milano",  "price": "180",  "vat": "0",  "lineTotal": "180",  "currency": "EUR"}[/code]

And i think to change to new format:

{  "customerName": "derr",  "customerEmail": "sss.dd@gmail.com",  "items": [    {      "invoiceNumber": "1",      "invoiceAmount": "180",      "invoiceDate": "2021-08-30",      "poNumber": "234234",      "productName": "Hilton Milano",      "price": "180",      "vat": "0",      "lineTotal": "180",      "currency": "EUR"    },    {      "invoiceNumber": "2",      "invoiceAmount": "180",      "invoiceDate": "2021-08-30",      "poNumber": "45455",      "productName": "Item2",      "price": "180",      "vat": "0",      "lineTotal": "180",      "currency": "EUR"    }  ]}[/code]

main.jrxm

<?xml version="1.0" encoding="UTF-8"?><!-- Created with Jaspersoft Studio version 6.17.0.final using JasperReports Library version 6.17.0-6d93193241dd8cc42629e188b94f9e0bc5722efd  --><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="invoicePdf" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="30" rightMargin="30" topMargin="30" bottomMargin="50" uuid="7d0a15bc-a154-4191-a8d5-465fd3e2f940">    <property name="ireport.zoom" value="1.5"/>    <property name="ireport.x" value="0"/>    <property name="ireport.y" value="140"/>    <property name="ireport.callouts" value="##Fri Mar 30 16:17:30 NPT 2012"/>    <style name="table">        <box>            <pen lineWidth="1.0" lineColor="#000000"/>        </box>    </style>    <style name="table_TH" mode="Opaque" backcolor="#F0F8FF">        <box>            <pen lineWidth="0.5" lineColor="#000000"/>        </box>    </style>    <style name="table_CH" mode="Opaque" backcolor="#BFE1FF">        <box>            <pen lineWidth="0.5" lineColor="#000000"/>        </box>    </style>    <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">        <box>            <pen lineWidth="0.5" lineColor="#000000"/>        </box>    </style>    <style name="table 1">        <box>            <pen lineWidth="1.0" lineColor="#000000"/>        </box>    </style>    <style name="table 1_TH" mode="Opaque" backcolor="#F0F8FF">        <box>            <pen lineWidth="0.5" lineColor="#000000"/>        </box>    </style>    <style name="table 1_CH" mode="Opaque" backcolor="#BFE1FF">        <box>            <pen lineWidth="0.5" lineColor="#000000"/>        </box>    </style>    <style name="table 1_TD" mode="Opaque" backcolor="#FFFFFF">        <box>            <pen lineWidth="0.5" lineColor="#000000"/>        </box>    </style>    <subDataset name="dataset1" uuid="20b79c3c-1fd4-42fd-ad01-d369a2f1a0cd"/>    <subDataset name="Table Dataset 1" uuid="3839d134-0267-43a2-854f-041071168b36"/>    <subDataset name="Table Dataset 2" uuid="1975b989-32d5-4346-ad08-b457e77b1e29"/>    <parameter name="customerName" class="java.lang.String"/>    <parameter name="customerEmail" class="java.lang.String"/>    <parameter name="invoiceNumber" class="java.lang.String"/>    <parameter name="invoiceAmount" class="java.lang.String"/>    <parameter name="invoiceDate" class="java.lang.String"/>    <parameter name="poNumber" class="java.lang.String"/>    <parameter name="logo" class="java.lang.String"/>    <parameter name="paymentType" class="java.lang.String"/>    <parameter name="itemNo" class="java.lang.String"/>    <parameter name="productName" class="java.lang.String"/>    <parameter name="price" class="java.lang.String"/>    <parameter name="qty" class="java.lang.String"/>    <parameter name="vat" class="java.lang.String"/>    <parameter name="lineTotal" class="java.lang.String"/>    <parameter name="currency" class="java.lang.String"/>    <group name="Total">        <groupExpression>            <![CDATA[]]>        </groupExpression>        <groupHeader>            <band height="4"/>        </groupHeader>        <groupFooter>            <band height="44">                <rectangle>                    <reportElement mode="Opaque" x="310" y="14" width="223" height="25" backcolor="#C7D8ED" uuid="e811544e-cfab-4550-8286-bfdaf44eee65"/>                    <graphicElement>                        <pen lineColor="#666666"/>                    </graphicElement>                </rectangle>                <staticText>                    <reportElement x="322" y="17" width="121" height="20" uuid="c011ebd4-5c97-442e-a53f-2d7d7c8ad948"/>                    <textElement textAlignment="Center">                        <font size="12" isBold="true"/>                    </textElement>                    <text>                        <![CDATA[balance Due ]]>                    </text>                </staticText>                <textField>                    <reportElement x="424" y="17" width="89" height="20" uuid="90f7d030-b646-407e-ab10-0bb7dcd5f5ea"/>                    <textElement textAlignment="Right"/>                    <textFieldExpression>                        <![CDATA[$P{invoiceAmount}]]>                    </textFieldExpression>                </textField>                <line>                    <reportElement x="2" y="6" width="531" height="1" uuid="183f46f9-d5ef-4358-85b5-b85fa132de92"/>                </line>            </band>        </groupFooter>    </group>    <title>        <band height="84" splitType="Stretch">            <staticText>                <reportElement x="2" y="10" width="553" height="29" forecolor="#B9B9B9" uuid="9b5e2863-aa0c-4549-ae8b-f7d1cbd23db9"/>                <textElement textAlignment="Center">                    <font size="18" isBold="true"/>                </textElement>                <text>                    <![CDATA[invoice]]>                </text>            </staticText>            <image>                <reportElement x="0" y="0" width="126" height="50" uuid="dd60c047-d53d-413d-9d78-3fb63659601b"/>                <imageExpression>                    <![CDATA[$P{logo}]]>                </imageExpression>            </image>        </band>    </title>    <pageHeader>        <band height="110" splitType="Stretch">            <rectangle>                <reportElement mode="Opaque" x="315" y="1" width="218" height="96" forecolor="#000000" backcolor="#C7D8ED" uuid="1f1fcf4d-aad5-4412-89d6-f611c77ca62b"/>                <graphicElement>                    <pen lineColor="#666666"/>                </graphicElement>            </rectangle>            <staticText>                <reportElement x="325" y="3" width="93" height="20" uuid="df764129-cd10-4ab5-9a53-dea9049661ca"/>                <textElement>                    <font isBold="true"/>                </textElement>                <text>                    <![CDATA[invoice Number :]]>                </text>            </staticText>            <staticText>                <reportElement x="325" y="28" width="93" height="20" uuid="8124942b-695d-456f-89af-1d2173f76f7c"/>                <textElement>                    <font isBold="true"/>                </textElement>                <text>                    <![CDATA[invoice Date :]]>                </text>            </staticText>            <staticText>                <reportElement x="325" y="52" width="93" height="20" uuid="ad5877e4-f5f7-4590-8ff9-5fe28bc03f1c"/>                <textElement>                    <font isBold="true"/>                </textElement>                <text>                    <![CDATA[Payment Type :]]>                </text>            </staticText>            <staticText>                <reportElement x="325" y="76" width="93" height="20" uuid="c8255484-a451-45ee-9edd-00c46c10f035"/>                <textElement>                    <font isBold="true"/>                </textElement>                <text>                    <![CDATA[PO Number :]]>                </text>            </staticText>            <textField>                <reportElement x="434" y="52" width="88" height="20" uuid="3bfc9280-bf70-4e75-a78a-059c6c0db751"/>                <textFieldExpression>                    <![CDATA[$P{paymentType}]]>                </textFieldExpression>            </textField>            <textField>                <reportElement x="434" y="76" width="88" height="20" uuid="e73d80ee-3f41-4287-adee-05fc0662c19b"/>                <textFieldExpression>                    <![CDATA[$P{poNumber}]]>                </textFieldExpression>            </textField>            <textField>                <reportElement x="0" y="23" width="167" height="20" uuid="49c625cf-a655-4d84-87ad-acd5c518abce"/>                <textFieldExpression>                    <![CDATA[$P{customerEmail}]]>                </textFieldExpression>            </textField>            <textField>                <reportElement x="0" y="1" width="167" height="20" uuid="688ecd1e-e722-4fd0-ba48-be1a00e2ed56"/>                <textFieldExpression>                    <![CDATA[$P{customerName}]]>                </textFieldExpression>            </textField>            <textField>                <reportElement x="434" y="3" width="88" height="20" uuid="bc17e675-9dc6-4bbd-bea7-b34279434b79"/>                <textFieldExpression>                    <![CDATA[$P{invoiceNumber}]]>                </textFieldExpression>            </textField>            <textField pattern="MMMMM dd, yyyy">                <reportElement x="434" y="28" width="88" height="20" uuid="ac9b4d6e-5f0e-4f96-839a-de364ec4c1e1"/>                <textFieldExpression>                    <![CDATA[$P{invoiceDate}]]>                </textFieldExpression>            </textField>            <staticText>                <reportElement x="2" y="90" width="53" height="20" uuid="6445e47d-6bff-4a50-a972-f32ed369663b"/>                <textElement>                    <font isBold="true"/>                </textElement>                <text>                    <![CDATA[Currency :]]>                </text>            </staticText>            <textField>                <reportElement x="59" y="90" width="86" height="20" uuid="732a0a0e-cdd1-416c-b27a-d17ee88cbe5f"/>                <textFieldExpression>                    <![CDATA[$P{currency}]]>                </textFieldExpression>            </textField>        </band>    </pageHeader>    <columnHeader>        <band height="27" splitType="Stretch">            <rectangle>                <reportElement mode="Opaque" x="2" y="2" width="531" height="20" backcolor="#C0D4EC" uuid="92d1cf50-6c32-45de-bb2b-45e6156df9c7"/>                <graphicElement>                    <pen lineColor="#666666"/>                </graphicElement>            </rectangle>            <staticText>                <reportElement x="5" y="4" width="50" height="20" uuid="10c2e6ed-df03-4c67-8c98-7cadb4ffc402"/>                <textElement textAlignment="Left">                    <font size="12" isBold="false"/>                </textElement>                <text>                    <![CDATA[Pos.]]>                </text>            </staticText>            <staticText>                <reportElement x="70" y="4" width="179" height="20" uuid="d0f2894e-ce61-49f1-a644-3aeda6d84872"/>                <textElement textAlignment="Left">                    <font size="12" isBold="false"/>                </textElement>                <text>                    <![CDATA[Description]]>                </text>            </staticText>            <staticText>                <reportElement x="265" y="4" width="80" height="20" uuid="ebf8ca5f-0378-4615-9092-c3ce3bef6e70"/>                <textElement textAlignment="Center">                    <font size="12" isBold="false"/>                </textElement>                <text>                    <![CDATA[Amount]]>                </text>            </staticText>            <staticText>                <reportElement x="358" y="4" width="80" height="20" uuid="f2de7d8f-dfaf-46c8-9331-8a5a72eef41c"/>                <textElement textAlignment="Center">                    <font size="12" isBold="false"/>                </textElement>                <text>                    <![CDATA[Vat%]]>                </text>            </staticText>            <staticText>                <reportElement x="454" y="4" width="79" height="18" uuid="725f0632-7166-4eff-be2e-b88e176d7ef0"/>                <textElement textAlignment="Center">                    <font size="12" isBold="false"/>                </textElement>                <text>                    <![CDATA[Price]]>                </text>            </staticText>        </band>    </columnHeader>    <detail>        <band height="29" splitType="Stretch">            <textField>                <reportElement x="5" y="5" width="54" height="20" uuid="0528046a-26fc-4f5a-b7bc-21e94ff7df04"/>                <textFieldExpression>                    <![CDATA[$P{itemNo}]]>                </textFieldExpression>            </textField>            <textField>                <reportElement x="70" y="5" width="167" height="20" uuid="bcc78fda-a62f-4a3b-9b53-7b4286e98825"/>                <textFieldExpression>                    <![CDATA[$P{productName}]]>                </textFieldExpression>            </textField>            <textField>                <reportElement x="265" y="5" width="57" height="20" uuid="4b550cd8-c1bb-4205-9239-0e65592b778a"/>                <textElement textAlignment="Right"/>                <textFieldExpression>                    <![CDATA[$P{price}]]>                </textFieldExpression>            </textField>            <textField>                <reportElement x="358" y="5" width="75" height="20" uuid="c2f3c7b5-6323-4475-8dc7-1a3d6c7a15bd"/>                <textElement textAlignment="Center"/>                <textFieldExpression>                    <![CDATA[$P{vat}]]>                </textFieldExpression>            </textField>            <textField>                <reportElement x="448" y="5" width="64" height="20" uuid="cf8302d4-9cf6-4e3e-bd0e-ba820fac9430"/>                <textElement textAlignment="Right"/>                <textFieldExpression>                    <![CDATA[$P{lineTotal}]]>                </textFieldExpression>            </textField>        </band>    </detail></jasperReport>[/code]

and this is a java block that generates the PDF

 [/code]
    // Generating PDF invoice    public byte[] generateInvoiceFor(String pdfInvoiceParameters) {        try {            LOGGER.debug("Generating pdf invoice for user: [{}]", "x");            validateXmlFormat(pdfJasperReportXml);            // Load invoice jrxml template.            JasperDesign design = JRXmlLoader.load(new ByteArrayInputStream(pdfJasperReportXml.getBytes(StandardCharsets.UTF_8)));            JasperReport report = JasperCompileManager.compileReport(design);            // Create parameters map.            Map<String, Object> parameters = buildParametrsMap(pdfInvoiceParameters);            // Fill the template with parameters map.            JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());            return JasperExportManager.exportReportToPdf(jasperPrint);        } catch (SAXException saxEx) {            LOGGER.error("XML format is not correct for following PDF template {} and exception has raised : {} ", pdfJasperReportXml, saxEx.getMessage());            return null;        } catch (JsonProcessingException jsonEx) {            LOGGER.error("Json params format error for {} and exception has raised : {} ", pdfInvoiceParameters, jsonEx.getMessage());            return null;        } catch (Exception e) {            LOGGER.error("Error has occured {}", e);            return null;        }    }    // Fill template order parametres    private Map<String, Object> buildParametrsMap(String pdfInvoiceParameters) throws JsonProcessingException {        Map<String, Object> pdfInvoiceParams = mapper.readValue(pdfInvoiceParameters, Map.class);//         pdfInvoiceParams.put("logo", resourcesInvoiceImagesPath + "logo.jpg");       [/code]

Output PDF

capture.jpg.52097287056e40916ce8f7a13556973e.jpg

 

 

So actually need to add dynamically to add the items if they present in parameters like first row item1 with its price, then new row item2 with its price and etc.

Link to comment
Share on other sites

  • 1 year later...
  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...