Image expression URL in Jasper Reports - jasper-reports

I would like to embed an image into my JasperReports jrxml file. I have this directory structure.
Tomcat Root
webapps
reports
-->images
--> company_logo.jpg
-->reports
-->sample.jasper
-->WEB-INF
-->classes
And I tried doing this thinking that this is relative to my reports context root
<image>
<reportElement x="0" y="0" width="104" height="40"/>
<imageExpression class="java.lang.String">
<![CDATA["images/company_logo.jpg"]]>
</imageExpression>
</image>
..and this also
<image>
<reportElement x="0" y="0" width="104" height="40"/>
<imageExpression class="java.lang.String">
<![CDATA["/images/company_logo.jpg"]]>
</imageExpression>
</image>
but it always says there was an error loading bytes from location JRLoader.
I tried doing this and it works, but I am a bit confused why my first two attempt doesn't.
Is this really how you embed images in JasperReports? Do you need to supply the whole path? I am thinking that there should be a page relative
something.
<image>
<reportElement x="0" y="0" width="104" height="40"/>
<imageExpression class="java.lang.String">
<![CDATA["http://localhost:8080/reports/images/company_logo.jpg"]]>
</imageExpression>
</image>
I am a bit puzzled about the correct way.

<![CDATA["../images/company_logo.jpg"]]> should do the trick. The path is relative to your .jasper file.

You need to create a PARAMETER of type String called for example CONTEXT, and from your JSP send the servlet context:
parameters.put("CONTEXT",this.getServletContext().getRealPath("/"));
Now, in your report you use the parameter:
$P{CONTEXT}.toString()+"reports/images/logo.png"
The same apply for sub reports or other web resources, example:
$P{CONTEXT}.toString()+"reports/OrdenCompraAlmacen_Items.jasper"
I hope it is useful.

With .jrxml when use an absolute path with image, when the packaged jar filr will be deployed you will get:
java.lang.IllegalArgumentException: name
at sun.misc.URLClassPath$Loader.findResource(Unknown Source) ~[na:1.8.0_121]
at sun.misc.URLClassPath.findResource(Unknown Source) ~[na:1.8.0_121]
at java.net.URLClassLoader$2.run(Unknown Source) ~[na:1.8.0_121]
at java.net.URLClassLoader$2.run(Unknown Source) ~[na:1.8.0_121]
Try to load resource as:
<imageExpression><![CDATA[this.getClass().getResourceAsStream("/img/mdg_logo.jpg")]]></imageExpression>

sometimes is better use File.separtor: "ima" + File.separator + "logo.jpg"

Use the below expression in the image Expression of jasper report IDE on image properties of the jrxml and the relative image path
Accordingly change w.r.t path
getClass().getResource("META-INF/resources/webjars/Bank/themes/default/images/Logo.png").openStream()

This way the path is relative to your current working directory of your project. "." means current working directory.

Put your picture in the following structure:
webapps
reports
-->reports
-->sample.jasper
-->WEB-INF
-->classes
-->images
--> company_logo.jpg
<imageExpression class="java.lang.String">
<![CDATA["company_logo.jpg"]]>
</imageExpression>

Related

Error filling reportResource not found at: :Branch_Summary_SubReport1.jrxml

I have main report and calling two sub-report. In Jasper Studio it is working, when we deploy in server below error am getting.
ERROR : "Error filling reportResource not found at: Branch_Summary_SubReport1.jrxml."
<subreport overflowType="Stretch">
<reportElement mode="Opaque" x="0" y="206" width="180" height="134" backcolor="#FFFFFF" uuid="f2b1f959-0351-4fa6-967a-edf545f59a33"/>
<subreportParameter name="REPORT_CONNECTION">
<subreportParameterExpression><![CDATA[$P{REPORT_CONNECTION}]]></subreportParameterExpression>
</subreportParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA["Branch_Summary_SubReport1.jrxml"]]></subreportExpression>
</subreport>
<subreport>
<reportElement mode="Opaque" x="0" y="361" width="180" height="269" uuid="1efdacc7-8f4f-4e2d-b1b2-3ad3bd778dc0"/>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA["Branch_Summary_SubReport2.jrxml"]]></subreportExpression>
</subreport>
<subreport>
jrxml file is a report template file defined in an xml file format. it has .jrxml file name extension. this file (jrxml) cannot be used directly to generate the report. It has to be compiled to JasperReport file. JasperReport file is a binary format file with .jasper file name extension.
in your report calling two sub reports. but these files are jrxml files. hence call the compiled jasper files of these sub reports,
<subreportExpression><![CDATA["Branch_Summary_SubReport1.jasper"]]></subreportExpression>
<subreportExpression><![CDATA["Branch_Summary_SubReport2.jasper"]]></subreportExpression>
Note: don't forget to deploy the compiled sub reports (jasper files) into correct path in the server

How can we use relative path for an image [duplicate]

