Rendering an HTML page to thumbnail
Is it possible to render out an HTML page to create a thumbnail?
I'm trying to write a web app that can give a live thumbnail view of a page but am not sure where to start.
Thhe thumbnail just needs to be rendered to a GIF or JPEG image but I want to be able to render on the fly.
Anyone been able to do this or know of any leads that could get me started?
Anthony
[396 byte] By [
[tone]a] at [2007-10-2 12:41:23]

It sounds like a fool's errand to me. Considering display of HTML is dynamic (for example, depending upon screen size and resolution, as well as the browser and the settings on the user end) there is really no standard image of a given web page to turn into a thumbnail.
Then when you have finally figured out a way to do it, the thumbnails are going to be meaningless. The text is obviously not going to be readable and all but the largest images will be inscrutable. What purpose would it serve?
In terms of how to do it, I think you would have to parse the HTML, write all the stuff to a Graphics object (or JPanel or Image or whatever... never worked with drawing graphics myself) off-screen, perform a size transformation on it, and save it to file. So actually only the HTML parsing part would be a pain. There should be a tool already out there that does that.
It is going to require substantial coding.
Drake
Ok sure. First there are two ways you could try to render the page. You could try using JEditorPane which IMO is not anywhere near professional grade or you could use the JDIC webbrowser component. If you do the former you can just create a bufferedImage for your thumbnail, get the graphics object from it, scale it down (using graphics2D.scale) and call editorpane.paint with it. If you choose the latter, you'll need to use the java.awt.Robot to screen scrape the web browser. Then you can scale that down like above
Fools errand? Hmmm sorry you seem to have misunderstood my use of the thumbnails.
I'm trying to put together a CMS of web sites. In order to do so I want to be able to have a photo album style view of pages that allow a clickable link to the pages.
They are not used for reading but identifying as well as setting up an album of site templates.
1) A user accesses the main site editorial page and it lists all the pages by name, alongside which is a thumbnail of the page
2) A user can click on the link (or thumbnail) to go the editable mode of the page.
3) They can swap the template used by bringing up an album of templates whhich they can scroll through and get a larger view (obviously just a HTML view) by clicking on the thumbnail.
Still sound like a fools errand?
Well, if we are just dealing with templates rather than actual content, the approaches described thus far won't work. What are we talking about exactly? Actual pages, or just framework HTML set up for someone to plug their own content in?Drake
See this:
http://weblogs.mozillazine.org/roc/archives/2005/05/rendering_web_p.html
I want to do something similar in Java.
It will be scheduled once every so often (not consistently there is no point).
I've gathered so far I need to render the HTML to a graphics component (possibly using JRex) then get the painted component and resize the image to the required size and save it.
Thanks tjacobs01, I'll give it a try and see if it yields the results I need.
Here is the way i am used in my project(CMS project). But it is impl in the client with filter.
I use iframe tag to load the page , then apply filtering on the iframe:
<style>
.previewFrame{cursor:hand; width:800px;border:8px solid #ccc;height:600px;filter:progid:DXImageTransform.Microsoft.Matrix(M11=0.25,M22=0.25,SizingMethod='auto expand');}
</style>
<iframe scrolling="no" frameborder="0" src="page.html" class="previewFrame"></iframe>
But the effect is not so pretty.
shamphone's approach is quite brilliant!
It has numerous advantages (such as consistency with the user's browser, making the client machine do all the work, etc.) and only a few disadvantages (IE-specific, a bit fragile, etc.)
I thought I would point out you can fix the 'not so pretty' problem simply by applying two filters instead of one...
<style>
.previewFrame{cursor:hand; width:800px;border:8px solid #ccc;height:600px;filter:progid:DXImageTransform.Microsoft.Matrix(M11=0.6,M22=0.6) progid:DXImageTransform.Microsoft.Matrix(M11=0.75,M22=0.75);}
</style>
<iframe scrolling="no" frameborder="0" src="page.html" class="previewFrame"></iframe>
...the Microsoft.Matrix uses bilinear resampling by default, and you shouldn't try to bilinear sample more than 0.6 or so in any one filter.
The code above produces a lovely, anti-aliased thumbnail.