Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Ajax: pdf response

I have a link and when user click on it, then he get a PDF. In jQuery, I create a POST ajax call to the server to get the PDF. The response is a PDF, with correct content headers etc that would normally cause the browser to open the Reader plugin, or allow the user to save the PDF. But in my case, this is not working. Is there any way to set data content type, or set content type to PDF?

My ajax call:

$('#sf_getpdf').click(function() {

 $.ajax({    //create an ajax request to load_page.php

        type: "POST",

        url: "index.php?route=sale/order/superfaktura_getpdf&token=<?php echo $token; ?>",

        data: 'invoice_id=<?php echo $invoice_id; ?>&sf_token=<?php echo $sf_token ?>',  //with the page number as a parameter

        dataType: "text",   //expect html to be returned


        success: function(msg){



            if(parseInt(msg)!=0)    //if no errors

            {

            document.write(msg)
            }

        }

    });

});

Firebug, response: Firebug

Response in browser...

response in browser

I already tried to set content-type in server, without success:

content-type in server

Edit: The Ajax is not needed, to do this.

<a id="adr_stitok" target="_blank" href="index.php?route=sale/order/superfaktura_getpdf&token=<?php echo $token; ?>&invoice_id=<?php echo $invoice_id; ?>&sf_token=<?php echo $sf_token ?>" >Download</a></td>

Will do the thing.

like image 529
Adrian Avatar asked May 16 '12 21:05

Adrian


People also ask

How to download Pdf using jQuery AJAX?

Downloading PDF File on Button Click using jQueryInside the DownloadFile JavaScript function, the URL of the File is passed as parameter to the jQuery AJAX function. Inside the jQuery AJAX function, using the XmlHttpRequest (XHR) call, the PDF file is downloaded as Byte Array (Binary Data).

How can I open PDF file in Ajax?

Add a following code in the index. cshtml for open a PDF in new tab or download the PDF using the Ajax Call. var pdfData = new Uint8Array(num); //var blob = new Blob([pdfData], { type: 'text/plain' });

How can I download Ajax PDF in PHP?

Code PHP: <? php $file_name = $_POST['file_id']; // We'll be outputting a PDF header('Content-type: application/pdf'); // It will be called downloaded. pdf header('Content-Disposition: attachment; filename="'.


1 Answers

Be sure your server side returns a downloadeable content and make a submit to the file, something like:

            //$.download('path', 'data' [, 'post'])
            $.download = function(url, data, method) {
                //url and data options required
                if(url && data) {
                    var form = $('<form />', { action: url, method: (method || 'get') });
                    $.each(data, function(key, value) {
                        var input = $('<input />', {
                            type: 'hidden',
                            name: key,
                            value: value
                        }).appendTo(form);
                    });
                return form.appendTo('body').submit().remove();
                }
            throw new Error('$.download(url, data) - url or data invalid');
            };

$.download("index.php?route=sale/order/superfaktura_getpdf&token=<?php echo $token; ?>", {}, 'post')
like image 145
Fagner Brack Avatar answered Oct 14 '22 23:10

Fagner Brack