I would like to embed an image into my JasperReports jrxml file. I have this directory structure.
Tomcat Root
webapps
reports
-->images
--> company_logo.jpg
-->reports
-->sample.jasper
-->WEB-INF
-->classes
And I tried doing this thinking that this is relative to my reports context root
<image>
<reportElement x="0" y="0" width="104" height="40"/>
<imageExpression class="java.lang.String">
<![CDATA["images/company_logo.jpg"]]>
</imageExpression>
</image>
..and this also
<image>
<reportElement x="0" y="0" width="104" height="40"/>
<imageExpression class="java.lang.String">
<![CDATA["/images/company_logo.jpg"]]>
</imageExpression>
</image>
but it always says there was an error loading bytes from location JRLoader.
I tried doing this and it works, but I am a bit confused why my first two attempt doesn't.
Is this really how you embed images in JasperReports? Do you need to supply the whole path? I am thinking that there should be a page relative
something.
<image>
<reportElement x="0" y="0" width="104" height="40"/>
<imageExpression class="java.lang.String">
<![CDATA["http://localhost:8080/reports/images/company_logo.jpg"]]>
</imageExpression>
</image>
I am a bit puzzled about the correct way.
<![CDATA["../images/company_logo.jpg"]]> should do the trick. The path is relative to your .jasper file.
You need to create a PARAMETER of type String called for example CONTEXT, and from your JSP send the servlet context:
parameters.put("CONTEXT",this.getServletContext().getRealPath("/"));
Now, in your report you use the parameter:
$P{CONTEXT}.toString()+"reports/images/logo.png"
The same apply for sub reports or other web resources, example:
$P{CONTEXT}.toString()+"reports/OrdenCompraAlmacen_Items.jasper"
I hope it is useful.
With .jrxml when use an absolute path with image, when the packaged jar filr will be deployed you will get:
java.lang.IllegalArgumentException: name
at sun.misc.URLClassPath$Loader.findResource(Unknown Source) ~[na:1.8.0_121]
at sun.misc.URLClassPath.findResource(Unknown Source) ~[na:1.8.0_121]
at java.net.URLClassLoader$2.run(Unknown Source) ~[na:1.8.0_121]
at java.net.URLClassLoader$2.run(Unknown Source) ~[na:1.8.0_121]
Try to load resource as:
<imageExpression><![CDATA[this.getClass().getResourceAsStream("/img/mdg_logo.jpg")]]></imageExpression>
sometimes is better use File.separtor: "ima" + File.separator + "logo.jpg"
Use the below expression in the image Expression of jasper report IDE on image properties of the jrxml and the relative image path
Accordingly change w.r.t path
getClass().getResource("META-INF/resources/webjars/Bank/themes/default/images/Logo.png").openStream()
This way the path is relative to your current working directory of your project. "." means current working directory.
Put your picture in the following structure:
webapps
reports
-->reports
-->sample.jasper
-->WEB-INF
-->classes
-->images
--> company_logo.jpg
<imageExpression class="java.lang.String">
<![CDATA["company_logo.jpg"]]>
</imageExpression>

How to run report on ireport which is running of Jasper Server with image paths?

I am using Jasper Server version 6.0.1 and iReport version 5.6.0. I have an image on report it has path(Image Expression) according to server. When I run the report on iReport I does not find the path because it is path according to server. One option is I have to change path of every image according to local machine. If I use this option I have to change the path again before uploading to server. Is there any other option too?
Yes, you can pass the image path in parameter,
<parameter name="LOGO" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["/somePath/virgin.png"]]></defaultValueExpression>
</parameter>
...
<image>
<reportElement uuid="8a12495e-e2a9-4d0c-9e78-650a5c084fcd" x="0" y="1" width="100" height="66"/>
<imageExpression><![CDATA[$P{LOGO}]]></imageExpression>
</image>
When you pass the image path from a java file that path will be used for loading the image. If you are not passing any value then default path will be used. Hope this helps.
UPDATE:
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("LOGO", imageDirectory + "/myLogo.jpg");
JasperReport jasperReport = CompileManager.compileReport(jrxmlFilePath, mainReportName);
JasperPrint jasperPrint =
JasperFillManager.fillReport(jasperReport, parameters, null);
Have a look at here.

http://jasperreports.sourceforge.net/xsd/htmlcomponent.xsd can not be read

I inserted a HTML element into my japserreporttest.jrxml and the code generated automatically is like this:
<componentElement>
<reportElement x="466" y="579" width="100" height="30" uuid="4d62f8bd-23d4-4199-97bb-a703a0769a8c"/>
<hc:html xmlns:hc="http://jasperreports.sourceforge.net/htmlcomponent" xsi:schemaLocation="http://jasperreports.sourceforge.net/htmlcomponent http://jasperreports.sourceforge.net/xsd/htmlcomponent.xsd" scaleType="RetainShape" horizontalAlign="Left" verticalAlign="Middle">
<hc:htmlContentExpression><![CDATA["<p style='background-color:yellow;'>HTML paragraph</p>"]]></hc:htmlContentExpression>
</hc:html>
</componentElement>
But when I tried to generate the report I get the following error "http://jasperreports.sourceforge.net/xsd/htmlcomponent.xsd cannot be read". I input this URL in browser and get the 404 error. Do you know what happened with source forge?
To solve this, you should add the library jasperreports-htmlcomponent.
If you are using maven you add :
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-htmlcomponent</artifactId>
<version>5.0.1</version>
</dependency>
Else this is a link to the jar : http://maven.sonner.com.br/~maven2/net/sf/jasperreports/jasperreports-htmlcomponent/5.0.1/

