I have a Template like this:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.4.3.final using JasperReports Library version 6.4.3 -->
<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="Invoice" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4eedbb89-b4f6-4469-9ab6-f642a1688cf7">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="localdb"/>
<style name="Title" forecolor="#FFFFFF" fontName="Times New Roman" fontSize="50" isBold="false" pdfFontName="Times-Bold"/>
<style name="SubTitle" forecolor="#CCCCCC" fontName="Times New Roman" fontSize="18" isBold="false" pdfFontName="Times-Roman"/>
<style name="Column header" forecolor="#666666" fontName="Times New Roman" fontSize="14" isBold="true"/>
<style name="Detail" mode="Transparent" fontName="Times New Roman"/>
<style name="Row" mode="Transparent" fontName="Times New Roman" pdfFontName="Times-Roman">
<conditionalStyle>
<conditionExpression><![CDATA[$V{REPORT_COUNT}%2 == 0]]></conditionExpression>
<style mode="Opaque" backcolor="#EEEFF0"/>
</conditionalStyle>
</style>
.....
<queryString language="SQL">
<![CDATA[SELECT * from test]]>
</queryString>
And this code for generating:
#Autowired
private DataSource datasource;
#RequestMapping(value = "invoice", produces = MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity createInvoice() throws JRException, SQLException, IOException {
InputStream resourceAsStream = PDFReporter.class.getResourceAsStream(BILL_JRXML);
assert resourceAsStream != null : "No " + BILL_JRXML + " found!";
JasperReport r = JasperCompileManager.compileReport(resourceAsStream);
Map<String, Object> parameters = new HashMap();
Connection connection = datasource.getConnection();
JasperPrint print = JasperFillManager.fillReport(r, parameters, connection);
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(print));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(bos));
exporter.exportReport();
connection.close();
return new ResponseEntity(bos.toByteArray(), HttpStatus.OK);
}
But the result is a PDF having only one page that is blank.
I download the PDF and check for errors but the PDF having the lead-in %PDF-1.4 and the lead-out %%EOF.
If I do it in Jaspersoft Studio it works good.
What is wrong?
Oh, the table test is empty. I never expected to have a empty page!
Related
I have to upgrade jasperreport-4.0.2 to jasperreport-6.5.1 I found some differences regarding export reports to excel format as
net.sf.jasperreports.engine.export.JExcelApiExporter
Is deprecated and replaced by
net.sf.jasperreports.engine.export.JRXlsExporter
And many others things are also changed.
As in jasperreport-4.0.2 I can change excel sheet names as by this code
JRAbstractExporter exporter = new net.sf.jasperreports.engine.export.JExcelApiExporter();
exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
What is alternate for naming excel sheets in jasperreport-6.5.1 is?
In accordance with the new API (comparing with 4.x version) you can solve the task in several ways.
Using ReportExportConfiguration
We can set sheet name using SimpleXlsxReportConfiguration, in thit case we should override getSheetNames() method.
Java code:
Map<String, Object> params = new HashMap<>();
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params);
JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setConfiguration(new SimpleXlsxReportConfiguration() {
#Override
public String[] getSheetNames() {
return new String[]{"MyName"};
}
});
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleOutputStreamExporterOutput exporterOutput = null;
try (OutputStream outputStream = new FileOutputStream(file)) {
exporterOutput = new SimpleOutputStreamExporterOutput(outputStream);
exporter.setExporterOutput(exporterOutput);
exporter.exportReport();
} finally {
if (exporterOutput != null) {
exporterOutput.close();
}
}
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="Set sheet name via exporter's configuration" pageWidth="595" pageHeight="200" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<title>
<band height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30"/>
<text><![CDATA[Set sheet name via exporter's configuration]]></text>
</staticText>
</band>
</title>
</jasperReport>
The generated result will be:
Using report's parameter and net.sf.jasperreports.export.xls.sheet.name property
The sheet name can be set with help of net.sf.jasperreports.export.xls.sheet.name property. In case we want set this name dynamically we can use report's parameter for passing the name.
Java code:
Map<String, Object> params = new HashMap<>();
params.put("stringParam", "ZZZ");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params);
JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setConfiguration(new SimpleXlsxReportConfiguration());
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleOutputStreamExporterOutput exporterOutput = null;
try (OutputStream outputStream = new FileOutputStream(file)) {
exporterOutput = new SimpleOutputStreamExporterOutput(outputStream);
exporter.setExporterOutput(exporterOutput);
exporter.exportReport();
} finally {
if (exporterOutput != null) {
exporterOutput.close();
}
}
The report's 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="Set sheet name via parameter" pageWidth="595" pageHeight="200" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<parameter name="name" class="java.lang.String"/>
<title>
<band height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30">
<propertyExpression name="net.sf.jasperreports.export.xls.sheet.name"><![CDATA[$P{name}]]></propertyExpression>
</reportElement>
<text><![CDATA[Set sheet name via parameter]]></text>
</staticText>
</band>
</title>
</jasperReport>
The result will be:
More information about using xls/xlsx exporters:
Advanced Excel Features
I am trying to convert an encoded tag in an xml to image and then convert into PDF using Java and Jaspersoft Studio.
NOTE : When I am previewing in Jasper studio I am getting proper display image but getting the above error while running it using Java main method.
Motive of the code is to fetch the tag from xml and convert it to pdf with image.
I am using JasperReports 6.4.0. While previewing from Jaspersoft Studio image is getting displayed properly and jrxml file is getting compiled too. But while running using main method in Java, I am getting error.
Below is the jrxml file I am using.
<?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="ImageDS" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<queryString language="xPath">
<![CDATA[/Data/ListOfIdeaCafEntryFormIio/IdeaCafEntryFormBc]]>
</queryString>
<field name="Photo" class="java.lang.String">
<fieldDescription><![CDATA[Photo]]></fieldDescription>
</field>
<group name="Photo">
<groupExpression><![CDATA[$F{Photo}]]></groupExpression>
</group>
<columnHeader>
<band height="61" splitType="Stretch">
<staticText>
<reportElement x="206" y="0" width="100" height="30"/>
<text><![CDATA[Photo]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<image>
<reportElement x="205" y="52" width="50" height="50"/>
<imageExpression><![CDATA[new java.io.ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64($F{Photo}.getBytes()))]]></imageExpression>
</image>
</band>
</detail>
</jasperReport>
Java Code used to generate PDF :
public class Java8Base64Image {
public static void main(String[] args) {
BaseDao dao = new BaseDao();
Connection connection = dao.getConnection();
try {
String reportName = "C:/..../MyReports/ImageDS";
Map<String, Object> parameters = new HashMap<String, Object>();
JasperCompileManager.compileReportToFile(reportName + ".jrxml");
JasperPrint print = JasperFillManager.fillReport(reportName + ".jasper", parameters, connection);
// exports report to pdf
JRExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, new FileOutputStream(reportName + ".pdf"));
exporter.exportReport();
} catch (Exception e) {
throw new RuntimeException("It's not possible to generate the pdf report.", e);
} finally {
if (connection != null) {
try { connection.close(); } catch (Exception e) {}
}
}
}
}
XML with image:
<Photo>/9j/..../OiiiQon/9k=</Photo>
The error I got
Error is : Caused by: net.sf.jasperreports.engine.JRException:
Errors were encountered when compiling report expressions class file:
1. Cannot cast from ByteArrayInputStream to String value = (java.lang.String)(new java.io.ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64(((java.lang.String)field_Photo.getValue()).getBytes()))); //$JR_EXPR_ID=11$
I need to design a report to represent a pojo structure like the following:
class Obj{
field1;
field2;
List<Obj> substitutions;
}
My report will have the same field design, used one time in detail 1 band and then n-times in detail 2 with a subreport.
How can I reuse the same field design in both detail 1 and 2?
I have tryed to put a subreport in detail1 and a subreport in detail 2 pointing the same jrxml file. While the second report is ok because is filled by a collection in back object of report, I don't know how to pass current report datasource to first subreport.
Does someone have any ideas?
Here an example on the result report.
I have a pojo class named purchase.
public class Purchase {
private String name;
private String remark;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getRemark() {
return remark;
}
}
Main class to generate pdf file.
public class JasperReportIntro {
public static void main(String[] args) {
JasperReport jasperReport;
JasperPrint jasperPrint;
ArrayList<Purchase> list1 = new ArrayList<Purchase>();
ArrayList<Purchase> list2 = new ArrayList<Purchase>();
for(int i=0;i<20;i++) {
Purchase purchase = new Purchase();
purchase.setName("Vivek" + i);
purchase.setRemark("This is remark" + i);
purchase.setDiscount(10.0);
purchase.setId(i);
list1.add(purchase);
}
for(int i=0;i<20;i++) {
Purchase purchase = new Purchase();
purchase.setName("yadav" + i);
purchase.setRemark("This is remark" + i);
purchase.setDiscount(10.0);
purchase.setId(i);
list2.add(purchase);
}
try {
jasperReport = JasperCompileManager.compileReport("path/to/report.jrxml");
Map<String, List> result = new HashMap<String, List>();
JRDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(list1);
Map<String, Object> map = new HashMap<String, Object>();
map.put("dataForSubreport1",list1);
map.put("dataForSubreport2",list2);
jasperPrint = JasperFillManager.fillReport(jasperReport,map, beanCollectionDataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint,
"path/to/simple_report.pdf");
System.out.println("Completed");
} catch (JRException e) {
e.printStackTrace();
}
}
}
Main report class report.jrxml.
<?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" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4ddfa540-9b62-4339-9334-18733f7469cc">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["/home/jft/code/jasper-report/Genratedreports/"]]></defaultValueExpression>
</parameter>
<parameter name="dataForSubreport1" class="java.util.List"/>
<parameter name="dataForSubreport2" class="java.util.List"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="100">
<subreport>
<reportElement x="0" y="20" width="555" height="40" uuid="8798ed7b-f389-4037-9381-3862d2f3e43a"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{dataForSubreport1})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "sub1.jasper"]]></subreportExpression>
</subreport>
<subreport>
<reportElement positionType="Float" x="0" y="60" width="555" height="40" uuid="f99fdb31-afc3-4127-8148-8e0426cd71a6"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{dataForSubreport2})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "sub1.jasper"]]></subreportExpression>
</subreport>
</band>
</title>
</jasperReport>
Here is subreport sub1.jrxml.
<?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="sub1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="48bc0742-6980-42a7-9a52-6f125fb83bc2">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<field name="name" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="ac3c1a86-6d0d-4b63-b339-744528573666"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Download file using main class of jasperreportintro. I have passed two parameter dataForSubreport1 and dataForSubreport2 using map. and send both parameter in subreport. It use same subreport with different -2 value. Hope it will help you.
Enjoy.
My requirement is to Create a PDF with some text in Hindi/Marathi/Other Indian Language while some text in English.
Text in Hindi/Marathi/Other Indian Language will be static text in Unicode (UTF-8) while text in English will be static and dynamic. I created a jrxml file in iReport 5.5.0. In iReport Internal Viewer, it shows proper output. However while exporting in pdf, it doesn't show the Hindi Text at all. It only shows English Text. While exporting in doc, the Hindi Text is coming as squares which I feel is word setting issue. In HTML or RTF export, it works perfectly. The same thing is happening when I create pdf/doc/html/rtf using Java Code. I am attaching Java and jrxml file over here for reference.
public class TestHindiPDF {
public static void main(String[] args) throws Exception {
String strJasperFilePath = null;
String strJRXMLFilePath = null;
String strReportPath = "D:\\Chintan\\Tech\\Input_Files\\TestMulti";
String strSaveReportPath = "D:\\Chintan\\Tech\\Input_Files\\TestMulti";
String strReportName = "multi1";
strJasperFilePath = strReportPath + "/multi1.jasper";
strJRXMLFilePath = strReportPath + "/multi1.jrxml";
strReportName = "multi1";
File reportFile = new File(strJasperFilePath);//your report_name.jasper file
if (!reportFile.exists()) {
try {
System.out.println("Compiling JRXML File : " + strJRXMLFilePath);
JasperCompileManager.compileReportToFile(strJRXMLFilePath, strJasperFilePath);
reportFile = new File(strJasperFilePath);
} catch (JRException e) {
e.printStackTrace();
}
}
if (reportFile != null) {
OutputStream fileOut = null;
try {
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, new JREmptyDataSource());
JRExporter exporter2 = new JRRtfExporter();
exporter2.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter2.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter2.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, strSaveReportPath + "/" + strReportName + ".rtf");
exporter2.exportReport();
JRExporter exporter3 = new JRHtmlExporter();
exporter3.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter3.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter3.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, strSaveReportPath + "/" + strReportName + ".html");
exporter3.exportReport();
JRExporter exporter4 = new JRDocxExporter();
exporter4.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter4.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter4.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, strSaveReportPath + "/" + strReportName + ".docx");
exporter4.exportReport();
JRExporter exporter1 = new JRPdfExporter();
exporter1.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter1.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter1.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, strSaveReportPath + "/" + strReportName + ".pdf");
exporter1.exportReport();
JasperExportManager.exportReportToPdfStream(jasperPrint, fileOut);
} catch (JRException e) {
e.printStackTrace();
} finally {
if (fileOut != null) {
fileOut.close();
}
}
}
}
}
The JRXML File is :
<?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="multi1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c8a053f6-858e-427b-aec1-89231aadddd6">
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement x="104" y="0" width="313" height="25" uuid="fa5e3e6e-26f7-4973-a2d5-3c3b7a5e8227"/>
<textElement textAlignment="Center">
<font size="16"/>
</textElement>
<text><![CDATA[अतिदेय राशि के पुनर्भुगतान हेतु अनुस्मारक]]></text>
</staticText>
<staticText>
<reportElement x="132" y="25" width="252" height="25" uuid="fa5e3e6e-26f7-4973-a2d5-3c3b7a5e8227"/>
<textElement textAlignment="Center">
<font size="16"/>
</textElement>
<text><![CDATA[Reminder for repaying Overdue Amount]]></text>
</staticText>
</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>
I'm just doing a test, i want to print some static text yet it doesn't save pdf.
<?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="FirstReport">
<detail>
<band height="20">
<staticText>
<reportElement x="20" y="0" width="200" height="20" />
<text>
<![CDATA[If you don't see this, it didn't work]]>
</text>
</staticText>
</band>
</detail>
</jasperReport>
The code is:
public class PDF {
public void genereazaRaport() {
try {
InputStream input = new FileInputStream(new File("jrxml/test.jrxml"));
JasperDesign jasperDesign = JRXmlLoader.load(input);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
//Map<String, Object> parameters = new HashMap<String, Object>();
//parameters.put("Titlu Raport", "PDF JasperReport");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, new JREmptyDataSource());
OutputStream out = new FileOutputStream(new File("raport/raport.pdf"));
JasperExportManager.exportReportToPdfStream(jasperPrint, out);
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (JRException e) {
System.out.println("JRException");
}
}
public static void main(String[] args) {
new PDF().genereazaRaport();
}
}
And I get a blank page when it save in file. I think it's something that has to do with the datasource but i can't figure what.
Try this jrxml. It will work for you.
<?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="FirstReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30">
<detail>
<band height="20">
<staticText>
<reportElement x="20" y="0" width="200" height="20"/>
<textElement/>
<text><![CDATA[If you don't see this, it didn't work
]]></text>
</staticText>
</band>
</detail>
</jasperReport>
Enjoy...