JSF/Seam:如何在没有用户干预的情况下下载和打印动态生成的PDF文件? [英] JSF/Seam: How to download and print a dynamically generated PDF file without user intervention?

查看:89
本文介绍了JSF/Seam:如何在没有用户干预的情况下下载和打印动态生成的PDF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSF/Seam Web应用程序,该应用程序具有一个带有表单的页面,该表单在提交(单击按钮)时会基于表单输入来动态创建PDF文件(在Java中,服务器端).目前,我正在做的工作是将生成的PDF作为文件下载返回到浏览器,因此用户可以选择保存它或在Acrobat Reader中打开它以进行后续打印.

I have a JSF/Seam web app which has a page with a form which, when submitted (button clicked), causes a PDF file to be dynamically created (in Java, server side) based on the form input. Currently what I have working is that the resulting PDF is returned to the browser as a file download, so the user can opt to save it or open it in Acrobat Reader for subsequent printing.

我想发生的事情是,PDF被发送到浏览器并打印(客户端),而无需进一步的用户干预(嗯,除了可能出现Windows打印机选项对话框外,我无能为力).

What I would like to happen is that the PDF is sent to the browser and printed (client side) without further user intervention (well, other than perhaps the Windows printer options dialog appearing, about which there's nothing I could do).

该解决方案似乎是基于在要加载PDF的页面上具有隐藏的iframe,然后在iframe上调用.contentWindow.print().但是,我不知道如何通过HttpServletResponse(在Java中)将PDF导入iframe,更不用说一旦加载pdf后如何在iframe上自动调用print()了.

The solution seems to be based on having a hidden iframe on the page into which the PDF is loaded and then calling .contentWindow.print() on the iframe. However, I have no idea how to get the PDF into the iframe via the HttpServletResponse (in Java), much less how to automatically call print() on the iframe once the pdf has been loaded.

推荐答案

但是,我不知道如何通过HttpServletResponse(在Java中)将PDF导入iframe

However, I have no idea how to get the PDF into the iframe via the HttpServletResponse (in Java)

<iframe src>指向 servlet URL,该URL获取PDF的InputStream并将其写入响应的OutputStream.如有必要,您可以将JSF bean属性作为附加的GET参数传递,以便您可以控制PDF的生成.

Let the <iframe src> point to a servlet URL which gets an InputStream of the PDF and writes it to the OutputStream of the response. You can if necessary pass JSF bean properties as additional GET parameters so that you can control the PDF generation.

<iframe src="pdfServlet?foo=#{bean.foo}&amp;bar=#{bean.bar}"></iframe>

有问题的servlet的doGet()方法看起来像这样:

The doGet() method of the servlet in question can look like this:

String foo = request.getParameter("foo");
String bar = request.getParameter("bar");

response.setContentType("application/pdf");

InputStream input = generatePdfSomehowBasedOn(foo, bar);
OutputStream output = response.getOutputStream();
// Now just write input to output.


更少的是在加载pdf后如何在iframe上自动调用print().

<iframe onload>上挂接一个功能.但是,您需要考虑浏览器的特定行为.在此问题中可以找到更多提示:如何如何从Safari/Chrome中的javascript打印iframe

Hook a function on <iframe onload>. You however need to take browser specific behaviours into account. More hints can be found in this question: How do I print an IFrame from javascript in Safari/Chrome

这篇关于JSF/Seam:如何在没有用户干预的情况下下载和打印动态生成的PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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