Linas
Linas

Reputation: 4408

Download pdf or image through ajax

I would like to send a lot of data through ajax request to my server which will generate pdf or jpg format according to that data.

Now i have done all that, my issue is to how output that generated pdf/jpg back to the user trough ajax? I guess i might be able to use json for that, but im not really sure how, and i think there would be a lot issues with pdf.

Also if some one gonna suggest using form with hidden inputs that will not work since i have really big multidimensional array with lot's of data and it would simply take to much effort to make it work.

By the way, i am using jquery, but anything else is acceptable as long as it does the job done without making me to rewrite half of my script.

Upvotes: 0

Views: 671

Answers (1)

Andy
Andy

Reputation: 11985

To display a JPG

  1. AJAX: You can return the data hex encoded (be sure to set the content type appropriately: header('Content-type: image/jpeg')). Then you just inject an <img/> element into the DOM and set it's src attribute to the returned Data URI.

  2. HTML: Also, you could inject the <img/>'s with a normal src URL to some location on your server.

For PDF

It's a little more tricky. Some browsers display PDF's natively (Chrome/Firefox), others rely on optional third-party plugins. You can detect these plugins, but can't control whether the PDF is displayed in a window/frame or is downloaded.

If you choose to display, you can create a new window/tab to display it or display it in an iframe dynamically.

Upvotes: 1

Related Questions