I have a small applet that is meant to print the html from a given url to form a small receipt. It's all text except for an image at the end (barcode). In my initial tests everything was working when the sample barcode was located in the same directory as the jar file that has my applet's code.
The problem now is that for each url I want to print, the image at the bottom will be unique and generated on the fly (pretty much an encoding of the id).
Something like this:
<img src="/asp/barcode.asp?id=2147419365" />
When I view the html page itself for the generated receipt it all shows up fine, but when I print I lose the image (a broken image icon appears). I have tried specifying a full path in the image source, but it didn't change anything. The strange part is that if I run it off my hard drive with the with all files in the same folder and that dynamic barcode it prints correctly. Once I put it on my localhost it loses the barcode. This is my print method in the applet:
public void printDocument(final String url) throws FileNotFoundException
{
AccessController.doPrivileged(new PrivilegedAction<Object>()
{
#Override
public Object run()
{
try
{
new ParserDelegator();
JEditorPane jEditorPane = new JEditorPane();
jEditorPane.setPage(url);
JeditorRendererer docRenderer = new JeditorRendererer();
docRenderer.print(jEditorPane);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
});
}
Currently, when deployed, the jar file, the receipt generator, and the barcode generator are all in different locations.
<img src="/asp/barcode.asp?id=2147419365" />
You most likely need to use an URL that is relative to the HTML. e.g.
<img src="../asp/barcode.asp?id=2147419365" />
Related
I am using Java and Vaadin to create a simple view where an image is displayed. I have a single Image element containing a source link. Here is my code.
#Route(value = "")
public class MainView extends VerticalLayout {
public MainView() {
add(new Image("https://media.reaperscans.com/file/4SRBHm/comics/14659ab2-5650-4aaf-a5bb-9ad5a9828662/chapters/3aff6399-5ff4-46d1-b502-88788510d962/000.jpg", ""));
}
}
This compiles to a HTML in the browser containing an image element:
<img src="https://media.reaperscans.com/file/4SRBHm/comics/14659ab2-5650-4aaf-a5bb-9ad5a9828662/chapters/3aff6399-5ff4-46d1-b502-88788510d962/000.jpg">
When I open this image in new tab I can see the image correctly, but when I see it in my app there is only:
EDIT:
I can open the image using the link in Google Chrome.
If you open the image on Chrome you will notice the website doesn't allow hotlinking. That's why it doesn't load. Try using a different image (from Wikipedia, for example) and it should work well.
new Image("https://upload.wikimedia.org/wikipedia/commons/8/8e/Vaadin-flow-bakery.png","vaadin");
I recommend using the alt attribute as well, instead of leaving it empty as you had in your original question.
I'm parsing pdf from html with ITextRenderer as follows:
private void createPdf(File file, String content) throws IOException, DocumentException {
OutputStream os = new FileOutputStream(file);
content = tidyUpHTML(content);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(content);
renderer.layout();
renderer.createPDF(os);
os.close();
}
Now, if the html contains a local path to an image, it doesn't show in the pdf. However, if the img tag's src-value is an URL to an image online, it does work.
As follows:
Doesn't show on the pdf:
<img src="C:\path\to\image\image.png" />
Does show correctly on the pdf:
<img src="http://flagpedia.net/data/currency/jpy/100jpy.jpg" />
The path to the local file is correct, as it shows the image if I copy paste that path to my web browser for example.
How to get it to show on the pdf?
UPDATE: This all is running in a JSF / Primefaces Web Application on a WildFly10 Application server. So it seems that direct paths to a file system won't work. Then, which directory in the war should I use to use static resources, such as this image. Currently it is in webapp/resources/images.
Here's how to do it when using JSF2.0 or newer, which has the <h:graphicImage> component.
I have the image myimage.png in the folder webapp/resources/images/.
Now I add the component as follows:
<h:graphicImage library="images" name="myimage.png" />
Now in the browser I inspected that element to get the url and inserted that url as the value of src-attribute in the <img>-tag as follows:
<img src="http://localhost:8080/myproject//javax.faces.resource/myimage.png.xhtml?ln=images"/>
And voila! Now I'm using a static image with an url in the html template, which will be parsed as the said pdf and the image works in the pdf as well. I hope this trick helps someone.
I have a form that allow user to upload an image (jpg), in order to show that image in another page.
This is the method I used:
public void salvaFoto(...) throws FotoUtilException{
...
String path = context.getRealPath("prova/1.jpg");
File file = new File(path);
commonsMultipartFile.transferTo(file);
...
}
It works.
The jpg is saved here: D:\root backup\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\SpringGestioneErrori\prova\1.jpg, that is the url the I get with context.getRealPath("prova/1.jpg")
Now, I have problem printing that image.
I tried to find the image using again context.getRealPath("prova/1.jpg"), this way:
<img src="D:/root backup/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/SpringGestioneErrori/prova/1.jpg">
but it doesn't work.
I tried also:
<img src="/SpringGestioneErrori/prova/1.jpg">
but it didn't work as well.
Thank you
I am using local rootDirecotry instead. This is working fine for me.
String rootDirectory = request.getSession().getServletContext().getRealPath("/");
image.transferTo(new File(rootDirectory+"\images\"+ImageName.jpg"));
Then access it from same directory
<img src="<c:url value="/images/imageName.jpg">
I want to give a image link on my pc to a row on jtextpane.
I give "text/html" ttype to jtextpane
jTextPane1.setContentType("text/html");
and I wrote this code for give image:
html text:
<img src= file:/"+myimageplace+" alt=\"Click to Open Image\" width=\"30\" height=\"30\">
this is working for showing image.
But I want to give that image to go to image like this :
\"<img src= file:/"+mytext+" alt=\"Click to Open Image\" width=\"30\" height=\"30\">
But this isnt working?
How can I do that?
Thanks.
You need to have an event/link handler related to link clicks for this to work.
Even though your rendering HTML, without a specific link handler to handle clicks it will not open the window.
I am quoting from here: Hyperlink in JEditorPane
Add the link handler
By default clicking the links won't do anything; you need a HyperlinkListener to deal with them:
editor.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
// Do something with e.getURL() here
}
}
});
How you launch the browser to handle e.getURL() is up to you. One way if you're using Java 6 and a supported platform is to use the Desktop class:
if(Desktop.isDesktopSupported()) {
Desktop.getDesktop().browse(e.getURL().toURI());
}
I am having some html content with images(html+images) embedded inside it on my local drive. When i open it by double clicking on browser directly, i am able to see the content with embedded images in it.
Now i have to render this content on web browser using java apis.I am able to render html content without any issue but while rendering , image is not getting rendered , instead alt text is getting rendered on browser.
I got to know that i can not set two mime types in java code to render image as well as text on same page.I want to know how can i render html content with image inside it so that it can be rendered.
Regards.
Code added:
public void readFile() throws FileNotFoundException{
String lsFilePath = "D:\\alf3.3\\deployment\\target\\";
//String lsFilename = "/WEB-INF/message.properties";
String lsFilename = "spcontent2.html";
File loNewFile = new File(lsFilePath + lsFilename);
FileInputStream loFileInputStream = new FileInputStream(loNewFile);
FileOutputStream loFilOutputStream = new FileOutputStream(loNewFile);
}
It is fine till now but i need to know how can i render an image that is referred to in file mentioned.
For embedding an Image into html source page by adding content id into image source. This is usually practised while sending HTML emails with embedded images
Please refer following document for more info.
http://www.faqs.org/rfcs/rfc2111.html