java


JasperReport image inside report jrxml on HTML src is missing?


I have a method in Java using the JasperReport to export a report in HTML like this:
public static byte[] exportToHtmlWithConn(String urlReport, Connection conn) throws JRException {
Map<String, Object> parameters = new HashMap<>();
String urlLogo = "/reports/image.PNG";
String urlLogo2 = JasperUtilities.class.getResource(urlLogo).toString();
parameters.put("CONTEXT", urlLogo2);
JasperReport report = JasperCompileManager.compileReport(JasperUtilities.class.getResourceAsStream(urlReport));
jasperPrint = JasperFillManager.fillReport(report, parameters, conn);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
HtmlExporter exporter = new HtmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleHtmlExporterOutput(baos));
exporter.exportReport();
return baos.toByteArray();
}
In the report I have one parameter like this:
<parameter name="CONTEXT" class="java.lang.String">
<parameterDescription><![CDATA[CONTEXT]]></parameterDescription>
<defaultValueExpression><![CDATA[0]]></defaultValueExpression>
</parameter>
And the idea is to show one image inside the report
<image>
<reportElement x="180" y="-5" width="185" height="80" uuid="d8978cb8-9c4e-4d1b-83fb-a83356803128">
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<imageExpression><![CDATA[$P{CONTEXT}.toString()]]></imageExpression>
</image>
The problem is when I use this method to export a HTML the image is missing but when I use the same structure for example to export a PDF or xlsx the image works Ok.
Why not work in HTML? When I check the code in html the src is empty
<img src="" style="width: 185px" alt=""/>
Can someone help with any idea about this? I have seen many ways to do the same but this has been impossible to work. I also have used one parameters like java.io.InputStream and the image works in PDF and others formats. But in HTML doesn't work.
Also I have used others images but the same problem.
Images in HTML exports, usually require a handler that deals with them. The handler is an implementation of net.sf.jasperreports.engine.export.HtmlResourceHandler.
When using SimpleHtmlExporterOutput(java.io.OutputStream) no handlers are registred by default, hence your output.
When using file-based SimpleHtmlExporterOutput like SimpleHtmlExporterOutput(java.io.File) or SimpleHtmlExporterOutput(java.lang.String) and their flavours, an image handler is registered by default. Besides the HTML file that it writes to disk it will also write a folder(ending in _files) containing your images.
In your case, depending on how you want to deal with images, you could register:
A net.sf.jasperreports.engine.export.FileHtmlResourceHandler to mimic the existing behaviour as described above:
HtmlExporter exporter = new HtmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleHtmlExporterOutput htmlExporterOutput = new SimpleHtmlExporterOutput(baos);
htmlExporterOutput.setImageHandler(new FileHtmlResourceHandler(new File("html_images"), "html_images/{0}"));
exporter.setExporterOutput(htmlExporterOutput);
exporter.exportReport();
This will create the html_images folder with the images from your report and point the HTML <img>s to them. You will see that they have different names than the original ones do. This is because some internal mappings are done, and each image name contains the actual location in the jasperPrint.
Or a net.sf.jasperreports.web.util.WebHtmlResourceHandler to provide a custom handling, like serving the images through a servlet.
HtmlExporter exporter = new HtmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleHtmlExporterOutput htmlOutput = new SimpleHtmlExporterOutput(baos);
htmlOutput.setImageHandler(new WebHtmlResourceHandler("http://myserver/images?image={0}"));
exporter.setExporterOutput(htmlOutput);
exporter.exportReport();
In this case the image handling is a bit more complex and involves retrieving the images from the jasperPrint, something similar to what the ImageWebResourceHandler does.
this is my solution:
I need to export in PDF, xlsx and show the report in HTML. For each there are a method. But I had the problem with HTML the image missing. But the solution thanks to Narcis is like this:
public static byte[] exportToHtmlWithConn(String urlReport, Connection conn) throws JRException {
Map<String, Object> parameters = new HashMap<>();
parameters = returnLogoPutAsParam();
JasperReport report = JasperCompileManager.compileReport(JasperUtilities.class.getResourceAsStream(urlReport));
jasperPrint = JasperFillManager.fillReport(report, parameters, conn);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
HtmlExporter exporter = new HtmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleHtmlExporterOutput htmlExporterOutput = new SimpleHtmlExporterOutput(baos);
htmlExporterOutput.setImageHandler(new FileHtmlResourceHandler(new File("html_images"), "/web/resources/images/logo.png"));
exporter.setExporterOutput(htmlExporterOutput);
exporter.exportReport();
return baos.toByteArray();
}
In this part htmlExporterOutput.setImageHandler(new FileHtmlResourceHandler(new File("html_images"), "/web/resources/images/logo.png")); I put the logo.png in one folder inside the resources in my frontend.
I use the method returnLogoPutAsParam(); for return the image for PDF and xlsx
public static Map<String, Object> returnLogoPutAsParam() {
Map<String, Object> parameters = new HashMap<>();
InputStream logo = returnLogoByte();
parameters.put("logo", logo);
return parameters;
}
And finally this:
public static InputStream returnLogoByte() {
InputStream is = null;
try {
byte[] imageByteArray;
BufferedImage img = ImageIO.read(JasperUtilities.class.getResourceAsStream("/reports/logo.png"));
ByteArrayOutputStream bas = new ByteArrayOutputStream();
ImageIO.write(img, "bmp", bas);
imageByteArray = bas.toByteArray();
System.out.println("Byte[] logo:" + imageByteArray.length);
is = new ByteArrayInputStream(imageByteArray);
return is;
} catch (Exception e) {
System.out.println("Error reading logo.");
e.printStackTrace();
}
return is;
}
Really I am not sure if this is the best solution but it works for one image.
I hope this info help to someone.
Cheers.

