Headlessly; Pure Java:
A few Java based HTML renders exist, all with different sets of drawbacks. The most common is the one built in. This is quite simple and can only render fairly basic HTML. The most interesting I know of is The Flying Saucer Project. This can render fairly complex XHTML but you will have to convert HTML before you can use it (JTidy may be able to help here). Taking a Swing component and creating a image is fairly simple, you just pass an BufferedImages graphics object and pass it to the Swing components paint method. Then splat that out with ImageIO.
A big advantage to this would be that the renderer would be headless. The disadvantage is that it would not be a perfect rendering and it would lack any plugin support.
Headlessly; External Application:
PhantomJS (a headless webkit browser) can take screen shots in an automated manor. It will require using ProcessBuilder to fire it off with the correct parameters; but that is quite simple.
String phantomjsHome = "c:\\tools\\phantomjs-1.9.1-windows\\";
String phantomjsRasterizeScript = phantomjsHome + "examples\\rasterize.js";
String url = "http://google.com";
String file = "c:\\code\\google.png";
ProcessBuilder pb = new ProcessBuilder(phantomjsHome + "phantomjs", phantomjsRasterizeScript, url, file);
ProcessBuilder.Redirect error = pb.redirectError();
ProcessBuilder.Redirect out = pb.redirectOutput();
ProcessBuilder.Redirect in = pb.redirectInput();
Process process = pb.start();
process.waitFor();
Like using a Java in-memory browser, it is headless and as it is WebKit based it should be very close to Safari. Alas it does not support plugins.
With a head
The simplest solution would be to make use of Selenium.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
It will require a head (even if it is virtual) but will give a perfect rendering for a given browser (as it will use said browser). The Cloud
Plenty of cloud based solutions also exist if hosting a service locally is an issue.
- Grabz It
- Url2Png
- thumbalizr
- webshotspro
- browsershots
- immediatenet
- shrinktheweb
- Browser Shot
- Snapito
- wIMG
No comments:
Post a Comment