如何直接在AdobeReader中显示java(jsf)生成的pdf
我正在使用 JSF 2.0 (Eclipse IDE),并且我的一个 bean 中有一种方法可以使用 Apache FOP 1.0 生成一些 PDF 文件。当我单击 JSF 页面上的“makePDF”按钮时,生成的 PDF 会在我的页面所在的同一窗口中打开(并且在查看/保存/打印 PDF 后我必须使用“后退”按钮)。
我可以通过以下方式更改此设置:按下“makePDF”按钮时,会出现标准弹出对话框,询问我是否要保存文件或使用 AdobeReader 打开它。
有没有一种方法,当我单击“makePDF”按钮时,生成的 PDF 会直接在 AdobeReader 中打开(并且我仍然有我的 JSF 页面,不需要确认任何内容)???另外(这并不是真正需要的,但很高兴知道),有没有办法在新窗口中打开它或直接将其发送到打印机?
这是我的代码的主要部分(我删除了一些不相关的内容):
public void makePDF(long contractId, int documentType) {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
String basePath = externalContext.getRealPath("/");
try {
fopFactory.setUserConfig(new File(basePath + "/pdf_transform/config/userconfig.xml"));
fopFactory.setBaseURL(basePath);
fopFactory.getFontManager().setFontBaseURL(basePath);
} catch (Exception e) {
e.printStackTrace();
}
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
foUserAgent.setBaseURL(fopFactory.getBaseURL());
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
response.reset();
response.setHeader("Content-Type", "application/pdf");
response.setHeader("Content-disposition", "attachment");
BufferedOutputStream output = null;
try {
output = new BufferedOutputStream(response.getOutputStream(), 10240);
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, output);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(new StreamSource(xsltfile));
Source src = new DOMSource(makeXML(contract)); // my method
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
} catch (Exception e) {
e.printStackTrace();
}
facesContext.responseComplete();
}`
当我删除 response.setHeader("Content-disposition", "attachment");
时,它会在同一个窗口中打开,否则它要求我保存或打开。
提前致谢。
i am using JSF 2.0 (Eclipse IDE) and i have a method in one of my beans which generates some PDF files using Apache FOP 1.0. When I click "makePDF" button on my JSF page, generated PDF opens in the SAME window where my page is (and i have to use 'back' button after i view/save/print PDF).
I can change this in a way that when "makePDF" button is pressed, standard pop-out dialog appears and asks me if i wish to save file or to open it with AdobeReader.
Is there a way that, when i click my "makePDF" button, generated PDF is directly opened in AdobeReader (and i still have my JSF page and dont need to confirm anything) ??? Also (it's not really needed, but good to know), is there a way to open it in new window or send it directly to printer?
Here is the main part of my code (i deleted some irrelevant stuff) :
public void makePDF(long contractId, int documentType) {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
String basePath = externalContext.getRealPath("/");
try {
fopFactory.setUserConfig(new File(basePath + "/pdf_transform/config/userconfig.xml"));
fopFactory.setBaseURL(basePath);
fopFactory.getFontManager().setFontBaseURL(basePath);
} catch (Exception e) {
e.printStackTrace();
}
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
foUserAgent.setBaseURL(fopFactory.getBaseURL());
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
response.reset();
response.setHeader("Content-Type", "application/pdf");
response.setHeader("Content-disposition", "attachment");
BufferedOutputStream output = null;
try {
output = new BufferedOutputStream(response.getOutputStream(), 10240);
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, output);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(new StreamSource(xsltfile));
Source src = new DOMSource(makeXML(contract)); // my method
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
} catch (Exception e) {
e.printStackTrace();
}
facesContext.responseComplete();
}`
When i remove response.setHeader("Content-disposition", "attachment");
it opens in the same window, otherwise it asks me to save or open.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在新窗口中打开请求并不是 JSF 直接关注的问题。这更多的是一个 HTML 问题。
将
target="_blank"
添加到表单中,它会将请求提交到新选项卡/窗口中。
如果不引入一些在客户端作为中间人运行的代码(如小程序),则直接发送到客户端的打印机是不可能的。
Opening a request in a new window is not directly a JSF concern. It's more a HTML concern.
Add
target="_blank"
to the formand it'll submit the request into a new tab/window.
Sending to the client's printer directly is not possible without introducing some piece of code which runs at the client side as man in the middle, like an applet.
我遇到了类似的问题,幸运的是找到了一种优雅的方法来解决它。如果有人有更好的解决方案,请告诉我。
使用案例:
用户在 JSF 生成的表单中输入创建 PDF 所需的数据(PDF 是从 servlet 生成的)。如果验证失败,则会呈现错误消息,但如果一切顺利,pdf 文件将在新窗口中打开。:-)
我的解决方案:
在 template.xhtml(或定义 Facelets 的站点)中输入:
JSF 表单中的命令链接也指向 myBean 中的操作方法(例如 myBean.showPdf())
希望它可以帮助某人..
I had a similar problem and luckily found an elegant way to solve it. If someone has a better solution then please let me know.
Use case:
User enters data in a JSF generated form necessary for PDF creation (PDF is generated from a servlet). If validation fails, error messages are rendered but if everything goes fine pdf file opens in a new window.:-)
My solution:
In a template.xhtml (or the site which defines facelets) enter:
Your command link in the JSF-form points to action method in myBean as well (e.g. myBean.showPdf())
Hope it helps somebody..
如果您想直接打开 PDF,请使用
if you want to open a PDF directly, pls use