How to force image url in jasper report export to HTML?

10,384

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).

Share:
10,384
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    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.