Dynamic Jasper: Image not displayed after exporting as XLSX - java

I'm using JRXlsxExporterto export my JasperReport to XLSX format. To create the output stream, I'm opening up a blob stream and setting it to the SimpleOutputStreamExporterOutput. The blob is then saved to the DB after the exporter.exportReport() method is called.
OutputStreamExporterOutput simpleOutputStreamExporterOutput = new SimpleOutputStreamExporterOutput(blob.setBinaryStream(1));
The problem is, there is a jpg image part of the report content. When the report is exported to XLSX, 'The image cannot be displayed' message shows up in the xlsx in place of the image, but the rest of the data comes up fine.
I tried using a FileOutputStream instead of the blob stream and everything including the image comes up fine in the exported xlsx. But since I cannot be saving the exported report files on the application servers, and since I have to write to the DB as a blob, I would most definitely want to use the blob stream.
Jasper reports version - 6.2.0
Dynamic Jasper version - 5.0.4
I'm using the DynamicReportBuilder addFirstPageImageBanner() method to add the image.
Any help is deeply appreciated. Thank you!
Here's part of my jrxml file as requested.
<title>
<band height="30"/>
</title>
<pageHeader>
<band height="141">
<textField>
<reportElement x="0" y="78" width="290" height="78"/>
<textElement>
<font fontName="Times New Roman"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{reportInfo}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="58" width="290" height="20"/>
<textElement>
<font fontName="Times New Roman" size="14" isBold="true"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{reportType}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="38" width="290" height="20"/>
<textElement>
<font fontName="Times New Roman" size="14" isBold="true"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{reportTitle}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="0" width="290" height="38"/>
<textElement>
<font fontName="Times New Roman" size="18" isBold="true"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{title}]]></textFieldExpression>
</textField>
</band>
</pageHeader>
Here's the code to add the image though:
drb.addFirstPageImageBanner("imagepath" + "imagename", new Integer(197), new Integer(53), ImageBanner.ALIGN_LEFT);

You can display images in XLS by click
-> report properties
-> advanced
-> edit properties
and search for :
net.sf.jasperreports.export.xls.ignore.graphic
and mark it as false
it will fix the problem ..

Related

Jasper report blank page

My jrxml and java code below . I am trying to create report with some text and values . However blank report is generated . I am pasing map to fill the report as parameter . I have to add 4-5 static lines and then one dynamic variable . I have added these thing in detailed band .whats wrong
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Created with iReport - A designer for JasperReports -->
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport
name="Untitled_report_2"
columnCount="1"
printOrder="Vertical"
orientation="Portrait"
pageWidth="595"
pageHeight="842"
columnWidth="535"
columnSpacing="0"
leftMargin="30"
rightMargin="30"
topMargin="20"
bottomMargin="20"
whenNoDataType="NoPages"
isTitleNewPage="false"
isSummaryNewPage="false">
<property name="ireport.scriptlethandling" value="0" />
<property name="ireport.encoding" value="UTF-8" />
<import value="java.util.*" />
<import value="net.sf.jasperreports.engine.*" />
<import value="net.sf.jasperreports.engine.data.*" />
<field name="Field" class="java.lang.String"/>
<background>
<band height="0" isSplitAllowed="true" >
</band>
</background>
<title>
<band height="50" isSplitAllowed="true" >
</band>
</title>
<pageHeader>
<band height="50" isSplitAllowed="true" >
</band>
</pageHeader>
<columnHeader>
<band height="30" isSplitAllowed="true" >
</band>
</columnHeader>
<detail>
<band height="100" isSplitAllowed="true" >
<staticText>
<reportElement
x="20"
y="10"
width="180"
height="30"
key="staticText-1"/>
<box></box>
<textElement>
<font/>
</textElement>
<text><![CDATA[4-5 lines text]]></text>
</staticText>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="20"
y="40"
width="60"
height="20"
key="textField-1"/>
<box></box>
<textElement>
<font/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Field}]]></textFieldExpression>
</textField>
<staticText>
<reportElement
x="20"
y="70"
width="160"
height="30"
key="staticText-2"/>
<box></box>
<textElement>
<font/>
</textElement>
<text><![CDATA[4-5 lines text]]></text>
</staticText>
</band>
</detail>
<columnFooter>
<band height="30" isSplitAllowed="true" >
</band>
</columnFooter>
<pageFooter>
<band height="50" isSplitAllowed="true" >
</band>
</pageFooter>
<lastPageFooter>
<band height="50" isSplitAllowed="true" >
</band>
</lastPageFooter>
<summary>
<band height="50" isSplitAllowed="true" >
</band>
</summary>
</jasperReport>
Java code :
InputStream fileInput = getApplicationContext().getResource("/WEB-INF/reports/myjrxml.jrxml").getInputStream();
JasperReport jasperReport = JasperCompileManager.compileReport(fileInput);
HashMap map = new HashMap();
map.put("Field", "test");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map);
byte[] output1 = JasperExportManager.exportReportToPdf(jasperPrint);
String filename = "d:/test.pdf";
FileOutputStream fo = new FileOutputStream(filename);
fo.write(output1);
fo.close();
You are confusing the report data with parameters. I don't see that you have any report data, which is why you are getting an empty report. Further, you are filling a parameter named "Field" instead of the actual report data. So you either need to create a parameter called "Field" in your report, and then refer to it in the detail band, OR you need to pass the report data into your report something like
JRBeanCollectionDataSource data = new JRBeanCollectionDataSource(dataSet);
jasperPrint = JasperFillManager.fillReport(jasperReport, parameterMap, data);
where dataSet is a collection, such as List<?>. Within the List, you would have an object where one of the properties is Field.
I had also blank page (pdf). Even for the simplest report.Until I sent empty datasource (new JREmptyDataSource())!
This solved my problem:
Map<String, Object> param = new HashMap<>();
param.put("SOME_PARAM","something");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, new JREmptyDataSource());
Explanation here.
By default, when no datasource info is present in a report, JR generates no pages.

