How to use relative path to reference json schema when exporting to Json? - jasper-reports

I have a report as simple as this:
<?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="testJsonExportWithSchema" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f59243f1-8df7-4c2a-b075-3ca20c4b17f2">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<property name="net.sf.jasperreports.export.json.schema" value="schema.json"/>
<queryString>
<![CDATA[]]>
</queryString>
<detail>
<band height="63" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="555" height="63" uuid="7801126d-70f5-4d0c-9c19-3588dc5fce40">
<property name="net.sf.jasperreports.export.json.path" value="key"/>
</reportElement>
<textFieldExpression><![CDATA["value"]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
And would like to export it to Json using below Json schema:
{
_type: 'object',
key: 'value'
}
Using a relative path to schema (as I do with subreports, images and other resources on the same folder):
<property name="net.sf.jasperreports.export.json.schema" value="schema.json"/>
I get this:
net.sf.jasperreports.engine.JRException: Input stream not found at: schema.json
If I use absolute path it works fine:
<property name="net.sf.jasperreports.export.json.schema" value="c:\users\username\path\to\schema.json"/>
This need for an absolute path is preventing me from using Json schemas in JasperReports Server too.

Related

How to horizontally print a list in subreport in Jasper Report

I have a two models Customer and Order as follow:
class Customer{
private String customerName;
private List<Order> orders;
}
class Order{
private String orderName;
}
I am creating a pdf that lists all the customer name vertically and its corresponding orders horizontally which look like below:
customer1 order1 order2
customer2 order3 order4
.
.
.
where order1 and order2 belong to customer1 and order3 and order4 belong to customer2.
What I did is for each customer object, i will print the customer name and pass the orders list to another subreport and inside this subreport, i will print all of the element in this orders list but horizontally instead of vertically.
Here is my jrxml code:
My main report:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<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="epicsReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="98e422a6-47f1-4bf1-9436-b6cbfdc8c904">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<parameter name="ordersSubReport" class="net.sf.jasperreports.engine.JasperReport"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="customerName" class="java.lang.String"/>
<field name="orders" class="java.util.List"/>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="25" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="270" height="25" uuid="26bf39b4-9072-4edc-a846-3141bff7da16">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="8d2c2571-e603-4eba-8048-3053e3282a99"/>
</reportElement>
<textFieldExpression><![CDATA[$F{customerName}]]></textFieldExpression>
</textField>
<subreport>
<reportElement x="312" y="0" width="200" height="25" uuid="b2675578-87cc-4a7b-8b5a-2c8af0fbde73"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{orders})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{ordersSubReport}]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>
My subreport:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<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="skillsDataReport" columnCount="10" printOrder="Horizontal" pageWidth="595" pageHeight="842" columnWidth="55" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c6d95fd0-d8af-4727-999c-9804a17721e4">
<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"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="orderName" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="25" splitType="Stretch">
<textField>
<reportElement x="9" y="0" width="100" height="25" uuid="1ac7d520-9918-4106-b4ad-1b446da6a01f"/>
<textFieldExpression><![CDATA[$F{orderName}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
As data source for main report I pass a list of customers to it: new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{customers})
And as data source for my subreport, for each customer object I pass a list of orders to it: new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{orders})
The codes were compiled successfully but the orders list were not shown as I expected. I see only the list of customerName are printed but not the orders list.
What did I do wrong here? Any help will be a big glue for me :)

Jaspersoft studio json data adapter book or subreport document is Empty

