Java HTML rendering using Cobra - java

I am currently using Cobra: Java HTML Renderer & Parser to render an HTML page that is dynamically generated based on user choices in a java app.
In my app the user has a choice of hundreds of items to select. The items are displayed in the form of special uniquely colored symbols and the user can select more then one item.
Once a number of items are selected their written description is dynamically generated and formatted to include css2 and html4 tags and loaded into the Cobra HTMLPanel for display.
I wish to display the image of the symbol with the written description of an item in the HTMLPanel.
One way to do this would be to save the BufferedImage to a file using ImageIO.write and then include the img html tag in my dynamically generated HTML document that is being loaded into HTMLPanel. Unfortunately this is unacceptable as there may be hundreds of symbols being selected by the user wich in turn would result in hundreds of ImageIO.write calls and an incredible decrease in performance of my app.
An alternate way would be to convert the BufferedImage to a Base64 encoding and then directly place the encoding into the HTML document as follows
<img alt="Embedded Image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />
Unfortunately HTMLPanel appears to ignore the data URI scheme.
Does anyone know a solution?

Use an embedded servlet container like Jetty. Point the URLs to "http://localhost:somePort/imageId", and then serve those URLs up from memory.

Related

How to add a row in a itext PdfPTable dynamically?

I have to create a pdf using itext which will contain a button, when clicked should add a row in an existing PdfPTable. I wrote some code to create a PushbuttonField. While trying to set action I can only find PdfAction.javaScript. I am not able to figure out how to add a row in a table. I tried searching online but all I could find is PdfAction.javaScript
Any help would be greatly appreciated. Thank you.
When you create a PDF file, you draw text, lines and shapes to a canvas. That is also what happens when you add a PdfPTable to a Document. If you look at the syntax of the PDF page, you won't recognize a table. You'll find text (the content of the cells), lines (the borders), and shapes (the backgrounds), but you won't find a table. If the table is distributed over different pages, the "table" on one page won't know that it is related to the "table" on the other page.
Sure, you can add semantic structure to the document by introducing marked content, and by creating a structure tree, but that mechanism which we call Tagged PDF can't be used to make the PDF "editable" the same way a Word document is editable. Tagged PDF is (among others) used to allow assistive technology to present the content to the visually impaired (e.g. in the context of PDF/UA). The presence of structure doesn't change the fact that all text, all lines, and all shapes are added at absolute positions.
This is very different from HTML where the position on a page of a <table>, <tr>, <th>, or <td> is calculated at the moment the page is rendered. In HTML this position can even change when you resize the browser window.
There is no such thing in PDF (except if you use XFA (*), a technology that is deprecated since ISO 32000-2). All content on a page has a fixed position, hardcoded into the page's content stream. Changing the size of the PDF viewer window won't change anything to the position of the page content.
Because of all of this, your question is invalid. It is impossible to create a button in PDF that adds a row to a table, because:
In many cases there is no table: there is just a bunch of text, lines, and shapes at absolute positions,
Even if there is the notion of a table (using Tagged PDF): the visual represenation of that table is fixed at creation time, it can't be changed at consumption time.
You want to use an ordinary PDF viewer as if it were a PDF editor. That is impossible for all the reasons listed above.
(*) XFA was deprecated for different reasons. One of the most important reasons it is the lack of support for XFA. There aren't many viewers that support XFA. If you would post a follow-up question asking *"How can I create an XFA document?", the answer would be: "Don't do this!" Creating XFA is extremely complex, and once you've succeeded in creating an XFA form, you'll discover that many of your customers won't be able to consume the file because their viewer doesn't support the format.

Do I have to physically save image in order to display it in JSP?

I have an image on top of which I need to add different text based on url parameter. I'm planning to do this with Graphics2D/BufferedImage.
The question is, do I have to save image to disk in order to show it in JSP to the user? The purpose of the image is only be displayed and I'd rather not to save thousands of the images that will be generated...
Thanks!
You do not have to save the image to disk. You can use a bare-bones vanilla servlet to generate the BufferedImage, and then use ImageIO.write() to write that image to the response.getOutputStream(). Just make sure you set the headers to the correct content-type. Then, in the HTML generated by the JSP, just use the URL of the servlet as the src for the img tag.
You can base64 encode the image and chuck it onto the page. Browsers won't cache it, though.
Embedding Base64 Images
https://en.wikipedia.org/wiki/Data_URI_scheme#Web_browser_support

