1

So I need to be able to open a pdf that is generated, by a server-side script, based on the html that I send it. The html is a huge string. I was thinking using ajax with widow.open but I can't seem to wrap my head around how.

$.ajax({
        type: "POST",
        url: "/pdf",
        data: {input:html},
        success: function(msg) {
            window.open(msg)
        },
        error: function(msg) {

        }
    });

I thought this would work, but it did not.

Travis Pessetto
  • 3,260
  • 4
  • 27
  • 55
Phong Le
  • 281
  • 5
  • 14
  • 3
    Would this help http://stackoverflow.com/a/2186623/723057 ? – zybroxz Jun 11 '12 at 12:43
  • I believe it what I'm looking for. I can't really comprehend it. – Phong Le Jun 11 '12 at 13:14
  • Do you need help with the JavaScript or the back-end processing? The reason I ask is because I think JavaScript itself cannot generate a PDF, but if can get it from a script and render the right content headers. – Travis Pessetto Jun 11 '12 at 16:03
  • 1
    I don't think this is possible in a simple cross-browser way. Why not open an `iframe` with the PDF in it? – Pekka Jun 11 '12 at 16:56
  • possible duplicate of [POST to server, receive PDF, deliver to user w/ jQuery](http://stackoverflow.com/questions/2186562/post-to-server-receive-pdf-deliver-to-user-w-jquery) – icktoofay Jun 12 '12 at 05:57
  • So the back end server takes html that I give it and generates a PDF. That PDF is then returned back to me. Sorry if it was initially confusing I been working on this project for 12 straight hours. – Phong Le Jun 12 '12 at 15:52
  • So I discovered that it was better not to do this with ajax. I did it using this tutorial which worked out perfectly. http://filamentgroup.com/lab/jquery_plugin_for_requesting_ajax_like_file_downloads/ – Phong Le Jun 12 '12 at 19:19

1 Answers1

-1

You can use a jQuery PDF Viewer plugin. For example, using jsPDF you'll write something like this:

$.ajax({
   type : "POST",
   url : "/pdf",
   data : {
      input : html
   },
   success : function (msg) {
      var pdf = new jsPDF();
      pdf.text(20, 20, msg);
      pdf.output('datauri');
   },
   error : function (msg) {}
});
Victor
  • 5,493
  • 1
  • 27
  • 28
  • If I understand the question correctly, it's asking how to view a PDF retrieved from the server with AJAX; not generate a PDF based on content received with AJAX. – icktoofay Jun 12 '12 at 05:56
  • @icktoofay Hmm. Maybe you're right, but looking at code example, I'm not agree with you. It seems to me that the server returns a message (`msg`), not an URL. – Victor Jun 12 '12 at 10:46
  • Sorry for the confusion but @icktoofay is correct. I need to be able to view the pdf. – Phong Le Jun 12 '12 at 15:55
  • @B7ackAnge7z the msg is actually the returned pdf. I know it confusing. – Phong Le Jun 12 '12 at 16:22
  • @PhongLe In this case an `iframe` can be a solution for your problem. Also, you can use `jsPDF`, but server need to return message, not PDF contents. – Victor Jun 12 '12 at 16:33
  • Would be possible to get it into a a different window? The pdf is a report and I really dont want the user to leave the page they were on. – Phong Le Jun 12 '12 at 16:38