A report uses images on a web-server (but not necessarily the application's web-server). The report has an image element expression as follows:
"http://www.example.de/images/" + $F{picture}
When I export the report to HTML using the JRXhtmlExporter
and display the generated HTML in a browser, the image is not visible. When I inspect the img tag with firebug the src parameter is not the same as the expression but some generated folder and generated file name. If the report is exported to PDF via JasperExportManager.exportReportToPdfStream()
the image is displayed correctly in the resulting PDF file.
I set JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR
to Boolean.FALSE
, but it didn't help.
How can I force that the image url stays the same while exporting?
Note: The "Is Lazy" option the iReport does what I want.
The key is setting the isLazy property to true (as indicated by @ThomasKessler in this answer). This works for me and generates the three reports (PDF, XLS, HTML) flawlessly.
I do the following:
.jrxml
...
<parameter name="LOGO_URL" class="java.lang.String" isForPrompting="false"/>
...
<image isLazy="true">
<reportElement uuid="24062838-1ede-4578-acdf-9a63662ea738" x="0" y="0" width="108" height="30"/>
<imageExpression><![CDATA[$P{LOGO_URL}]]></imageExpression>
</image>
...
In a .properties file I have configured (for each environment):
my.logo.url=http://localhost:8080/MySite/img/my_logo.jpg
In a Servlet, I have 3 methods: generatePDFReport, generateXLSReport and generateHTMLreport. In this last one, I have:
Properties prop = Configurator.getProperties(BUNDLENAME);
Connection con = ReportsDB.getConnection();
String reportPathTag = prop.getProperty(Report.JASPERURL);
Map parameters = Report.extractJasperParams(request.getParameterMap());
String jasperPath = parameters.containsKey(reportPathTag) ? (String) parameters.get(reportPathTag) : "";
String reportName = parameters.containsKey(Report.JASPERTITLE) ? (String) parameters.get(Report.JASPERTITLE) : "myReport";
String path = getServletContext().getRealPath("/");
path += jasperPath;
JasperReport jasperReport = null;
JasperDesign jasperDesign = null;
jasperDesign = JRXmlLoader.load(path);
logFilteringCard(parameters);
jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, con);
JRHtmlExporter htmlExporter = new JRHtmlExporter();
htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
htmlExporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
htmlExporter.setParameter(JRExporterParameter.OUTPUT_WRITER, pw);
htmlExporter.exportReport();
con.close();
And in the line:
Map parameters = Report.extractJasperParams(request.getParameterMap());
I set all the parameters of the report, including LOGO_URL, setting the properties value.
In my case I use this generic method to generate all the reports I need. The method Report.extractJasperParams uses the request's map to reflect which report should be generated and sets the parameters accordingly, but you can simplify it for you specific needs.
The method Configurator.getPoperties() is to simplify the loading of the Properties file (in my case a file with some encrypted values).
I would say that the standard solution is to implement a way to serve up your images. Refer to this sample that ships with JasperReports: demo/samples/webapp
.
But if you're willing to pass in a parameter so that you can use Lazy Loading for the HTML but not for PDF, then that will certainly work as well.
I also faced the same problem and resolved the issue with the new Jasper API. Here is the blog article corresponding to it. The problem was resolved using the new API and by using ImageServlet configuration in the web.xml.