2

This is what I want to do:

I want to send an HTTP request to a server, potentially returning a PDF file. But the server may also just return an error code (PDF file unavailable, PDF file invalid, PDF system down, etc). When I get the PDF, I would like to open the PDF and refresh the page that loaded the PDF, because the PDF is then marked as "read". When I get an error code (or timeout), I would like to redirect the page to an error screen. Downloading Google Chrome works in a similar manner:

http://www.google.com/chrome/eula.html?hl=en&platform=win

This is what I don't want do:

For performance reasons, I don't want to issue two requests as suggested in this question here:

Download and open pdf file using Ajax

Two requests can mean:

  1. Make a request for the PDF and return a code to indicate whether the PDF is available or not. If unavailable, immediately display an error page
  2. If it is available, open a window and request the PDF again in that window, and display it.

That's expensive because the PDF's have to be accessed via remote systems. I don't want to access the PDF resource twice. Another solution involving two requests:

  1. Make a request for the PDF and retrieve an error code or a temporary URL where the PDF is cached. On error, immediately display an error page
  2. If the PDF is available, open a window in which the cached PDF is displayed.

This will require for quite a large cache for the PDF's

This might be an interesting lead:

I found this question here giving me some information about how I could download the binary data and make it available in JavaScript as binary data:

Is there a way to read binary data in JavaScript?

Maybe that's a nice lead, but of course it won't solve my problem yet, as I want to use the browser's default editor to open the file, just as if I had requested the file from a normal URL.

So the question is:

Can I download binary data and open them like a regular document from JavaScript? If not, I'll cache the document in some managed memory container in Weblogic and just hope that this won't kill our system. Please only respond:

  • If you know for sure it cannot be done (some links explaining why would be nice)
  • If you know how to do it
  • If you have a different solution doing roughly what I want to do (not issuing two requests)
Community
  • 1
  • 1
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • You lost me at "fetching PDF's two times"... why would you ever have to fetch it twice? – Dolph Sep 08 '11 at 14:39
  • Hmm, true, that might not make sense outside of my head. I'll rephrase that section – Lukas Eder Sep 08 '11 at 14:45
  • You can display pdf files with javascript. The most popular library is [pdf.js](https://github.com/andreasgal/pdf.js), [demo](http://people.mozilla.org/~gal/test.html). Also you can fetch raw html via ajax request (or part of code on the page) and generate pdf with [jspdf](http://code.google.com/p/jspdf/) as binary data via data:uri - [demos](http://snapshotmedia.co.uk/blog/jspdf) – atma Sep 08 '11 at 15:04
  • @atma: Amazing libraries! They might not be useful for me, but nice to see... – Lukas Eder Sep 08 '11 at 15:18
  • Just interesting. If you want to use the browser's default viewer and only one request, why you can not encode on the server side pdf file (e.g. `base64_encode` in php) and make response like a `{success: true, message: "all ok", code: xx, data: encoded_bin_data}`. Then `if (resp.success) document.location.href = 'data:application/pdf;base64,'+encoded_bin_data; else show_error_page(resp.code)`? – atma Sep 08 '11 at 15:51
  • @atma: I've tried those `data:` urls (jspdf demos). They don't seem to be supported by Firefox or IE very well. I need full compatibility, even with older IE versions... – Lukas Eder Sep 09 '11 at 06:57

1 Answers1

3

The implemented "old-school" solution works like this:

  1. The JavaScript client sends an AJAX request to the server to "prepare" a PDF document
  2. The server responds with any of these three messages:
  3. The JavaScript client then reacts as such:
    • a) Open the returned URL in a new window, refresh the current window after 5 seconds
    • b) The current window is redirected to an error screen
    • c) The current window stays unchanged and AJAX polling is implemented to repeat step 2
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • This only allows you to inform the user when their file is prepared, right? You won't know when the file has actually been downloaded successfully? – crush Mar 11 '15 at 22:00
  • @crush: Why not? I mean, the download is a "classic" download of a PDF at a given URL. Browsers tend to indicate successful downloads... – Lukas Eder Mar 12 '15 at 07:46