How to render image within an html dynamically? - java

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

Related

How to show JSON volley request reponse's object (in HTML) form in textView?

I have a JSON response in which object values are in HTML form. In which i have a Paragraph, a img src and video src link. How can i parse or show the image and video onto the Activity. Any help would be appreciated.
Response I'm receiving from server(only showing one object here)
{
"description":"<p>The Sarawak Ministry of Modernisation of Agriculture</p>\n
<p><video src=\"https://myurl.biz/media/media/Nokia_X3_sample_video.3gp.3gp\" autoplay=\"autoplay\" controls=\"controls\" width=\"300\" height=\"150\">
<span id=\"mce_marker\" data-mce-type=\"bookmark\">â</span></video>
</p>\n<p><img src=\"https://myurl.biz/media/media/bird_SJhLHPo.jpg\" width=\"547\" height=\"264\" /></p>"
}
I have tried :
txtDetails.setText( Html.fromHtml(extras.getString("details"));
Actual Result I'm receiving in my text view:
The Sarawak Ministry of Modernisation of Agriculture,
​
What you're trying to do is impossible. You can't display an image/video in a TextView (this is not a browser). First, you need to parse the HTML. Then, once you have the components you can plug them in separate views. For example,
You parse the HTML (there are a lot of frameworks for that)
Get the image URL link and use Picasso (popular framework for displaying images from URLs) to display it on your Activity.
Get the paragraph and display it in a TextView
etc..

JavaFX HTML Editor display raw HTML code on the editor

I was wondering if there's a way to load an HTML file on JavaFx's HTMLEditor but rather then loading it as a web page, show the actual raw source code on the HTMLEditor. Is this possible with JavaFX's HTMLEditor?
I tried the following:
File f = fc.showOpenDialog(null); //This allows a user to find a file in a dialog
String textRead = readFile(f); //store the files content in a string
htmlFileEditor.setHtmlText(textRead); //displays the source but not as raw HTML
This only loads the html file and displays it, Im wondering if there is a way to show the raw source code of the HTML file loaded with tags and all on the HTMLEditor.

How to get image from local directory on a pdf created with ITextRenderer?

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.

Have PDF appear in HTML rather than loading with Adobe or into separate window

I have a java web application that in theory, when working correctly, will display a PDF in the browser window next to some input text boxes so that an employee could index the document and store it away.
JSP URL: localhost:1234/Application
What is currently happening is this:
->User tells JSP to tell servlet to go into filesystem and grab a batch of PDFs
-->JSP displays which batch(folder) was grabbed, and the files contained inside the folder to the screen
EXAMPLE:
Folder1:
-document1 (button)
-document2 (button)
-document3 (button)
--->Person clicks on button that appears next to document name on the JSP and the servlet serves up the PDF data in a byte array as follows:
File file = new File(Dir + "\\" + batchName + "\\" + fileName);
byte[] by = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(by);
fis.close();
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment; filename=TheDocument." + "pdf");
response.getOutputStream().write(by);
response.getOutputStream().flush();
response.getOutputStream().close();
The PDF then opens in a SEPARATE WINDOW than what the application is running on, and the PDF file is downloaded to the machine into the downloads folder
What happens after this does not matter in the scope of this question.
What I WANT to happen is this:
User tells JSP to tell servlet to go into filesystem and grab a batch of PDFs
JSP displays which batch(folder) was grabbed, and the files contained inside the folder to the screen
Person clicks on button next to document name in the JSP and the servlet serves up the PDF document and loads it into the html page so that the person can view it in real time on the same window and do their indexing.
Changing the header to inline does not give me the desired effect as it loads the PDF in the same browser window, but only by clicking back can I get back to my JSP. It essentially loads up a whole new HTML/JSP window. The browser actually points right to my servlet in this case:
localhost:1234/Application/Servlet
Below is what I want the application to look like:
I've looked into several jquery plugins such as PDFObject, Colorbox and many others. I do not seem to get the desired effect that I want.
You could use pdf.js to render the pdf, with a Flash fallback for browser that aren't supported by pdf.js.
The helloworld example renders a pdf in a <canvas> element.
Whether a PDF opens in a separate application or in the browser is a setting in the browser, in Adobe, and in Windows Explorer (assuming Windows). You have no control over it, short of displaying your PDF in a Flash app, and then the user might not have Flash.
Not sure if this is what you are looking for but typically you can put a PDF in an iframe and have it opened up that way. The PDF just needs to be published on your server.
This answer may help you:
How to open a PDF file in an <iframe>?

Getting a broken image icon when printing a JEditorPane

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" />

Categories

Resources