如何强制jasper报告中的图像url导出为HTML? [英] How to force image url in jasper report export to HTML?

查看:218
本文介绍了如何强制jasper报告中的图像url导出为HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

报告在网络服务器上使用图像(但不一定是应用程序的网络服务器)。该报告有一个图像元素表达式,如下所示:

 http://www.example.de/images/+ $ F {picture} 

当我使用 JRXhtmlExporter 并在浏览器中显示生成的HTML,图像不可见。当我用萤火虫检查img标签时,src参数与表达式不同,但有些生成的文件夹和生成的文件名。如果报告通过 JasperExportManager.exportReportToPdfStream()导出为PDF,图像将正确显示在生成的PDF文件中。



我将 JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR 设置为 Boolean.FALSE ,但它没有



如何强制图片url在导出时保持不变?



注意:是懒选项 iReport做什么我想。

解决方案

关键是将 isLazy 属性设置为 true (如此答案中的@ThomasKessler所示)。这适用于我,并完美地生成了三个报告(PDF,XLS,HTML)。



我做了以下工作:

.jrxml

  ... 
<参数名称=LOGO_URL class =java.lang.StringisForPrompting =false/>
...
< image isLazy =true>
< reportElement uuid =24062838-1ede-4578-acdf-9a63662ea738x =0y =0width =108height =30/>
< imageExpression><![CDATA [$ P {LOGO_URL}]]>< / imageExpression>
< / image>
...

.properties 文件中,我有配置(针对每个环境):

  my.logo.url = http:// localhost:8080 / MySite / img / my_logo .jpg 

Servlet 中,我有3种方法: generatePDFReport,generateXLSReport 和 generateHTMLreport 。在这最后一个,我有:

 属性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();

然后行:

  Map参数= Report.extractJasperParams(request.getParameterMap()); 

我设置报告的所有参数,包括 LOGO_URL ,设置属性值。

在我的情况下,我使用这种通用方法来生成我需要的所有报告。 Report.extractJasperParams 方法使用 request 的映射来反映应该生成哪个报告并相应地设置参数,但是您可以根据您的特定需求简化它。



Configurator.getPoperties()方法用于简化属性文件的加载(在我的情况下,一些加密值)。

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

这篇关于如何强制jasper报告中的图像url导出为HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