tags:

views:

1299

answers:

2

Hey extjs/java GURU,

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?

A: 

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.

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

bmoeskau
Thank you for response. Sure the principles are the same, but devil is in details :) With extjs you can send ajax request to save form but cann't get response as streamed PDF as I got. So in that case we have to send second request to stream file.
A: 

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

        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

    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.

yep, that's how i'd do it. note though, on the server side, make sure you set the response headers to force a download (i think Content-Type and Content-Disposition), otherwise on some setup (Windows with PDF browser plugin), you end up open a pdf in an invisible iframe, rather than asking to download
Ben