I am using iText to generate PDF invoices for a J2EE web application and included on the page is an image read from a URL constructed from the request URL. In the development and test environments this works fine, but in production I get a java.io.IOException: is not a recognized imageformat.
If I paste the url into my browser then the correct image is returned, however the request is redirected from http to https. In my code if I hard code the redirect URL then the image is displayed correctly.
So it seems that when retrieving the image using com.lowagie.text.Image.getInstance(URL), the redirects on the URL are not being followed. How can I output an image from a redirected URL using iText?
Well,
If you ask for an image from a URL, it must actually point to the image. If the URL points to a web page that then redirects to another URL (or the return code from the URL is a redirection), then it is going to fail.
This is essentially due to the getInstance() method understanding how to use the HTTP location protocol to get a file, but not understanding the HTTP protocol enough to be a HTTP client.
You could just use the 'https' address, or you could store the image with your program and locate the as CFreiner suggests. If neither of these options are feasible, then your only real solution is to implement code to query the URL, check if it is a redirection and if it is follow the redirection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With