如何强制 jasper 报告中的图像 url 导出为 HTML?

发布于 2025-01-02 14:57:47 字数 674 浏览 3 评论 0原文

报告使用 Web 服务器上的图像(但不一定是应用程序的 Web 服务器)。该报表具有如下图像元素表达式:

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

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

我将 JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR 设置为 Boolean.FALSE,但没有帮助。

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

注意:“Is Lazy”选项 iReport 做了我想要的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提赋 2025-01-09 14:57:47

关键是将 isLazy 属性设置为 true (如 中的 @ThomasKessler 所示)这个答案)。这对我有用,可以完美地生成三个报告(PDF、XLS、HTML)。

我执行以下操作:

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

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

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

Servlet 中,我有 3 种方法:生成PDF报告、生成XLS报告生成HTML报告。在最后一个中,我有:

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

在行中:

Map parameters = Report.extractJasperParams(request.getParameterMap());

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

就我而言,我使用这种通用方法来生成我需要的所有报告。 Report.extractJasperParams 方法使用请求的映射来反映应生成哪个报告并相应地设置参数,但您可以根据您的特定需求对其进行简化。

Configurator.getPoperties() 方法用于简化 Properties 文件(在我的例子中是包含一些加密值的文件)的加载。

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

囍笑 2025-01-09 14:57:47

我想说,标准解决方案是实施一种提供图像的方法。请参阅 JasperReports 附带的示例:demo/samples/webapp

但是,如果您愿意传递一个参数,以便可以对 HTML 使用延迟加载,但不能对 PDF 使用延迟加载,那么这当然也可以。

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.

黑寡妇 2025-01-09 14:57:47

我也遇到了同样的问题,并使用新的 Jasper API 解决了该问题。 这里是与其对应的博客文章。使用新 API 和 web.xml 中的 ImageServlet 配置解决了该问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文