java 从 extjs 打印 pdf - 最佳解决方案

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2421257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:03:58  来源:igfitidea点击:

Printing pdf from extjs - best solution

javajakarta-eeextjs

提问by maxx

I'm trying to print PDF file using extjs and any help will be appreciated. My idea was to pass pdf file from server as stream as adviced on http://xmlgraphics.apache.org/fop/0.95/servlets.html#minimal-servlet. But problem that firtly I submit form data via ajax, save them to DB, create PDF using FOP and .... want to pass resulted PDF back to client. My last idea is to save PDF to temp file on server, return success:'true' to extjs and then retrieve temp file again using iframe for printing :) Are there any more correct solutions? or may be someone has ready working code for that stuff?

我正在尝试使用 extjs 打印 PDF 文件,任何帮助将不胜感激。我的想法是按照http://xmlgraphics.apache.org/fop/0.95/servlets.html#minimal-servlet 上的建议将 pdf 文件从服务器作为流传递。但是问题是我首先通过ajax提交表单数据,将它们保存到数据库,使用FOP创建PDF,然后......想要将结果PDF传回客户端。我的最后一个想法是将 PDF 保存到服务器上的临时文件,将 success:'true' 返回到 extjs,然后使用 iframe 再次检索临时文件进行打印:) 还有更正确的解决方案吗?或者可能有人已经准备好了这些东西的工作代码?

回答by maxx

Well, finally I came to the next solution: firstly we use AJAX request to save form details, and generate PDF on server side.

嗯,最后我来到了下一个解决方案:首先我们使用AJAX请求保存表单详细信息,并在服务器端生成PDF。

        success : function(form, action) {
            var result = Ext.decode(action.response.responseText)
            if (result.success) {
                this.openForPrint(result.tmpFileName);
            }
        },

Than we use iframe to download and open file

比我们使用 iframe 下载和打开文件

    openForPrint : function(fileSrc) {
        Ext.DomHelper.append(document.body, {
            tag : 'iframe',
            name : 'printIframe',
            src : this.getPrintPalletTagUrl()+'?userAction=actionPrint&tmpFileName='+fileSrc
        });
    }

Such approach lets us to check saving operation response and show meaningful dialog to user if saving fail.

这种方法让我们可以检查保存操作响应并在保存失败时向用户显示有意义的对话框。

回答by Brian Moeskau

I don't think this has anything to do with Ext JS. You either need to generate/store the PDF and return a URL to it as you mentioned, or you could send a response directly back to the browser with content-type "application/pdf" and the default browser behavior will handle it. Either approach is generic to any front end code.

我认为这与 Ext JS 没有任何关系。您要么需要生成/存储 PDF 并像您提到的那样返回一个 URL,或者您可以使用内容类型“application/pdf”将响应直接发送回浏览器,默认浏览器行为将处理它。这两种方法对于任何前端代码都是通用的。

I have done the second approach successfully, but in a .NET environment. The principles should be the same.

我已经成功地完成了第二种方法,但在 .NET 环境中。原则应该是一样的。