File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Ajax and PDF data via Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Ajax and PDF data via Javascript" Watch "Ajax and PDF data via Javascript" New topic
Author

Ajax and PDF data via Javascript

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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.


GenRocket - Experts at Building Test Data
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jul 11, 2001
Posts: 15300
    
    6

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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jul 11, 2001
Posts: 15300
    
    6

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

Joined: Jan 10, 2002
Posts: 61772
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61772
    
  67

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

Joined: Jul 11, 2001
Posts: 15300
    
    6

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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ajax and PDF data via Javascript