Getting "java.lang.NullPointerException at sun.font" during exporting report with string like "it's test<"

I'm working with Jaspersoft iReport Designer 4.0.2
Product Version: Jaspersoft iReport Designer 4.0.2
Java: 1.6.0_25; Java HotSpot(TM) Client VM 20.0-b11
I have a query in report which return this kind of result :
<div align="right"><font color="3366FF"><b><font size="3">it's test</font></b></font></div>
This is the code in JasperReports's report related to the textField: $F{TEXT}
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement positionType="Float" stretchType="RelativeToBandHeight" x="10" y="21" width="145" height="30"/>
<box>
<topPen lineWidth="0.1"/>
<leftPen lineWidth="1.0" lineColor="#000066"/>
<rightPen lineWidth="0.1"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle" markup="html">
<font fontName="Arial" size="8" pdfFontName="arial.ttf" pdfEncoding="Identity-H" isPdfEmbedded="true"/>
<paragraph lineSpacing="Proportional" rightIndent="10" spacingAfter="1"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{TEXT}]]></textFieldExpression>
</textField>
as you I used markup="html" in order to display the correct result in my pdf which will be generated , in my example I should display : it's test
bu when I run my example I have this error :
sun.font.StandardTextSource#1ddfca[start:0, len:5, cstart:0, clen:5, chars:"645 631 641 642 200b", level:1, flags:9, font:java.awt.Font[family=Arial,name=Arial,style=plain,size=8], frc:java.awt.font.FontRenderContext#2496298, cm:sun.font.CoreMetrics#40ebf000]
java.lang.NullPointerException
at sun.font.ExtendedTextSourceLabel.createCharinfo(ExtendedTextSourceLabel.java:608)
at sun.font.ExtendedTextSourceLabel.getCharinfo(ExtendedTextSourceLabel.java:492)
at sun.font.ExtendedTextSourceLabel.getLineBreakIndex(ExtendedTextSourceLabel.java:438)
at java.awt.font.TextMeasurer.calcLineBreak(TextMeasurer.java:308)
at java.awt.font.TextMeasurer.getLineBreakIndex(TextMeasurer.java:544)
at java.awt.font.LineBreakMeasurer.nextOffset(LineBreakMeasurer.java:340)
at java.awt.font.LineBreakMeasurer.nextLayout(LineBreakMeasurer.java:422)
at net.sf.jasperreports.engine.fill.TextMeasurer.renderNextLine(TextMeasurer.java:649)
at net.sf.jasperreports.engine.fill.TextMeasurer.renderParagraph(TextMeasurer.java:418)
at net.sf.jasperreports.engine.fill.TextMeasurer.measure(TextMeasurer.java:362)
at net.sf.jasperreports.engine.fill.JRFillTextElement.chopTextElement(JRFillTextElement.java:1151)
at net.sf.jasperreports.engine.fill.JRFillTextField.prepare(JRFillTextField.java:540)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:329)
at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:419)
at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:378)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2038)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:760)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:270)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:128)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:946)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:845)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:58)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:247)
at net.sf.jasperreports.engine.JasperRunManager.runReportToPdf(JasperRunManager.java:203)

