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&cs=377041B‌​A2467C3CEA7FD989A12126E0E&services=2815&format=1&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.
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.
Related
Simple question, I've got a pdf generated on page load, subsequently I want to let the user download the file.
Is there a way to do it? To show the download dialog after pdf was created?
I've tried this:
<%response.setHeader("Content-Disposition", "attachment; filename=\""+filename+"\"");%>
But where do I define, which file do I want user to download? Because code above leads to user downloading some corrupted empty pdf file.
I suggest you do this with a Servlet instead of a JSP. You have more control over the code and it is less fragile (if you handle the response content in the JSP and you have even a single space or line feed between your scriptlets, that can be written to the output stream
corrupting the file content).
So have a look at this FileServlet example and adapt it to your needs.
I have web-application.
When I follow a link:
http://..... document/view?type=pdf
I expect browser to show a pdf file. It works correct on Firefox and IE, but Chrome starts downloading it instead of showing.
Here is the code:
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=test;");
OutputStream out = response.getOutputStream();
out.write(pdfByteData);
out.flush();
What could be the problem? May be it is because of my local chrome settings or something else? I have no idea.
Try to install the pdf plugin in chrome using the following link
https://support.google.com/chrome/answer/142056?hl=en
How Browsers Work With File Downloads
Usually when a user goes to a file URL (for example: a download link), the file will show in the
browser if the browser supports it. Image files like jpg, png, gif
etc. will almost always show in the browser. Archive files like zip,
tar, gzip etc. will always be downloaded. Some file types show up in
some browsers but not others depending of if the browser can read the
file or not. For example, Internet Explorer (IE) will usually try to
show Microsoft Word files (doc and docx) in the browser, while most
other browsers will download it. Google Chrome has its own PDF
converter and it will try to convert a PDF file and render it in the
browser.
The key thing to understand is that some browsers maybe able to read a
particular file type based on the addons you have installed for that
browser while others may not be able to. If a browser can read the
file type it will show it in the browser. If the browser cannot read a
file type it will force a download to the hard disk. Usually this is
not an issue since the users can save the file to there computer after
it is shown in the browser.
Just use HTML command, this what i did to my website :
Click Here to view the PDF file
works in every browser !.
I have a JSF Web application, and at some point i present the client a big chunk of information, I want to have a save as link, that allows the client to save this information on his computer as a .txt file.
Information on how to achieve this or a good tutorial would be great.
Does this work for you? You probably would need to set the ContentType to "application/octet-stream", otherwise the client's browser will display your text file instead of offering the option to "Save as".
I believe your best bet may be to have that link actually generate an Ajax call to generate the text file and set it as the src attribute of an iframe on the page. That will trigger (I think) the file download box.
how a browser open a saved html page ? It must have to run html file and other files from hard-disk. But how can a browser find the link of the other small files ? Is a browser change the link of the other small files of html page from url to hard-disk location?
How it can do that? I want to do the same thing in my application. But I could not figure out the process.
Most browsers store attached resources (Style sheets, images, scripts and the like) in a separate folder named after the saved page.
All references to resources are then converted to relative references, like so:
<img src="name_of_saved_folder/image.jpg">
the browser will then look in name_of_saved_folder relative to the saved HTML document's location.
If the HTML file is moved to a different locations, the references will usually no longer work.
Internet Explorer introduced the very interesting concept of an archived HTML format in 1999 that combines all resources in one file, but sadly, this hasn't yet caught on in terms of global, real-world support in all browsers.
Instead of coding this on your own, you may be able to interface with an existing tool like wget that can do all the grabbing for you. For most programming languages, there are probably related questions on Stack Overflow already on how to best store a HTML page and its resources locally.
You just have to use relative URLs, so the browser will load the external files (images, etc) relatively to the location of the HTML page.
So if your HTML page is saved at file:///some/directory/page.html, if you have a <img src="image.png">, the browser will load this image from file:///some/directory/image.png.
How do I mass upload PDF files or a folder with PDF files and save it into my MySQL database using Servlet?
The best you do is to ask user to specify the files using several <input type="file"...> elements on the page. And on submit check if all the files are PDF or not, perform desired action in either case. Or you can check for the PDF extension right away using JavaScript. You can also validate using AJAX, just send an AJAX request to the server on a onBlur event of the input field.
Otherwise, a privileged applet might be able to help a little more than this. For example asking a directory and scanning for all PDF's etc.