通过JSF发送PDF到浏览器 [英] Send a PDF to browser via JSF
本文介绍了通过JSF发送PDF到浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试发送一个JasperReports生成的PDF文件到用户的浏览器,我找不到我的托管bean方法有什么问题,这里是一个代码片段:
I am trying to send a JasperReports generated PDF file to user's browser, I can't find what's wrong in my managed bean method, here's a snippet:
System.out.println("Making pdf...");
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
String tplPath = ec.getRealPath("testTemplate.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(tplPath);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap<String,Object>(), ds );
String pdfName = "/testReport.pdf";
String pdfPath = ec.getRealPath(pdfName);
JasperExportManager.exportReportToPdfFile(jasperPrint, pdfPath);
System.out.println("PDF ready!");
ec.responseReset();
ec.setResponseContentType(ec.getMimeType(pdfPath));
//ec.setResponseContentLength(contentLength);
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + pdfName + "\"");
InputStream input = new FileInputStream(pdfPath);
OutputStream output = ec.getResponseOutputStream();
IOUtils.copy(input, output);
System.out.println("Sending to browser...");
fc.responseComplete();
简单的用户点击:
<p:menuitem value="TestPDF" action="#{menuController.getTestPdf()}" />
我觉得这很容易找到。为什么我看不到它? :
I feel like it's something easy to find out. Why can't I see it? :)
推荐答案
默认情况下< p:menuitem>
阿贾克斯。您不能通过ajax下载文件。 JavaScript具有安全原因,没有任何设施以编程方式强制使用另存为对话与变量中的任意内容。
The <p:menuitem>
uses by default ajax. You can't download files by ajax. JavaScript has due security reasons no facilities to programmatically force a Save As dialogue with arbitrary content in a variable.
只需关闭ajax。 p>
Just turn off ajax.
<p:menuitem ... ajax="false" />
另请参见:
- 如何从JSF备份bean提供文件下载?
- How to provide a file download from a JSF backing bean?
See also:
这篇关于通过JSF发送PDF到浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文