Loupax
Loupax

Reputation: 4914

How can I print a PDF fetched via an ajax request?

I'm having a form that once submitted, the PHP generates a PDF file and sends it to the client. Everything works fine so far. What I'm having trouble with is that I need to trigger window.print() on the window containing the received pdf. Is there a way to make the printing wizard appear for the received pdf file?

Here is the code I have

//The #options is a form that once submitted is sends the requested PDF to the browser
$('#options').on('submit', function(e){

    if($(this).find('[name="action"]').val() == 'print')
    {
        var url = $(this).attr('action') || document.URL;
        e.preventDefault();
        $.post(url, $(this).serializeArray(),function(pdf){
            // Open the printing wizard for the requested document
        });
    }
    else
    {
        // The PDF is displayed normally
        return true;
    }

});

I'm not even sure if what I want to do is possible. Is is possible for example to open the PDF in a new tab and call window.print() there?

Upvotes: 1

Views: 8865

Answers (2)

VinayC
VinayC

Reputation: 49195

To open PDF in new window, you need to essentially generate an GET request (so that window can be open via URL) - one of the simple way is to code your server side code to accept the input parameters via query string. Better way is to use POST request (as you are currently doing) to generate the PDF at the server side and cache it in temp location, then return some token/ticket (e.g. it can be as simple as temp file name) to the browser. This token would be used in GET request to get the PDF file - GET request would go to server that would simply read the file off temp location and return it back as inline ((i.e. header content-disposition: inline;). Then you may try window.print() to print it. Similar ways can be used with iframe (with contentWindow.print()).

However, you may find that these solutions may not work - for example, there is no PDF plugin to display the PDF inline (or user has chosen always open file externally). Or it may not work across browser. So yet another (and IMO better) way is to embed java-script within PDF it self to instruct for print as soon as the file is opened.
For example, see this PHP code example that would embed java-script in PDF generation for auto printing - the example is using FPDF for PDF generation.

Upvotes: 1

Wolf
Wolf

Reputation: 2150

One easy approach for this is to put the PDF file in a new iFrame. Then you can print the complete content inside the iframe using window.print(); function.

<html>
<head>
<title>Print Test Page</title>
<script>
function printPDF() {
    window.frames["print_frame"].window.focus();
    window.frames["print_frame"].window.print();
}
</script>
</head>
<body>
Some content here
<iframe name=print_frame width=0 height=0 frameborder=0 src=about:blank>
  Your PDF is loaded here
</iframe>
Some more content here
</body>
</html>

Now call window.print(); function when you want to print your pdf.

Upvotes: 3

Related Questions