I'm using Jasper Reports to build a simple report pdf. I have a JSON file that looks like this:
{"employees": [
{"firstName" : "John", "lastName" : "Doe"},
{"firstName" : "Anna", "lastName" : "Smith"},
{"firstName" : "Peter", "lastName" : "Jones"}
]}
And I'm trying to read it in like this:
File file = new File("E:/Workspaces/jasperPDFreport/src/main/resources/emp.json");
JsonDataSource datasource = new JsonDataSource(file);
JasperDesign jasperDesign = JRXmlLoader.load("E:/Workspaces/jasperPDFreport/src/main/resources/jsonTemplate.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
Map parameters = new HashMap();
JasperPrint jasperPrint;
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, datasource);
JasperExportManager.exportReportToPdfFile(jasperPrint, "BasicReport.pdf");
JasperViewer.viewReport(jasperPrint);
However my the values from the JSON file are not passed to my pdf.
This is my Template:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.1.1.final using JasperReports Library version 6.1.1 -->
<!-- 2015-10-22T13:45:32 -->
<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="9e494ebe-c1fb-4448-bcee-38994e9720f7">
<!--property name="net.sf.jasperreports.json.source" value="emp.json"/-->
<queryString language="json">
<![CDATA[employees]]>
</queryString>
<field name="firstName" class="java.lang.String">
<fieldDescription><![CDATA[firstName]]></fieldDescription>
</field>
<field name="lastName" class="java.lang.String">
<fieldDescription><![CDATA[lastName]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="100" y="0" width="100" height="30" uuid="02b279da-3795-4655-8571-5a36a3ef378c"/>
<textFieldExpression><![CDATA[$F{firstName}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="671e61ad-8d8f-48cb-969f-78c05a516398"/>
<text><![CDATA[firstName]]></text>
</staticText>
<textField>
<reportElement x="100" y="30" width="100" height="30" uuid="9d53f46f-a252-48b3-9213-8c3092c29f49"/>
<textFieldExpression><![CDATA[$F{lastName}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="30" width="100" height="30" uuid="3b49affb-685a-4df2-a872-c0e6fdcab94b"/>
<text><![CDATA[lastName]]></text>
</staticText>
</band>
</detail>
</jasperReport>
Now you see the commented out line
property name="net.sf.jasperreports.json.source" value="emp.json"
If I comment this in, everything works as intended, I don't want to hard code my JSON values into the template, because later on I want to get them from a rest service, that's not ready yet. I do not understand, why the values are not getting parsed into the report, instead i just get two null values.
From JasperReports - JSON Data Source Sample (version 6.4.3)
The built-in JSON query executer (see the JsonQueryExecuter class) is a tool that uses the query string to produce a JsonDataSource instance, based on specific built-in parameters (or equivalent report properties). This query executer is registered via JsonQueryExecuterFactory factory class.
In order to prepare the data source, the JSON query executer looks for the JSON_INPUT_STREAM parameter that contains the JSON source objects in the form of an java.io.InputStream. If no JSON_INPUT_STREAM parameter is provided, then the query executer looks for the alternate net.sf.jasperreports.json.source String parameter or report property that stores the path to the location of the JSON source file.
JsonQueryExecuter runs the query over the input source and stores the result in an in-memory JsonDataSource object.
So if you do not want to use:
<property name="net.sf.jasperreports.json.source" value="emp.json"/>
You need to pass the file as java.io.InputStream in the parameter JSON_INPUT_STREAM
Hence you are currently passing it as datasource you should try something like this
params.put(JsonQueryExecuterFactory.JSON_INPUT_STREAM, new FileInputStream(file));
JasperFillManager.fillReportToFile(jasperReport, params);
If you instead like to use the new JsonQLQueryExecuterFactory JSONQL Data Source
params.put(JsonQLQueryExecuterFactory.JSON_INPUT_STREAM, new FileInputStream(file));
JasperFillManager.fillReportToFile(jasperReport, params);
If you pass your json string as InputStream then it will works.
String reportContents = "{}" //your json
InputStream is = new ByteArrayInputStream(reportContent.getBytes());
Map params = new HashMap();
params.put(JsonQueryExecuterFactory.JSON_INPUT_STREAM, is);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params);
Take a look here for data source implementation that wraps a collection of JavaBean objects.
List<YourClass> yourBeanCollection = queryDataFromJSON();
JRDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(yourBeanCollection);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
reportParams, beanCollectionDataSource);
and in the report template import java.util and declare the collection which you "injected" in the report
<import value="java.util.*"/>
<field name="yourBeanCollection" class="java.util.List"/>
also take a look here for an example
Related
I have created a jasper report template using jaspersoft studio and I am populating the template using java code. I have some data in the report which needs to be localize and for that I am seeting the "locale" by below code in java.
Locale locale = new Locale("zh", "CN");
templateParameters.put("REPORT_LOCALE", locale); //A map to pass to report
I have also tried -
Locale locale = java.util.Locale.CHINA;
In populated report "Number formatting" is there but currecny symbol is missing(only dollar, pound, euro symbols are coming)
Below is the code I have used in jasper report to populate the text field -
NumberFormat.getCurrencyInstance($P{REPORT_LOCALE}).format($P{Param_Name})
I would be very thankful if someone could pointout the mistake or provide some suggestion.
I believe that your issue related with font supporting.
We should use Font Extensions in case using JRPdfExporter.
I tried to use font with Chinese support and in this case everything is OK.I don't know why using a ton of another fonts is not helping
Example
Java code
Map<String, Object> params = new HashMap<>();
params.put(JRParameter.REPORT_LOCALE, Locale.CHINA);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());
Report template
There are 4 textFields in jrxml file: two using font with Chinese support and another 2 - without it.
<?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="Show currency" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<parameter name="value" class="java.lang.Double" isForPrompting="false">
<defaultValueExpression><![CDATA[1234.567]]></defaultValueExpression>
</parameter>
<title>
<band height="70">
<textField>
<reportElement x="10" y="10" width="300" height="15"/>
<textElement>
<font fontName="Sharp Dawn"/>
</textElement>
<textFieldExpression><![CDATA[NumberFormat.getCurrencyInstance(new Locale("zh", "CN")).format($P{value})]]></textFieldExpression>
</textField>
<textField>
<reportElement x="10" y="25" width="300" height="15"/>
<textElement>
<font fontName="Sharp Dawn"/>
</textElement>
<textFieldExpression><![CDATA[NumberFormat.getCurrencyInstance($P{REPORT_LOCALE}).format($P{value})]]></textFieldExpression>
</textField>
<textField>
<reportElement x="3" y="40" width="300" height="15"/>
<textFieldExpression><![CDATA[NumberFormat.getCurrencyInstance($P{REPORT_LOCALE}).format($P{value})]]></textFieldExpression>
</textField>
<textField>
<reportElement x="3" y="55" width="300" height="15"/>
<textFieldExpression><![CDATA[NumberFormat.getCurrencyInstance(new Locale("zh", "CN")).format($P{value})]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
Output result
The pdf file generated with help of JRPdfExporter looks like
The Yuan symbol is showing only for first group.
I am using this code for create report in MS Excel format:
JRXlsExporter exporter = new JRXlsExporter();
From there I don't know if it's possible to tell JasperReports to apply an auto filter on each column.
I was facing the same issue i.e in my spreadsheet(.xls) I wanted to add auto filter to all the columns(15), so I have added one property(Properties expression )name = net.sf.jasperreports.export.xls.auto.filter and value as 'Start' at first column header and same property with value as End at last column header and it worked.
Go to the 'Exporter Parameters' pane and make sure none of the 'Start' or 'Stop' options are set for Auto Filter. Choose the empty space in the related list box (see the attached image). Then run the report again. This time you should see the filters in the right places.
http://community.jaspersoft.com/jaspersoft-studio/issues/6876
JasperReports engine can is able to apply autofilters for columns for MS Excel output format. For applying autofilters we have to use net.sf.jasperreports.export.xls.auto.filter property. We should apply this property for header cells. It is possible to set filter for only one column or for the several columns.
Working example
Datasource
I used the csv datasource in this sample:
language,framework
Java,Guava
Java,Lombok
Java,JasperReports
Java,Spring
Java,Vaadin
C#,ASP.NET MVC
C#,NancyFX
C#,Automapper
JavaScript,AngularJS
JavaScript,React
JavaScript,jQuery
The name of data adapter for this datasource in the example below is frameworks.csv
The report's template
I used Jaspersoft Studio (JSS) for designing a report 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="excel_filter" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="b521f87a-96ed-43bd-80bd-c7b81ef2e8aa">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="frameworks.csv"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="language" class="java.lang.String"/>
<field name="framework" class="java.lang.String"/>
<columnHeader>
<band height="30" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="278" height="30" uuid="6b8ba60b-3df3-4c78-a780-79ee493ca5f8">
<property name="net.sf.jasperreports.export.xls.auto.filter" value="Start"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Language]]></text>
</staticText>
<staticText>
<reportElement x="278" y="0" width="277" height="30" uuid="731ba580-2168-49f6-b844-d02549d3d83c">
<property name="net.sf.jasperreports.export.xls.auto.filter" value="End"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Framework]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="30" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="278" height="30" uuid="7bf9acec-b13a-4261-91c1-0bf6f2c9c784"/>
<textFieldExpression><![CDATA[$F{language}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="278" y="0" width="277" height="30" uuid="d15bd48e-77ee-43df-b11f-4ec1a65811c0"/>
<textFieldExpression><![CDATA[$F{framework}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
I've used the Detail band for showing data. For the first column I set the Start value to the net.sf.jasperreports.export.xls.auto.filter property for staticText in Column Header band (will be the column header in Excel file) and the End value for the second (last) column.
In case using of setting property for only one column the only one filter will be created.
We can easy set property with help of context menu "XLS Tags -> Autofilter -> Start (End)" in JSS.
Output result
The generated output file in MS Excel format contains autofilters for both columns. The file was generated with help of JSS.
Notes:
More information about applying different features for exported Excel files can be found on vendor site
I have a Java class with two attribute id (integer) and name (string). I have created a list of objects and I want to use the JasperReports to make a pdf file.
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(conceptsList,true);
Map jasperParameters = new HashMap();
jasperParameters.put("Concepts", ds);
String input = "C:/report.jasper";
JasperPrint jp = JasperFillManager.fillReport(input, jasperParameters, new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jp, "C:/report.pdf");
In iReport Designer I have one parameter (Concepts) and set net.sf.jasperreports.engine.data.JRBeanCollectionDataSource as a parameter class. I also define two fields id and name and put them in the details band. but my pdf file does not show anything. I think somewhere I have to define datasource for id and name but I cannot find anything. It would be great if anyone can help me.
<?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="report22" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="313d6b25-bcea-43a0-80db-2c7733499ca2">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="Concepts" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="id" class="java.lang.Integer"/>
<field name="name" class="java.lang.String"/>
<title>
<band height="50" splitType="Stretch"/>
</title>
<detail>
<band height="200" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="b6c2dc20-ebaa-4dfe-8d42-b8318bf05d33"/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20" uuid="fbc83087-e178-4210-a9f0-a8e1d86a5f71"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
You can send the object data as a datasource to JRXML. Additionally, if the List should also be sent as a datasource to JRXML.
First design the iReport to accept Object as datasource:
1) Add the project "src" in class-path in iReport.
2) Specify the object package.className in JRXML report datasource.
3) Map the TextFields with the Object parameters.
Now in source code, use "JRBeanCollectionDataSource" Class for sending the List of Object in "JasperFillManager.fillReport()", for Connection.
Hope this helps.
I have written this code to show a simple report using iReport 4.7.1.
private void printReport() {
try {
JRTableModelDataSource dataSource = new JRTableModelDataSource(
new JTable().getModel());
String reportSource = "C:\\Reports\\report1.jrxml";
Map<String, Object> params = new HashMap<String, Object>();
params.put("Name", "SriLanka");
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
JasperViewer.viewReport(jasperPrint, false);
} catch (Exception e) {
e.printStackTrace();
}
}
Here are my libraries:
commons-beanutils-1.8.0.jar
commons-collections-2.1.1.jar
commons-digester-2.1.jar
commons-logging-1.1.1.jar
jasperrreports-4.7.1.jar
jasperreports-applet-4.7.1.jar
jasperreports-javaflow-4.7.1.jar
groovy-all-1.7.5.jar
and NetBeans output shows this warning
Oct 08, 2012 2:57:31 PM net.sf.jasperreports.engine.component.ComponentsEnvironment findBundles
WARNING: Found two components for namespace http://jasperreports.sourceforge.net/jasperreports/components
and here is the .jrxml file:
<?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" whenNoDataType="BlankPage" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5174594e-de3c-4b09-932b-c6665bf6a34b">
<parameter name="Name" class="java.lang.String" isForPrompting="false"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement uuid="fd02a8bc-548b-41c1-aed5-2a7c2e257704" x="210" y="23" width="157" height="36"/>
<textElement>
<font size="18"/>
</textElement>
<textFieldExpression><![CDATA[$P{Name}]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch"/>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
The report opens but doesn't show any text.
To solve this problem you should:
check your datasource, it can be empty;
add textFields elements with fields expressions (<textFieldExpression><![CDATA[$F{field}]]></textFieldExpression>) to the Detail band to show records in report.
Anyway your parameter Name should be displayed in the Title band (in generated report). You can pass even the JREmptyDataSource to check this.
For showing data you can chose one of this ways:
1. Using Detail band
It is classical, "old school" method for showing data stored in records from the data source. The JR engine iterates through the data source for rendering this section. With help of textField elements we can show the fields values.
The sample: look at $jasperreports$\demo\samples\jasper sample from JR distribution package.
2. Using Crosstab component
The Crosstab component used for summarizing data into grid which has two dimensions.
The sample: look at $jasperreports$\demo\samples\crosstabs sample.
3. Using List component
The List component iterates on set of its records (defined with help of subDataset property).
The sample: look at Using the Built-in List Component sample.
4. Using Table component
The Table is using for showing data with tabular structure. As a List component has its own subDataset.
The sample: look at Using the Built-in Table Component sample.
4. Using Subreport component
The Subreport is using for building complex reports such as compilation of different reports in one. This component often operates with subqueries.
The sample: look at Subreports sample.
You can read details in JasperReports Ultimate Guide
i have a EJB site with glassfish 3.1 + JSF for jasperreport 4.0.1. the site has no problem on streaming pdf, but it products blank PDF while printing PDF with runReportToPdfStream, below is the code snippet:
EJB
public class BookEJB {
public void printReport() throws ClassNotFoundException, IOException, JRException {
Map parameterMap = new HashMap();
FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
InputStream reportStream = ctx.getExternalContext().getResourceAsStream("/reports/test.jasper");
ServletOutputStream servletOutputStream = response.getOutputStream();
servletOutputStream.flush();
response.setContentType("application/pdf");
JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, parameterMap);
servletOutputStream.flush();
servletOutputStream.close();
ctx.responseComplete();
}}
test.jrxml - a simple report without SQL connection
<?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="test" pageWidth="800" pageHeight="1200" columnWidth="555" leftMargin="25" rightMargin="25" topMargin="30" bottomMargin="30">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[]]>
</queryString>
<pageHeader>
<band height="100">
<staticText>
<reportElement x="0" y="0" width="285" height="36"/>
<textElement>
<font size="24" isBold="true"/>
</textElement>
<text><![CDATA[Report of Testing]]></text>
</staticText>
</band>
</pageHeader>
<detail>
<band height="200">
<staticText>
<reportElement x="0" y="0" width="374" height="48"/>
<textElement>
<font size="18"/>
</textElement>
<text><![CDATA[If you don't see this, it didn't work blah blah blah.... ]]></text>
</staticText>
</band>
</detail>
<pageFooter>
<band height="100"/>
</pageFooter>
</jasperReport>
no error log in glassfish when generating this report on JSF, but only blank PDF has been shown. Please help, let me know if you need further info for the analysis.
Steven
After all, JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, parameterMap, new JREmptyDataSource()); solved the problem.
Quote from Sanda of Jasperreport:
By default, when no datasource info is present in a report, JR generates no pages. Another option (which can be set in the report's whenNoDataType attribute) would be to print all report sections, excepting the <detail>.
This report contains a detail section, but only with some static data. To ensure this section will be printed too, the simplest way is to provide an empty data source, containing a single empty record.
Source: https://community.jaspersoft.com/questions/537650/blank-pdf-even-simplest-jrxml
When you are not using the details band, just static values, you can do the following:
Right click in the iReport project, then select 'Properties', search for the property 'When no Data', and select 'All Sections, No Detail'
It works for me, using iReport 4.0
In addition to what Mythox said, I'll show the best way to fake a data source in jasper and if you need also on JasperServer.
1) Define an Empty Data Adapter (a simple .xml file), and deploy it in Server or put it in reports folder:
<?xml version="1.0" encoding="UTF-8" ?><emptyDataAdapter class="net.sf.jasperreports.data.empty.EmptyDataAdapterImpl"><name>Nuovo Data Adapter 1</name><recordCount>1</recordCount></emptyDataAdapter>
2) Link it into the main report:
<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="repor" language="javascript" pageWidth="612" pageHeight="792" whenNoDataType="NoDataSection" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isFloatColumnFooter="true" uuid="c0eee90e-1b1a-4f34-ad99-1112847752de">
<property name="net.sf.jasperreports.data.adapter" value="EmptyDataAdapter.xml"/>
prefix "repo:" to the value of the property for the data adapter, if the xml is deployed on jasper server.
The attribute "whenNoDataType" will be ignored.
Other details here.