WiX wxs script with option to install on Selecting current-user or all-users install

I need a complete WiX script that can be build.It had
Welcome
EULA
Install Folder
Selecting current-user or all-users install
Install
Finish
I just need a single wxs file because it is for a simple application. Also I need an option to install it for the current user or all users.
have you checked the WixUI_Advanced UI that comes built-in in Wix3?
Here are the dialogs that it has:
AdvancedWelcomeEulaDlg
BrowseDlg
DiskCostDlg
FeaturesDlg
InstallDirDlg
InstallScopeDlg (for selecting user or per machine)
InvalidDirDlg
I've also used the WixUI_InstallDir, but copied it and tweaked it according to my needs by adding some "What to do next" text at the end of the installer. I used this detailed walkthrough to take the stock Wix GUI and change a couple of the screen for my own purposes. This does require that you grab the Wix source code, but just for the purpose of getting the uncompiled versions of the actual Product.wxs file that the Wix developers include to drive the WixUI_InstallDir installer.
So basically I have something like this in a solution (using Votive add-in for VS):
MyWeb project
Wix Project
MyWeb.wxs - my product stuff obviously
Product.wxs - the Wix file that comes from the Wix source that defines the structure, flow, and content of the WixUI_InstallDir
My_InstallDir.wxs - This is where the fun happens. Essentially copied the source code Wix file, changed it to satisfy my needs, then made sure that in the Product.wxs I have a reference to my screen, not the original one.
Product.wxs
<UI>
<UIRef Id="My_InstallDir"/>
</UI>
<!-- Add the customized EULA -->
<WixVariable Id="WixUILicenseRtf" Value="$(var.SolutionDir)\doc\license.rtf" />
<Property Id="WIXUI_INSTALLDIR" Value="INSTALLLOCATION" />
<UIRef Id="My_InstallDir" />
<!-- Add the customized banner logo -->
<WixVariable Id="WixUIBannerBmp" Value="$(var.SolutionDir)\doc\InstallerBanner.bmp" />
<WixVariable Id="WixUIDialogBmp" Value="$(var.SolutionDir)\doc\InstallerSidebar.bmp" />
My_InstallDir.wxs is totally unchanged, apart from the line referring to my exit dialog, which is where my "What to do next" notes are:
<Publish Dialog="MyExitDialog" Control="Finish" Event="EndDialog" Value="Return" Order="999">1</Publish>
So then the real change is in the My_ExitDialog where I'm displaying the text:
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<UI>
<Dialog Id="MyExitDialog" Width="370" Height="270" Title="!(loc.ExitDialog_Title) test">
<Control Id="Finish" Type="PushButton" X="236" Y="243" Width="56" Height="17" Default="yes" Cancel="yes" Text="!(loc.WixUIFinish)" />
<Control Id="Cancel" Type="PushButton" X="304" Y="243" Width="56" Height="17" Disabled="yes" Text="!(loc.WixUICancel)" />
<Control Id="Bitmap" Type="Bitmap" X="0" Y="0" Width="370" Height="234" TabSkip="no" Text="!(loc.ExitDialogBitmap)" />
<Control Id="Back" Type="PushButton" X="180" Y="243" Width="56" Height="17" Disabled="yes" Text="!(loc.WixUIBack)" />
<Control Id="BottomLine" Type="Line" X="0" Y="234" Width="370" Height="0" />
<!--<Control Id="Description" Type="Text" X="135" Y="70" Width="220" Height="40" Transparent="yes" NoPrefix="yes" Text="!(loc.ExitDialogDescription)" />-->
<Control Id="Title" Type="Text" X="135" Y="20" Width="220" Height="60" Transparent="yes" NoPrefix="yes" Text="!(loc.ExitDialogTitle)" />
<Control Id="NextSteps" Type="ScrollableText" X="135" Y="70" Width="220" Height="140" Sunken="yes" TabSkip="no">
<Text SourceFile="$(var.SolutionDir)\doc\GemWebAfterInstall.rtf" />
</Control>
</Dialog>
<InstallUISequence>
<Show Dialog="MyExitDialog" OnExit="success" />
</InstallUISequence>
<AdminUISequence>
<Show Dialog="MyExitDialog" OnExit="success" />
</AdminUISequence>
</UI>
</Fragment>
I know you're looking for a solution, not pointers necessarily. However, using the article I reference as the key starting point, I think you can find all of the parts you need either in the ready-baked WixUI bits, or by replacing small pieces of the "out-of-the-box" stuff like I have. Good luck.