Fetching correct news image - JAVA

I am trying to make a small news crawler.
I got every thing working after many tries.
Problem is that approx every HTML news page have more then 50 images.
Many of them are too small. So, i am filtering them simply by checking size.
Only images lager them 200x200 will be taken.
But there are many images on a single page which are large.
and some news articles not have any related image.
Lets take a example -
Link to News - http://timesofindia.indiatimes.com/india/Over-9-3-lakh-TB-patients-in-India-undetected-Report/articleshow/24600851.cms
My code got this image - Image no. 0 http://timesofindia.indiatimes.com/photo/10905539.cms
Image height - 300
Image width - 450
But this image is useless to image topic.
In simple words "How to get correct image dynamically"
I do not want to make code for each website.
A blank image is better then a wrong image.
I would recommend an approach where you identify the proximity of an image based on its position.. so, if an Image comes inside the article its probably an image about the article itself (except for ads which are very wide).
you can findout the source of the image and decide if it should interest you or not. for instance ad images usually come from a different server which doesn't belong to the site. (in your case indiatimes.com).
Consider the alt text. The alt text usually contains either the title completely or some words from the title.
Also, the article does not have any relevant image associated with the title.
I also suggest JSoup:
jsoup: Java HTML Parser
jsoup is a Java library for working with real-world HTML. It provides
a very convenient API for extracting and manipulating data, using the
best of DOM, CSS, and jquery-like methods.
jsoup implements the WHATWG HTML5 specification, and parses HTML to
the same DOM as modern browsers do.
scrape and parse HTML from a URL, file, or string
find and extract data, using DOM traversal or CSS selectors
manipulate the HTML elements, attributes, and text
clean user-submitted content against a safe white-list, to prevent XSS attacks
output tidy HTML

How to test automatically a generated bar code within a PDF Form without a physical scanner?

We have a java application which generates PDF forms which have at the footer 2 dimensional bar codes. The bar codes contain the companies form id and the customer id.
Now we are looking for a way to test the generated PDF automatically without a physical scanner. Means we just like to read the PDF via Java API, render & capture somehow internally the bar codes and read their content. So this is the rough idea.
The question if this approach is achievable? Does anybody know some APIs in Java which may able to do such a thing? e.g.
reading a PDF and rendering its content as an image in the background?
reading a bar code image and extracts its conent?
processing an image and extract text content (OCR)?

When saving an image as BLOB how to display it in JSP with text?

I have done a lot of googling but I have not been able to find a concrete answer.
I am using Spring MVC 3 to save a user image to the database. I am now successfully able to do that. I am saving the image as a BLOB.
I can retrieve the image as a byte[].
Irregardless of file type - jpg, png, gif etc (My image upload is image file type agnostic) I would like to allow jpg and gif and png lets say to render i.e. my display technology should not be hard-coded to display just one type of image, say jpg, it should be able to display all images as they were uploaded in their respective types - that is the requirement.
NOW, I would like to do 2 things
re-size the image if I have to. ie.
200 by 200
render the image in a JSP WITH text.
This is a user profile page so I
need to have both text and image
shown.
How can spring mvc render the image WITH text?
I understand from my research that you can use a BufferedImage type for the jsp? but my problem is that it seems that you can only use that if the content type is strictly image/jpeg, image/gif.
I have come across some links for resizing:
http://forum.springsource.org/archiv...p/t-46021.html
any suggestions welcome if these work BUT ultimately I need to display the image.
Please pass your thoughts along.
Thank you.
Just create a servlet which streams the image from the DB to the outputstream of the response. Then you can just call it the usual HTML way as follows:
<p>
<img src="imageservlet/${bean.imageId}" />
${bean.text}
</p>
As you see, you just display the text next to the image in HTML. You cannot mix them in a single HTTP response anyway. Images counts as separate HTTP requests. For more detail and a kickoff code example of such a servlet, check this answer.
As to resizing, checkout the Java 2D API.

Categories

Resources