通过JSP直接以PDF格式打印动态创建的输出 [英] Printing dynamically created output through JSP in PDF format directly

查看:539
本文介绍了通过JSP直接以PDF格式打印动态创建的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过访问数据库动态地通过JSP打印报表。我正在使用Struts
Framework,Java代码在Action类中。最后,输出通过
JSP页面显示。它在屏幕上很好用,但在打印时它没有正常运行。
我只是想以PDF格式而不是JSP格式显示报告 - > HTML格式或JSP
页面应包含 PRINT IN PDF 按钮/链接做这个工作。

I am printing the report through JSP dynamically by accessing database. I am using Struts Framework and the Java codes are in Action classes. Finally the output is displayed through JSP pages. It is coming nicely on screen but while printing it does not coming properly. I just want to display the report in PDF format instead of JSP -> HTML format or the JSP page should contain a PRINT IN PDF button/link to do the job.

我使用 iText 在其他模块中创建PDF文件,但这太费时间了。这次
我想在我的页面中捕获JSP输出并以PDF格式打印。如何在同一个JSP页面中捕获JSP
输出以不同的格式打印?

I am using iText to create PDF file in other modules but it is too time consuming. This time I want to capture the JSP output in my page and print in PDF format. How to capture the JSP output in the same JSP page to print in different format?

我使用的是 iTextrenderer 但无法打印。它成功转换了包含HTML文件的URL
,但是如何从JSP输出创建 InputStream 并在
iText Renderer中使用或任何其他方法?有可能吗?请建议解决方案。

I was using iTextrenderer but not been able to print. It successfully converts a URL containing HTML file but how to create the InputStream from the JSP output and use in iText Renderer or any other method? Is it possible to do that? Please suggest the solution.

推荐答案

我可以看到你想在这里做什么。 FlyingSaucer / XHTMLRenderer可以很好地完成HTML> PDF转换,但它并不适用于魔术:在你的情况下涉及很多步骤。

I can see what you want to do here. FlyingSaucer/XHTMLRenderer does a good job of HTML > PDF conversion however it does not work by magic: in your case there are quite a few steps involved.

基本上步骤是:


  1. PDF导出按钮调用Struts Action或Servlet传递重新生成JSP所需的所有参数(即最初渲染时) )。

  1. PDF export Button calls a Struts Action or Servlet passing all params necessary to regenerate the JSP (i.e. as it was originally rendered).

此Action或Servlet必须调用JSP(确保设置任何所需的模型属性)并将响应写入String(而不是写入标准)响应)。

This Action or Servlet has to invoke the JSP (ensuring any required model attributes are set) and write the Response to a String (rather than writing to the standard response).

将此字符串传递给XHTMLRenderer以生成PDF。

Pass this String to XHTMLRenderer to generate the PDF.

流式传输btye [ ]由HTMLRenderer返回给客户。

Stream the btye [] returned by HTMLRenderer to the client.

我在这里使用简单的方法添加了一个完整的端到端示例Servlet的。但是,原则与Struts2或任何其他框架相同,因此您应该能够根据需要进行调整。

I have added a complete end-to-end example here using a simple Servlet. However the principles will be the same with Struts2 or any other framework so you should be able to adapt as required.

https://github.com/alanhay/jsp-to-pdf-exporter

这篇关于通过JSP直接以PDF格式打印动态创建的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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