15

I'm trying to display a PDF(which is created in the server side and pass to the client side as a web stream) through an AJAX call. My code is given below:

jQuery.ajax({
    type: "POST",
    processData: false,
    url: "aaaa.p?name=pdf",
    data: inputxml,
    contentType: "application/xml; charset=utf-8",
    success: function(data)
    {
      // here the data is the PDF stream i'm getting from the server side. 

    }
});

The 'inputxml' is containing input parameters for the server to create the PDF. and the 'data' in the success function containing PDF stream. Is there any way to open the PDF file on the browser inside the success function of the AJAX call with-out doing any page submit? In the server side the PDF is not physically generated also. Highly appreciate your help....

4 Answers 4

16

Why do you load it via AJAX? Why don't you load it in an IFRAME that you generate when you need it. The standard browsers plugin will display it then inside that Iframe.

$('#link').click(function(e) {
    e.preventDefault(); // if you have a URL in the link
    jQuery.ajax({
        type: "POST",
        processData: false,
        url: "aaaa.p?name=pdf",
        data: inputxml,
        contentType: "application/xml; charset=utf-8",
        success: function(data)
        {
            var iframe = $('<iframe>');
            iframe.attr('src','/pdf/yourpdf.pdf?options=first&second=here');
            $('#targetDiv').append(iframe);
        }
    });
});
11
  • Thanks for the quick respond. Can you give me some working example?
    – Knissanka
    Jan 28, 2013 at 9:44
  • I added something. inputxml must be converted to parameters for the URL request, but that shouldn't be a problem.
    – Zim84
    Jan 28, 2013 at 9:51
  • Thanks for the reply, but the issue is the inputxml is bit large and containing lot of data. I don't think its possible to put it to the URL as a query string.
    – Knissanka
    Jan 28, 2013 at 10:47
  • Then you can send the inputxml via AJAX and when the server tells you everything is ok you can insert the iframe. In the comment from a different post you said that the xmldata and the pdf aren't correlated, so this should work.
    – Zim84
    Jan 28, 2013 at 10:56
  • 2
    yes. instead of iframe.attr('src','/pdf/yourpdf.pdf?options=first&second=here'); Can I add the returned value from the AJAX success function to the iFrame.? (instead of specifying the src, because the respond not containing any URL, but only the PDF stream)
    – Knissanka
    Jan 28, 2013 at 10:59
8

Here is my way of dealing with this issue. It is based on line 50 of this pdfmake file (https://github.com/bpampuch/pdfmake/blob/master/src/browser-extensions/pdfMake.js).

  1. assuming you have a pdf stream I convert it to base64 and echo it back to my AJAX:

    $pdfString = $mpdf->Output('', 'S');
    $pdfBase64 = base64_encode($pdfString);
    echo 'data:application/pdf;base64,' . $pdfBase64;
    
  2. Here is my AJAX code. When receiving data it opens a new window and replaces url with base64 endoded data:

    var ajaxRequest = $.ajax({
        url: "php/generate-pdf/print-control.php",
        data: '',
        cache: false,
        contentType: 'application/json',
        processData: false,
        type: 'POST'
    
    });
    $.when(ajaxRequest).done(function (ajaxValue) {
        var win = window.open('', '_blank');
        win.location.href = ajaxValue;
    });
    

    The downside of this method is that you get a base64 string in the adress bar.

3
  • Thanks a lot!!! After day of search this is the only answer that not display a blank pdf :)
    – Mistre83
    Feb 5, 2017 at 8:49
  • This answer should be the accepted one. It solved my problem since i always use GET for rendering PDF with html2canvas image. Since GET can't accomodate the length of the base64 encoded image, i did a rough workaround to get it work until i saw this one. :) Jul 4, 2019 at 1:44
  • Thanks so much. Works well when combined with download.js like this $.ajax({... success: function(data) {download(data, "test.pdf", "application/pdf");}); See the binary section of download.js Aug 16, 2019 at 15:11
3

For anybody that stumbles across this. Here is an example using axios

  1. responseType must be 'arrayBuffer'
  2. create a Blob object from response
  3. create an "object url" from the blob that you can load into the iframe

        axios({
            url: `path/to/pdf`,
            method: "GET",
            responseType: 'arraybuffer'
        }).then((response) => {
            let blob = new Blob([response.data], { type: response.headers['content-type'] } );
            let url = window.URL.createObjectURL(blob);
    
            $('#frame').attr('src',url);
        });
    
0
0

You can generate a temporary URL to access the PDF file stored on the server, instead of sending it back to the AJAX call. Just pass the generated URL back to the client. Then, when you receive the URL, you could for example do a window.location = to redirect the browser to the download.

Make sure that the correct headers are set for the generated file (Content-Disposition: attachment etc.), and all should be fine.

Edit: Although you could probably just use a regular (non-JavaScript) link to generate and download the file. But doing it via AJAX enables you to show some specific animation etc. to the user while the file is being generated.

4
  • Thanks for the quick respond. In my situation the PDF file is not physically generated on the server. And I need the AJAX call to send the input parameters to the server side.
    – Knissanka
    Jan 28, 2013 at 9:51
  • Well, I see no way how to "download" a PDF file using AJAX. Either the data is processed by the browser, which can result in a download - or by JavaScript, which usually triggers DOM insertion etc. You would need a way to convert the PDF to HTML in order to display it - theoretically possible, but JavaScript is not really meant to do that, and that's probably not what you want. Jan 28, 2013 at 9:54
  • Using GET parameters instead of POST is not an option? Jan 28, 2013 at 9:55
  • Is there any jquery plugin which help me to do this?
    – Knissanka
    Jan 28, 2013 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.