0

I have a URL that causes a web server to generate a PDF when opened. Is it possible to save this PDF document to disk (client side), using Java? I found lots of examples for doing this when the PDF already exists as a document on the web server, but the code for these examples does not seem to work in the case where the web server doesn't begin to create the content until the link is opened (at least that is my impression at this point).

There is a link that I can click to produce the PDF. The HREF for that link is:

<a href="javascript:open_window('ReportDisplay.cfmincidentID=223189&amp;cs=377041B‌​A2467C3CEA7FD989A12126E0E&amp;services=2815&amp;format=1&amp;UniqueID=651F76E4E56‌​91207B9B2AF1F51A780AA')">
    <img src="../../Images/pdf-small.gif" alt="Report" border="0" height="15" width="15">
</a>

I construct a complete URL, including the protocol and such, and I can paste that complete URL into the location bar of the browser. This does in fact produce the PDF in the current window. So, this is what I'm trying to capture into a file on my local disk.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • It doesn't matter if the PDF is generated dynamically or is static. What matters is the content of the response from the web server. However, the site could be using Javascript as part of the process, and in that case, the URL alone is not enough. I don't think any further information can be given without more specific information. – RealSkeptic Nov 30 '15 at 21:33
  • The website itself does pass the URL to a javascript:open_window function. – John Masters Nov 30 '15 at 21:36
  • Well, what's the URL at that window, and can you use it directly? – RealSkeptic Nov 30 '15 at 21:37
  • One the web page, there is a link that I can click on to produce the PDF. The HREF for that link is: – John Masters Nov 30 '15 at 21:43
  • I constructed a complete URL with by adding the http://www.blah... in front of the HREF info. – John Masters Nov 30 '15 at 21:46
  • Don't put code in comments. Any additional information should be added to the question. You can then use the comment section to notify people that you made changes in the question. – RealSkeptic Nov 30 '15 at 21:47
  • I construct a complete url, includeing the protocol and such, and I can paste that complete url into the browser, and it does in fact produce the PDF in the current window. So, this is what I'm trying to capture into a file on my local disk. – John Masters Nov 30 '15 at 21:47

1 Answers1

0

You are currently serving the PDF inline whereas you want to change it as an attachment. See the answer to the question Content-Disposition:What are the differences between "inline" and "attachment"? to find out what's the difference.

If you use the Content-Disposition header "inline", the PDF will be shown in a browser window. If you use Content-Disposition header "attachment", a dialog box will open, asking the end user where he wants to save the PDF.

You can't "automatically" save the PDF on the end user's machine because you don't have any idea about the operating system and the disk organization of the end user. If he's on Windows, the path C:/temp will probably exist, but if he's on a Mac or a Linux machine, that path won't exist. That's why you'll always need a "Save as" dialog on the client side.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Hello, and thank you. To be clear, I can't control the generation of the PDF on the server side, so I don't know if I can adjust the Request attributes. Also, my environment is well know, so I do know where to save the file. I am interacting with two different websites, and the code I found below is working well with one, but not the other. My effort continues to figure out why... – John Masters Dec 01 '15 at 17:46
  • [basic links] http://www.codejava.net/java-se/networking/use-httpurlconnection-to-download-file-from-an-http-url – John Masters Dec 01 '15 at 17:46