2010-09-30 94 views
2

我使用jasper报告库与GWT应用程序。贾斯珀报告与HTML格式

这些报告生成与CSV格式很好,但与HTML格式它生成HTML页面的图标失踪图片。

我知道碧玉使用称为“PX”的透明图像,未找到该图像。

我该如何解决这个问题?

由于提前

+1

我有同样的问题。如果您接受答案,您可以提供解决方案吗?你如何传递报告的图像路径? – Manu 2012-11-29 15:57:22

回答

0

尝试在你的图像作为参数传递到报表,这样就不必担心图像路径。

您可以将参数的类型设置为BufferedImage或任何图像类适合的类型。

0

我的解决方案是使用数据URI。这不是很优雅,因为它扩大了HTML的大小,并且在IE8之前的IE中不起作用,但它确实可以让您不用担心Jasper向您发送的图像附件创建文件。

如果你要实现这一点,你要这个参数添加到您的要求:

<argument name="IMAGES_URI"><![CDATA[data:]]></argument> 

然后,你需要解析的JasperServer发回的报告HTML:

foreach ($attachments as $name => $attachment) { 
    // Cut off the cid: portion of the name. 
    $name = substr($name, 4); 

    // Replace any image URIs with a data: uri. 
    if (strtolower(substr($name, 0, 4)) !== 'uuid' && strtolower($name) !== 'report') { 
     if (strtoupper(substr($attachment, 0, 3)) === 'GIF') { 
      // It's a GIF. 
      $report = str_replace("data:$name", 'data:image/gif;base64,' . base64_encode($attachment), $report); 
     } elseif (/* more file type tests */) { 
      // and so on... 
     } 
    } 
} 

对于大图像,最好按照Gordon的建议做,并传入一个参数,指定永久存储在服务器上的文件的URL。此方法更适合处理JasperServer尝试投掷的任何意外图像的故障安全。

0

我对这个讨论有点迟,但这是我一直在使用的。关键是将imagesMap传递给会话属性和导出器参数,并设置IMAGES_URI导出器参数。

private void exportReportAsHtml(HttpServletRequest request, HttpServletResponse response, JasperPrint jasperPrint) throws IOException, JRException { 
    response.setContentType("text/html"); 

    request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint); 

    Map imagesMap = new HashMap(); 
    request.getSession().setAttribute("IMAGES_MAP", imagesMap); 

    JRHtmlExporter exporter = new JRHtmlExporter(); 
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
    exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, response.getWriter()); 
    exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap); 
    exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "image?image="); 

    exporter.exportReport(); 

}

+3

甚至不能远程线程安全。绝不应该在网络应用程序中完成。如果祈祷告诉客户有两个浏览器选项卡打开并同时生成报告 – MJB 2012-10-02 01:11:31

+0

上述代码需要先检查现有的映像映射,然后再替换它,但除了打开两个选项卡的用户将其导出两次后,它们才会导出报告。 – 2017-07-17 21:13:38

1

如果没有图像显示,那么你可以这样做:

JasperPrint jasperPrint = JasperFillManager.fillReport(path, parameters, con); 
JRHtmlExporter htmlExporter = new JRHtmlExporter(); 
response.setContentType("text/html"); 
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint); 
htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
htmlExporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out); 
htmlExporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, false); 
htmlExporter.exportReport(); 

重要的线是这个:

htmlExporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, false); 

这会使所有的“px”图像消失。