I am trying to do something really simple yet after searching for the past few days I could not find an answer, I need some help on this one.
What I am trying to do:
I have multiple reports so A4 portrait some landscape, some with just static text some with tables, some with tables and variables and I want to put merge them together.
They work 100% individually.
Tried to make a master report with subreports did not work, tried the book approach did not work, I always get Document is empty, or if I try to manipulate the data query I get all kind of weird stuff.
Now I will try to make this example as simple as possible.
JSON data: (New data Adapter)
{
"name": "Sample Name",
"people":[
{
"who": "Person 1"
},
{
"who": "Person 2"
},
{
"who": "Person 3"
}
]
}
Book master report:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<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="Empty_Book" pageWidth="595" pageHeight="842" sectionType="Part" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" uuid="d2716064-8ae4-40cf-a575-33afba400e3a">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="New Data Adapter "/>
<property name="net.sf.jasperreports.print.create.bookmarks" value="true"/>
<queryString language="json">
<![CDATA[]]>
</queryString>
<detail>
<part uuid="69d6ca13-26f6-425f-bff9-395c5b9c183b">
<p:subreportPart xmlns:p="http://jasperreports.sourceforge.net/jasperreports/parts" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/parts http://jasperreports.sourceforge.net/xsd/parts.xsd">
<subreportParameter name="REPORT_CONNECTION">
<subreportParameterExpression><![CDATA[$P{REPORT_CONNECTION}]]></subreportParameterExpression>
</subreportParameter>
<subreportExpression><![CDATA["Blank_A4_2.jasper"]]></subreportExpression>
</p:subreportPart>
</part>
<part uuid="055c9f89-88b4-4270-b6ef-addb2eac3e56">
<p:subreportPart xmlns:p="http://jasperreports.sourceforge.net/jasperreports/parts" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/parts http://jasperreports.sourceforge.net/xsd/parts.xsd">
<subreportParameter name="REPORT_CONNECTION">
<subreportParameterExpression><![CDATA[$P{REPORT_CONNECTION}]]></subreportParameterExpression>
</subreportParameter>
<subreportExpression><![CDATA["Blank_A4_Landscape_1.jasper"]]></subreportExpression>
</p:subreportPart>
</part>
</detail>
</jasperReport>
Blank_A4_2.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<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_2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="bd2bd70c-5a0d-4c3f-b81d-4d5d7731153d">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="New Data Adapter "/>
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="name" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="name"/>
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="150" y="30" width="100" height="30" uuid="eef5f1dc-3d96-4008-8321-ef0be92d42d4"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="50" y="30" width="100" height="30" uuid="cbe6173f-5399-45ec-926b-ff6f48b4fb0c"/>
<text><![CDATA[name]]></text>
</staticText>
</band>
</detail>
</jasperReport>
Blank_A4_Landscape_1.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<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_Landscape_1" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="62f06d09-42b5-4471-baa5-5aed60cc5fc4">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="New Data Adapter "/>
<subDataset name="Dataset1" uuid="eb54ec0d-0c60-44af-8476-e44f40560dee">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="New Data Adapter "/>
<queryString language="json">
<![CDATA[people]]>
</queryString>
<field name="who" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="who"/>
<fieldDescription><![CDATA[who]]></fieldDescription>
</field>
</subDataset>
<queryString language="JSON">
<![CDATA[]]>
</queryString>
<field name="name" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="name"/>
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<columnHeader>
<band height="175">
<textField>
<reportElement x="132" y="81" width="100" height="30" uuid="5cce1f44-2a8b-42ad-a03d-4c294f02d31c"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</columnHeader>
<detail>
<band height="140" splitType="Stretch">
<componentElement>
<reportElement x="110" y="45" width="200" height="40" uuid="75a06812-f9a0-4b0f-9a08-efec60d0b555"/>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="Dataset1" uuid="acd62160-c07c-4c00-b6b4-9ef001d1585d">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("people")]]></dataSourceExpression>
</datasetRun>
<jr:column width="200" uuid="253a610c-a780-4690-8cce-8a03597ef016">
<jr:detailCell height="30">
<textField>
<reportElement x="0" y="0" width="200" height="30" uuid="910a7e24-c02b-4b21-8c9d-6a3dfda21794"/>
<textFieldExpression><![CDATA[$F{who}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
</jasperReport>
Tested all files before posting, individually they work, I have no params to send to the subreports, I tried looping over the array and still nothing I always get document is Empty no matter what I do.
Passing the REPORT_CONNECTION to each subreport part has no effect. It's value will be non-null for JDBC connections only. So there's no reason to pass it.
You could have your setup running by tying each subreport to the Data Adapter file:
Export the Data Adapter to a file, say JSON_DA.xml, on the same level with your reports.
Add this property to each of the subreports:
<property name="net.sf.jasperreports.data.adapter" value="JSON_DA.xml"/>
(This could also be done by setting the Default Data Adapter in the Report Properties tab)
Recompile each subreport then preview the Master report again.
All this is necessary because JasperSoft Studio does not inject the Data Adapter into each subreport at runtime. It only injects it when each report is individually run(previewed) by automatically setting the com.jaspersoft.studio.data.defaultdataadapter property.

Error Subdataset with "One empty Record"

I want to see a report with all the fields empty but without a source, so i have to do it with "One empty Record". The main problem is that whatever i do, if there is a JRXmlDataSource in the code that i put in the JRDatasource expression, it will throw me an error if i put "One empty Record" as datasource.
Here is the code:
IF($P{REPORT_DATA_SOURCE}.toString().toLowerCase().contains("net.sf.jasperreports.engine.jremptydatasource"),new net.sf.jasperreports.engine.JREmptyDataSource(),((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("SOME XPATH HERE"))
I've been looking all day but all the things i do go to the same exact point.
Caused by: java.lang.ClassCastException: net.sf.jasperreports.engine.JREmptyDataSource cannot be cast to net.sf.jasperreports.engine.data.JRXmlDataSource
You should check that $P{REPORT_DATA_SOURCE} is subclass of net.sf.jasperreports.engine.JREmptyDataSource of not.
The valid expression in your case will be:
<dataSourceExpression><![CDATA[IF(($P{REPORT_DATA_SOURCE} instanceof net.sf.jasperreports.engine.JREmptyDataSource), new net.sf.jasperreports.engine.JREmptyDataSource(), ((net.sf.jasperreports.engine.data.JRXmlDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("SOME XPATH HERE"))]]></dataSourceExpression>
For example this Java code will work with jrxml below:
Map<String, Object> params = new HashMap<>();
//params.put("REPORT_DATA_SOURCE", new JRXmlDataSource("customers.xml", "/Customers/*")); // this is working expression also
params.put("REPORT_DATA_SOURCE", new JREmptyDataSource(1)); // one record dataset
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
Template:
<?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="Report with check of datasource" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<parameter name="REPORT_DATASOURCE" class="java.lang.Object"/>
<field name="id" class="java.lang.String">
<fieldDescription><![CDATA[child::text()]]></fieldDescription>
</field>
<variable name="expression" class="java.lang.String">
<variableExpression><![CDATA["/Customers/Customer[CustomerID='" + $F{id} + "']"]]></variableExpression>
</variable>
<detail>
<band height="70" splitType="Stretch">
<textField>
<reportElement x="100" y="0" width="100" height="15"/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="0" width="100" height="15"/>
<text><![CDATA[Id:]]></text>
</staticText>
<subreport>
<reportElement x="54" y="15" width="380" height="45"/>
<dataSourceExpression><![CDATA[IF(($P{REPORT_DATA_SOURCE} instanceof net.sf.jasperreports.engine.JREmptyDataSource), new net.sf.jasperreports.engine.JREmptyDataSource(), ((net.sf.jasperreports.engine.data.JRXmlDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("SOME XPATH HERE"))]]></dataSourceExpression>
<subreportExpression><![CDATA["./jrxml/subreport_with_xml.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>

Multipage text report template with Jasper (or Birt)

In iReport I can create single page template easy, but when have long static text with some inline fields, can't make that multipage template.
I tried create text field in raport, with very long static text and some variables inline:
<?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="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="82249f31-5b1d-4bff-951d-ad6da7f743c3">
<field name="field1" class="java.lang.String"/>
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement uuid="d966bf72-21f3-4393-b69b-1e4f36b31255" x="0" y="0" width="555" height="79" isPrintInFirstWholeBand="true" isPrintWhenDetailOverflows="true"/>
<textElement>
<font size="24"/>
</textElement>
<textFieldExpression><![CDATA["some text some some: " + $F{field1} + "very long textvery long text very long text verymultipage ...... "]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
Effect: only one page document.
So How I can create multipage template?
Would like template like this:
Have you tried BIRT yet? The text field uses the BIRT master page size that you set. If there is more content than can fit into the master page size, additional pages are created and your content flows into the other pages.

How to ignore margins in template: using image

I have JasperReports template with
leftMargin="100" rightMargin="50" topMargin="50" bottomMargin="50"
margins. In this template I have <image> element. I want to stretch the image to entire page ignoring the page margins.
Please, could you help me to find solution?
<summary>
<band splitType="Stretch">
<image isUsingCache="false" onErrorType="Blank">
<reportElement uuid="7569" x="0" y="0"/>
<imageExpression><![CDATA[$P{aaa}]]></imageExpression>
</image>
</band>
</summary>
You can't ignore the buttom margin, but the other ones by using a negative position. (Be aware that iReport will not let you enter that in design mode)
<?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="report name" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5c06ba5e-7f96-4b81-bb6a-fcfe8cc2d13f">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="10"/>
<summary>
<band height="802" splitType="Stretch">
<image>
<reportElement uuid="a104abd7-bd73-47c4-a4a1-47efeb32d8bf" x="-20" y="-20" width="594" height="822"/>
<imageExpression><![CDATA["/home/mor/Desktop/schlachtbeil.jpg"]]></imageExpression>
</image>
</band>
</summary>
</jasperReport>
The reason you can't ignore the bottom margin is that you cannot expand your band to more than pageHeight - (topMargin + bottomMargin). And every element cant have a height larger than bandHeight - position
It look like that you have not defined your image element very well.
Here is an example by which you can get whatever you want.
<summary>
<band height="471" splitType="Stretch">
<image scaleImage="FillFrame">
<reportElement uuid="45a2de1b-a048-4c65-b5cd-cefabdd39bca" x="0" y="0" width="555" height="471"/>
<imageExpression><![CDATA["/path/to/your/image"]]></imageExpression>
</image>
</band>
</summary>
Hope It will help you. Enjoy.