JSF/Seam:如何在无需用户干预的情况下下载并打印动态生成的 PDF 文件?

发布于 2024-12-04 20:49:42 字数 476 浏览 0 评论 0原文

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

我希望发生的是,PDF 被发送到浏览器并打印(客户端),而无需进一步的用户干预(好吧,除了可能出现的 Windows 打印机选项对话框,对此我无能为力)。

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

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.

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

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.

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

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

发布评论

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

评论(2

盗梦空间 2024-12-11 20:49:42

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

点到 servlet URL,该 URL 获取 PDF 的 InputStream 并将其写入 的输出流 回复。如果需要,您可以将 JSF bean 属性作为附加 GET 参数传递,以便您可以控制 PDF 生成。

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

相关 servlet 的 doGet() 方法如下所示:

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() 了。

上挂钩一个函数。但是,您需要考虑浏览器的特定行为。更多提示可以在此问题中找到:如何我是否可以在 Safari/Chrome 中从 javascript 打印 IFrame

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

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}&bar=#{bean.bar}"></iframe>

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.

much less how to automatically call print() on the iframe once the pdf has been loaded.

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

尴尬癌患者 2024-12-11 20:49:42

这不是您问题的答案,但您可以使用“kiosk”模式绕过 chrome 中的打印对话框(跳过打印对话框),请参阅:

如何在 Google Chrome 版本 38 上禁用打印预览?

我希望这对您有用:)

引用答案:

命令行参数--disable-print-preview适用于最新版本
Chrome 版本,我刚刚亲自测试过。

This is not an answer to your question, but you can by pass the print dialog in chrome using "kiosk" mode (skipping printing dialog) see:

How to disable print preview on Google Chrome ver 38?

I hope that works for you :)

quoted from answer:

The command-line argument --disable-print-preview works on the newest
version of Chrome, I've just tested it myself.

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