Related Links

Can't locate ImageIcon in JButton [duplicate]
How to calaculate HMAC-SHA3 in Android using Crypto.js?
jssc always timeout
Unable to edit netbeans.conf
Column value missing in BIRT report for Cassandra data source
Is it possible to pass optional parameter testIncludes or testExcludes to maven compile plugin through the command line?
Facebook4j - This authorization code has been used
Label not updating in same instance of GUI
How to make fields mandatory in SWT dialogue?
Java: My statements int he Main method are printing, but not in my other methods
Passing base64 encrypted string in the URL
How to create a private method which takes String in any uppercase/lowercase combination and return only the first letter uppercased
Can't find file error handling [duplicate]
Error in servlet url pattern matching implementation
How to solve java.lang.NoClassDefFoundError when creating bat file?
Understanding passing this in a medthod call in java

Categories

HOME
arduino-uno
google-chrome-extension
openstack
dictionary
reserved
onedrive
tesseract
sharepoint-designer
rubygems
sqlite3
alpha
podio
append
pheatmap
virtualization
volttron
rascal
circular-dependency
spring-tool-suite
windows-phone-7
netflix
workload-scheduler
after-effects
pc
contextmenu
custom-wordpress-pages
nas
xlsxwriter
jquery-ajaxq
tokenize
realex-payments-api
rundeck
pingfederate
minitab
buck
tasklet
unboundid
skia
asset-pipeline
hexo
io-redirection
devextreme
webdriver-manager
temporary-files
dynamic-reports
isbn
outlook-api
acoustics
galleria
chain-builder
dds
fancybox-2
aurelia-validation
jxcore
python-cryptography
account-kit
infix-notation
merge-conflict-resolution
connect-by
ubuntu-10.04
pagerank
pyke
splash
jsapi
csquery
python-green
offloading
knuth
winddk
googlemock
onactivityresult
terminal-services
dexterity
spring-io
html4
anonymous-methods
bluepill
frameset
dbproviderfactories
osql
enter
whoosh
invite
propagation
telerik-scheduler
digest-authentication
.nettiers
suppress
substrings
sef
ctp4
phonon
django-notification
3gp

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile