Printing dynamically created output through JSP in PDF format directly

点点圈 提交于 2019-12-02 11:51:15

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.

Essentially the steps are:

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

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

  3. Pass this String to XHTMLRenderer to generate the PDF.

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!