File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ajax and PDF data via Javascript

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got an odd requirement and I've already told the powers that be that it probably isn't possible, but I also said I would exhaust any resource to find out for sure.

Long story short, our system returns a PDF dynamically created from an AFP file based on a bunch of URL parameters. For reaons that would take too long to explain now, if I submit this url and parameters via AJAX I get the binary PDF data back in the responseText. It is not a file that is created. The binary data is streamed directly to the browser. Is there any way at all I can get that data from the ajax response and in javascript somehow tell the browser to render it as PDF?

Thanks.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope,

What you should do is go to the trusty old iframe and set the src to the dynamic pdf and whamo, it shows up.

Eric
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eric Pascarello:
Nope,

What you should do is go to the trusty old iframe and set the src to the dynamic pdf and whamo, it shows up.

Eric


Yea, I figured as much. The "problem" is that some of these PDF's have hundreds to thousands of pages. The server process to generate the PDF takes a bit when this is the case. So they wanted some sort of status messages placed on the browser. It is fairly simple using the readyState's to determine where in the process we are. And I could update some text accordingly.

If I do the iframe, I am in the same situation as if I just loaded it in the browser without the iframe.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't you just put a loading message on the screen when the user clicks on the link for the pdf and set the source of an iframe?

I personally would have a link, tell them to right click and save and not deal with this. But knowing people, they do not want them to save it.

Eric
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eric Pascarello:
Can't you just put a loading message on the screen when the user clicks on the link for the pdf and set the source of an iframe?

I personally would have a link, tell them to right click and save and not deal with this. But knowing people, they do not want them to save it.

Eric


I'm just looking for answers to the questions posed to me. Link is not an option. Thanks Eric.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:

Link is not an option.


Why not? If the link invokes a servlet that sets the content-disposition response header correctly, the streamed PDF can appear in the browser. Why force an Ajax solution? Or is it that whoever is calling the shots heard that Ajax is "the bomb" and insists on using it whether it's appropriate or not?
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


Why not? If the link invokes a servlet that sets the content-disposition response header correctly, the streamed PDF can appear in the browser. Why force an Ajax solution? Or is it that whoever is calling the shots heard that Ajax is "the bomb" and insists on using it whether it's appropriate or not?


Let me clarify. The link that is required runs some javascript that opens a new window. So the right click wouldn't work anyway. The powers that be though Ajax *might* help solve a problem, that isn't a real problem to begin with. They are concerned that users will get click happy waiting for the PDF to load and wanted something a bit more that "Please wait...".

If the link invokes a servlet that sets the content-disposition response header correctly,...

Ahh, to work with Servlets again. Now that would be pure bliss.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a thought. Feel free to shoot it down as I'm sure my brain is not functioning at its full capacity right now.

Let's assume that the Servlet that's pushing out the generated PDF is doing that in small chunks--let's say 10kB at a time. Each request for a PDF has a unique session ID. What if the Servlet would simply stop after each pushing each 10kB to the response stream and update a "delivery counter" in the session? You could then have an Ajax timer polling for the completion percentage which would be calculated based on the session's delivery counter and the PDF file's size.

If the PDF's size is not known beforehand, perhaps it would be possible to make an educated guess? Perhaps by generating a "prototype" of each PDF to sit in some obscure folder on the server's filesystem?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:

If I do the iframe, I am in the same situation as if I just loaded it in the browser without the iframe.


Not really. When you use the iframe mechanism, it will run in parallel with its parent where you can monitor the iframe at regular intervals.

Whether there's something useful or not to monitor is another question. The document object does possess a fileSize attribute, but whether it is something that is updated dynamically as the file loads is not something I know (and am not in a position at the moment to test).
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Plus, the problem is not so much how long the document takes to stream to the browser, but how long it takes for the document to actually be created by the back-end process. That happens prior to the streaming.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
Plus, the problem is not so much how long the document takes to stream to the browser, but how long it takes for the document to actually be created by the back-end process. That happens prior to the streaming.

Could you project a best guess based on history?

After all, what matters most with progress bars is that they're moving--not that they're accurate
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic