I need to send to the client a byte[] with a pdf data from my tomcat server.
I'm using this:
response.setContentType("application/pdf");
response.setHeader("Content-Disposition:","inline; filename=test.pdf");
But (at least) with firefox I get a file download instead of inline display.
The only way to show pdf data inline is to remove the Content-Disposition header record however, if I do so I cannot set the filename, the pdf name is get from the last folder of url.
You seems to be setting the right headers. But rendering of pdf or another such formats depends on the browser capabilities as well. I mean browser need to have a pdf plugin installed in order to render a pdf when it sees the same in the contentType header field. So make sure you install a pdf plugin for your firefox and try to test after that. You can download firefox pdf plugin from here:
https://addons.mozilla.org/en-US/firefox/addon/pdf-download/
Related
Is there a posability in Vaadin 7 or in Java generally, to check if a browser has an embedded pdfreader or not?
I need to know that because it depends on that how i open the pdf.
There is unfortunately no way to consistently check if the browser supports viewing PDF files or not. I would recommend using something like PDF.JS (https://github.com/mozilla/pdf.js) or FlexPaper (http://flexpaper.devaldi.com/products.jsp) on your web site to display your documents to make sure your visitors can see your documents
Both those options are available as open source
In a web application, Java (and per se, Vaadin) runs on server side, so you cannot know which technology is installed on client, in this case, the browser. Just fire your file download with the application/pdf mime header and let the client do it's work. If you want to fire it as a general file download, use application/octet-stream mime header instead.
Here's a more generic q/a on this topic: How to determine if the user's browser can view PDF files
I wanted to render image on PDF which is going to generated by FOP. To render image I am using tag <fo:external-graphic> As below :
<fo:external-graphic src="url('../offlinePaper/displayImage?disImg=4bec89f0-5b97-40c3-b7c9-ac555a664df8')" inline-progression-dimension.maximum="100%" content-height="scale-down-to-fit" content-width="scale-down-to-fit"> </fo:external-graphic>
I also tried by giving full URL as:
<fo:external-graphic src="http://10.2.10.79/Web/offlinePaper/displayImage?disImg=e391d672-ebf4-44d8-86cb-2cf987a50bf7" inline-progression-dimension.maximum="100%" content-height="scale-down-to-fit" content-width="scale-down-to-fit"> </fo:external-graphic>
In controller I am having an request mapping (../offlinePaper/displayImage) which takes image name disImg from getParameter and decrypt the image and return in OutputStream.
But when I generate PDF I found Error in log file as:
Image not available. URI: http://10.2.10.79:80/Web/offlinePaper/displayImage?disImg=4bec89f0-5b97-40c3-b7c9-ac555a664df8. Reason: org.apache.xmlgraphics.image.loader.ImageException: The file format is not supported. No ImagePreloader found for http://10.2.10.79:80/Web/offlinePaper/displayImage?disImg=4bec89f0-5b97-40c3-b7c9-ac555a664df8 (No context info available)
But if I copy this URL and paste in URL then image get shown in browser and I also found that when I generate PDF the request mapping which is suppose to get call for image decryption is not getting called.
Update
Also a strange think I come to know that with same code PDF get successfully generated if web server is Tomcat but if I deploy application on Glassfish it's giving error.
In the exception trace stands:
ImageException: The file format is not supported
The FO seems to be able to find an ImagePreloader according to file extension. If it doesn't find any, it breaks.
I'd propose to change the way how the url is built. There should be a real image file name included.
So, instead of:
../offlinePaper/displayImage?disImg=4bec89f0-5b97-40c3-b7c9-ac555a664df8
try
../offlinePaper/displayImage/disImg4bec89f0-5b97-40c3-b7c9-ac555a664df8.jpg
I've got a smartgwt application which create a link with a jpg/gif/png/pdf files. This files are shown in browser. I want to get the save dialog instead it which ask me the path when I want to save the file at local machine. How could I do that?
As I know, you have to change the response header by setting the Content-disposition to attachment. Like this:
'Content-disposition: attachment; filename=image.jpg'
'Content-type: image/jpeg'
With these the browser will understand most cases that it should show up a dialog to save the image with the name: image.jpg. Also it might offer you to send it directly to an application, for example to an image viewer.
To get it work from a simple link, perhaps you have to write a servlet which will return the requested file with the correct headers and call that servlet from every link with a parameter to the real file.
I use control named WebEngine (javafx) to show mail content downloaded by javamail. It's fine when the content is plain text or html with image src=http link, but when mail has embedded images (has content id in src) control dont show this images (of cource because has no idea where is it). What is the best control to show email? How to do it, when image storing in the memory. Maybe load mail in WebEngine is not a good idea...
You need a URLStreamHandler that handles the "cid" URL protocol and supplies the data from the appropriate part of the MIME message. I believe you need to configure the URLStreamHandler by creating a URLStreamHandlerFactory and calling the URL.setURLStreamHandlerFactory method. The trick, as I remember, is that there's no way to get the default URLStreamHandlerFactory so that your version can delegate to the default version for all the other URL protocols.
Alternatively, you can save all the attachments to disk and then process the html content before it's displayed to convert all cid: references into file: references.
My guess is that WebEngine isn't decoding and displaying multipart mime encoded messages such as what you are trying to process in your incoming mail. If I recall correctly some browsers like IE didn't handle multipart mime encoded messages either (and maybe still don't).
What you can do is decode the multipart message yourself using something like Mime4J and then display the decoded content parts in controls (or external apps of necessary) based on the decoded mimetype you get from the multipart message.
WebView will be able to display a bunch of mime types like text/html,
text/plain, image/jpeg, etc.
pdf can be displayed with JPedalFX PDF Viewer.
text/plain could be rendered by Label.
the various image/* mime types could be rendered by ImageView.
word docs and other formats not natively supported by any JavaFX controls could be written to a file and launched via HostServices.showDocument or DeskTop.open.
I am downloading png files to a bimap.
bitmap = BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream());
It works perfectly for "static"ยท images,images stored in a server with his .png file extension
But if the image is a jsp script response nothing is downloaded into the bitmap
The url of the script
http://xxxxxxxxxxxxxx:9000/xxxxxxx/jsp/ios/imageAlumno?X_ALUMNO=144244
The image is displayed properly in a browser and the html code of the response is:
<html><body style="margin: 0px;"><img style="-webkit-user-select: none" src="http://XXXXXXXXXXXXXXX:9000/XXXXXXX/jsp/ios/imageAlumno?X_ALUMNO=144244"></body></html>
Any idea?
First guess is: the JSP code sends wrong image header. Java is probably less permissive than a web browser when it comes to certain rules... I would first check what header is sent back to the user. Use the telnet XXXXXXXXXXXXXXX:9000, and then type in the GET /XXXXXXX/jsp/ios/imageAlumno?X_ALUMNO=144244 to pretend you are a browser, and you will see what the web server sends back.