如何直接在AdobeReader中显示java(jsf)生成的pdf

发布于 2024-10-07 06:12:49 字数 2112 浏览 5 评论 0原文

我正在使用 JSF 2.0 (Eclipse IDE),并且我的一个 bean 中有一种方法可以使用 Apache FOP 1.0 生成一些 PDF 文件。当我单击 JSF 页面上的“makePDF”按钮时,生成的 PDF 会在我的页面所在的同一窗口中打开(并且在查看/保存/打印 PDF 后我必须使用“后退”按钮)。

我可以通过以下方式更改此设置:按下“makePDF”按钮时,会出现标准弹出对话框,询问我是否要保存文件或使用 Adob​​eReader 打开它。

有没有一种方法,当我单击“makePDF”按钮时,生成的 PDF 会直接在 Adob​​eReader 中打开(并且我仍然有我的 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 技术交流群。

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

发布评论

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

评论(3

爱的故事 2024-10-14 06:12:49

在新窗口中打开请求并不是 JSF 直接关注的问题。这更多的是一个 HTML 问题。

target="_blank" 添加到表单中

<h:form 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 form

<h:form target="_blank">

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

淡写薰衣草的香 2024-10-14 06:12:49

我遇到了类似的问题,幸运的是找到了一种优雅的方法来解决它。如果有人有更好的解决方案,请告诉我。

使用案例:
用户在 JSF 生成的表单中输入创建 PDF 所需的数据(PDF 是从 servlet 生成的)。如果验证失败,则会呈现错误消息,但如果一切顺利,pdf 文件将在新窗口中打开。:-)

我的解决方案:

  1. 在 template.xhtml(或定义 Facelets 的站点)中输入:

    
            
                window.open('#{myBean.pdfUrl}',"pdfWindow","宽度=800,高度=900,左=50,顶部=10")
            
    
    
  2. JSF 表单中的命令链接也指向 myBean 中的操作方法(例如 myBean.showPdf())

  3. 如果表单数据有效并且调用了 showPdf(),则可以将 pdfDownloadable 字段设置为 true 且相同结果再次呈现,但这次带有 window.open() 。

希望它可以帮助某人..

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:

  1. In a template.xhtml (or the site which defines facelets) enter:

    <ui:define name="head">
            <h:outputScript rendered="#{myBean.pdfDownloadable}">
                window.open('#{myBean.pdfUrl}',"pdfWindow","width=800,height=900,left=50,top=10")
            </h:outputScript>
    </ui:define>
    
  2. Your command link in the JSF-form points to action method in myBean as well (e.g. myBean.showPdf())

  3. If form data are valid and showPdf() is called you can set the pdfDownloadable field to true and the same outcome is rendered again but this time with window.open() in it.

Hope it helps somebody..

笑看君怀她人 2024-10-14 06:12:49

如果您想直接打开 PDF,请使用

response.setHeader("Content-Disposition", "inline");

if you want to open a PDF directly, pls use

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