generating jasper reports using ireport designer tool

I generated PDF report using ireport designer tool. Upto here it is fine.
My question is :
If there is no data in the database then it is showing blank pdf page. At this time how can i show "No data found for this request" in pdf?
There is a property of the template WhenNoDataType.
Just set it to WhenNoDataType: AllSectionsNoDetail.
This will have the template(static fields) but no dynamic data from database.
If you just want to print some message than you will have to change the design up a bit. You would need to place a condition at the beginning of your report to check if data is coming from database, and act accordingly.
If you want to show none of the content of your report when there is no data (no title, headers, footers, etc.) then you should add a noData band to your report and put the message there. Then change the whenNoDataType report parameter to NoDataSection.
From the JasperReports Ultimate Guide:
If the <noData> section is defined in the report template, and if the data source is empty, then the <noData> section will be the only one taken into account at fill time
Example:
<?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"
whenNoDataType="NoDataSection">
<pageHeader>
<band height="20">
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<text><![CDATA[Page Header]]></text>
</staticText>
</band>
</pageHeader>
<detail>
<band height="20">
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<text><![CDATA[Detail]]></text>
</staticText>
</band>
</detail>
<pageFooter>
<band height="20">
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<text><![CDATA[Page Footer]]></text>
</staticText>
</band>
</pageFooter>
<noData>
<band height="20">
<staticText>
<reportElement x="0" y="0" width="555" height="20"/>
<text><![CDATA[No data found for this request]]></text>
</staticText>
</band>
</noData>
</jasperReport>

Calling Java API in the iReport expression

I'm trying to attach a call to a Java library into the iReport expression.
I tried this with a very simple library returning a hello world string.
package utils;
public class Hello {
public static String hello()
{
return "Hello";
}
}
Within iReport, i want to use this API. I compiled the above library into a jar file. Added the location in Tools -> Options -> Classpath.
then tried the following:
Editing the expression in a a text field new utils.Hello().hello()
Creating a new field and setting its type to utils.Hello. Then using the field.hello() in the expression
In both cases, it complains that it cannot resolve hello. However its in the classpath. I've also tried to right click on the report root and add utils.Hello/utils to the Java import directive. Neither of which seemed to pick up the class.
Any advice is greatly appreciated.
Your right expression might be like this:
<textFieldExpression><![CDATA[utils.Hello.hello()]]></textFieldExpression>
The working sample:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ..>
<queryString>
<![CDATA[SELECT DISTINCT city FROM address ORDER BY city]]>
</queryString>
<field name="CITY" class="java.lang.String"/>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{CITY}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[utils.Hello.hello()]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
You can also add import instruction to the report. In this case the expression will be:
<textFieldExpression><![CDATA[Hello.hello()]]></textFieldExpression>
The working sample:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ... whenNoDataType="AllSectionsNoDetail">
<import value="utils.Hello"/>
<title>
<band height="41">
<textField>
<reportElement x="188" y="11" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[Hello.hello()]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
Note: For both samples the jar file (with utils.Hello class) must be in classpath.
More info about using srciptlets you can find here.
You field type should be String , not utils.Hello

stretch a row to fit the data in jasper reports using iReport

I use iReport 2.0.5 to generate jrxml file and use the same in my java web propject, I have used all the flags to stretch to fit the data, but it doesn't work
The example:
<textField isStretchWithOverflow="true" isBlankWhenNull="true" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement style="dNew" mode="Opaque" x="55" y="0" width="55" height="19" key="value-1" stretchType="RelativeToTallestObject" positionType="Float" isPrintWhenDetailOverflows="true"/>
<box></box>
<textElement>
<font/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{value1}]]></textFieldExpression>
</textField>
It should be able to stretch down based on the band.
If it is a TextField you can set the option "Stretch with Overflow" AFAIK straight StaticTexts will not overflow.
Add the following two properties in your reportElement tag.
name="net.sf.jasperreports.print.keep.full.text" value="true"
name="net.sf.jasperreports.export.xls.wrap.text" value="false"

Categories

Resources