views:

34

answers:

1

I'm using a 3rd party tool (TCPDF) to export data to PDF. If I use a form and post the data to the export php page it works fine. However, I have another need to call the same exporter page and pass it the params it needs via ajax to output the pdf file that way. Everything seems to be working fine, except when I get the output for the pdf it isn't opening up my pdf reader to look at it when I use the ajax call...it just leaves a blank screen. In Firebug console I can see the output for the pdf document, its just not poping up the pdf reader like it does if I use the form and hit the submit button.

Any ideas on what I can do to fix this?

Here's my ajax code if it helps:

url = "admin.php?rst_post=y&action=exportresults&sid=123&answerid=321&filterinc=show&exportstyle=full&answers=long&type=pdf&export_from=&export_to=&convertyto=&convertnto=";

$.ajax({
    async: false,
    type: "GET", 
    url: url, 
    contentType: "charset=utf-8", 
    complete: function(rc)
    { 
        //alert(rc);
    }
});
A: 

Here's a working prototype that I put together. I'm not sure if it does what you're looking for, but I think it may be close.

Assumption: I'm assuming you want it to open up in the Reader plugin for the browser. If not, let me know and I'll adjust my answer accordingly.

Instead of using AJAX, why not dynamically add an IFRAME to you page and display the PDF in there? Here's a working prototype:

<html>
    <head>
        <style type="text/css">
            .inlinePdf {
                width: 50%;
                height: 50%;
            }
        </style>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"&gt;&lt;/script&gt;
        <script type="text/javascript">

            $(document).ready(function() {
                var url = "http://www.irs.gov/pub/irs-pdf/fw4.pdf";
                $("<iframe></iframe>").addClass("inlinePdf").attr("src", url).appendTo("#content");
            });

        </script>
    </head>
    <body>
        <div id="content">
        </div>
    </body>
</html>

This will load a US W-4 PDF (uggh...taxes) in an IFRAME. I'm doing this with document.ready, but I could do this with a button click.

Let me know if this is close. I'll update accordingly. Hope this helps!

BTW - this blog post on Encosia was useful (it deals with ASP.NET AJAX, but the concept is the same).

EDIT: If you just want to open a new window for the PDF (since you're requesting it via a URL), you could just do this instead of an embedded IFRAME:

$(document).ready(function() {
    var url = "http://www.irs.gov/pub/irs-pdf/fw4.pdf";
    window.open(url, "_blank");
});

And if you have control on the server side, set the content-disposition header to attachment and you should get an open/save dialog. The new window should close then.

David Hoerster