how to convert html page to image using java or php - java

I want to know how to convert html file to image. How do I do this?

You can checkout the source code for the popular BrowserShots service,
http://browsershots.org/

If you're running Windows, and have the GD library installed, you can use imagegrabwindow. I've never used it myself, but as always, the PHP site has lots of documentation and examples.

Use:
WKHTMLTOPDF.
It also has binding to PHP, or you can run it yourself from command line.

Problem is that you need to implement all the functionality of a browser and an HTTP stack (and this still does not deal with the case where the content is modified using javascript).
As John McCollum says, if you've got the website open in a browser on your PC, then you can use imagegrabwindow or snapsIE (MSIE only)
If you want to to be able to get a snapshot using code only, then you might want to look at one of the off the shelf solutions - AFAIK there are several programs (at least 2 of which are called html2pdf) which will generate a PDF of static html - and its relatively easy using standard tools to trim this to window size and convert to an image file.
e.g. https://metacpan.org/pod/distribution/PDF-FromHTML/script/html2pdf.pl

Related

Java: how to use xulrunner to convert html to postscript?

I need write a service that receives labels in html format via webservice(Endpoint) in a java SE application and print the same in an automated fashion by controlling the printing and return status (ie, if the document was printed successfully or not).
The problem is I need to convert the html to a print format that the printer(Zebra) accepts, (ie, postscript or image). Is this possible with xulrunner or otherwise (without ZPL Layout)?
Thank you!
You could try integrating PhantomJS with Java using GhostDriver:
https://github.com/detro/ghostdriver
Specifically, see the Java code here:
https://github.com/detro/ghostdriver/blob/master/binding/java/src/main/java/org/openqa/selenium/phantomjs/PhantomJSDriver.java
This library is available in Maven, so integration might be easy. You can get a screenshot with the getScreenshotAs() function.
Alternatively, see Embedding XULRunner application on Java if you want to give XULRunner a shot.

Java: How to send html content using java.awt.Desktop

I'm developing a console Java application(Not swing) using java.awt.Desktop class, which will launch the browser with something like this;
Desktop.getDesktop().browse(URI.create("http://www.google.com"));
This works, but actually what i want is not to give an absolute URL but to display a string with HTML content which I have builded in the code. Can I directly do do this without saving my content as a html page and then calling again?
String myHtmlstring="<body>.."
For this to work, you would need an browser, which can be started with the html-content as parameter.
firefox -code "<html><head><title>demo</title></head><body>..."
If you look at the manpages for firefox, lynx and opera, you won't find such an option (I didn't). But theoretically, it would be possible.
Since html-pages are normally some kb big, using the parameters would be very unhandy, because html often contains quotes and apostrophes, which would need masking. Therefore, if a passing of content would be possible, I would expect it as reading from stdin like so:
cat demo.html | browser
However, I don't know a browser which supports this.
Summary: No, it's not possible with today browsers, but in principle it would be possible. Going with temporary files would be the solution, you're already aware off.
Its not particularly clear what you're asking for though, what do you mean when you say, "display my own html string?" Do you want to generate html and then have the browser display that?
JavaFX 2.0 comes with a webkit component, and you can embed that in your swing application and do with it what you will. Its officially released for Windows and in [beta|preview|alpha] for max/linux. So if you don't require support for multiple platforms right now, its probably a good choice.

turn a web page into an image with ColdFusion, Java or command line utility?

Native ancient solution in ColdFusion that used to work with HTML 3.x...
<cfhttp url="#targetUrl#" resolveurl="yes">
<cfdocument format="pdf" name="pdfVar">
#cfhttp.filecontent#
</cfdocument>
<cfpdf action="thumbnail" source="#pdfVar#" pages="1" destination="image">
<cfimage action="writeToBrowser" source="#image#">
Super slow, even with cache, many CSS styles missing or broken.
Any good server-side solution to capture a rendered webpage into a thumbnail? like service provided by http://www.shrinktheweb.com/ ?
Any ColdFusion, Java or Command line utility solution?
This website has a script that does what I think your looking for, I haven’t tried using it for any server-side project though.
http://khtml2png.sourceforge.net/
Doesn’t make thumbnails though, but you could render the image created with cfimage.
If you have ColdFusion version 8 or better, you can simply use CFDOCUMENT to create a thumbnail.
From Ray's post:
<cfdocument src="http://www.coldfusionjedi.com" name="pdfdata" format="pdf" />
<cfpdf source="pdfdata" pages="1" action="thumbnail" destination="." format="jpg" overwrite="true" resolution="high" scale="25">
We ended up using SiteShoter which uses IE as the renderering engine. http://www.nirsoft.net/utils/web_site_screenshot.html

How to use wkhtmltopdf in Java web application?

I am newbie in wkhtmltopdf. I am wondering how to use wkhtmltopdf with my Dynamic Web Project in Eclipse? How to integrate wkhtmltopdf with my Java dynamic web application?
Is there any tutorials available for beginners of wkhtmltopdf ?
(Basically, I would like to use wkhtmltopdf in my web application so that when user click a save button , the current page will be saved to PDF file).
First, a technical note: Because you want to use wkhtmltopdf in a web project, if and when you deploy to a Linux server machine that you access via ssh (i.e. over the network), you will need to either use the patched Qt version, or run an X server, e.g. the dummy X server xvfb. (I don't know what happens if you deploy to a server running an operating system other than Linux.)
Second, it should be really quite simple to use wkhtmltopdf from any language in a web project.
If you just want to save the server-generated version of the current page, i.e. without any changes which might have been made like the user filling on forms, or Javascript adding new DOM elements, you just need to have an extra optional argument like ?generate=pdf on the end of your URL, which will cause that page to be generated as a PDF, and then the PDF button will link to that URL. This may be a lot of work to add to each page manually if you are just using simple JSP or something, but depending on which web framework you are using, the web framework may offer some help to implement the same action on every page, if you need to implement that.
To implement this approach, you would probably want to capture the response by wrapping the response object and overridding its getWriter() and getOutputStream() methods.
Another approach is to have a button "submit and generate PDF" which will generate the next page as a PDF. This might make more sense if you have a form the user needs to fill in - I don't know. It's a design decision really.
A third approach is to use Javascript to upload the current state of the page back to the server, and process that using wkhtmltopdf. This will work on any page. (This can even be used on any site, not just yours, if you make it a bookmarklet. Just an idea that occurred to me - it may not be a good idea.)
A fourth approach is, because wkhtmltopdf can fetch URLs, to pass the URL of your page instead of the contents of the page (which will only work if the request was a HTTP GET, or if it's equivalent to a HTTP GET on the same URL). This has some small amount of overhead over capturing your own response output, but it will probably be negligible. You will also very likely need to copy the cookie(s) into a cookie jar with this approach, since presumably your user might be logged in or have an implicit session.
So as you can see there are quite a lot of choices!
Now, the question remains: when your server has the necessary HTML, from any of the above approaches, how to feed it into wkhtmltopdf? This is pretty simple. You will need to spawn an external process using either Runtime.getRuntime().exec(), or the newer API called ProcessBuilder - see http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html for a comparison. If you are smart about it you should be able to do this without needing to create any temporary files.
One of the wkhtmltopdf websites is currently down, but the main README is available here, which explains the command line arguments.
This is merely an outline answer which gives some pointers. If you need more details, let us know what specifically you need to know.
Additional info:
If you do end up trying to call wkhtmltopdf in an external process from java (or for that matter, any language), please note that the "normal" output that you see when using wkhtmltopdf from the command line (i.e. what you would expect to see in STDOUT) is not not in STDOUT but in STDERR. I raised this issue in the project page
http://code.google.com/p/wkhtmltopdf/issues/detail?id=825
and was replied that this is by design because wkhtmltopdf supports giving the actual pdf output in STDOUT. Please see the link for more details and java code.
java-wkhtmltopdf-wrapper provides an easy API for using wkhtmltopdf in Java.
It also works out-of-the-box on a headless server with xvfb.
E.g., on a Ubuntu or Debian server:
aptitude install wkhtmltopdf xvfb
Then in Java:
Pdf pdf = new Pdf();
pdf.addPage("http://www.google.com", PageType.url);
pdf.saveAs("output.pdf");
See the examples on their Github page for more options.

Can I convert a .swf file to an image format?

I need to take a .swf Flash file, ideally from a URL (but I can read the file from disk also) and create an image preview of it (png, gif or jpeg is fine).
I am using Adobe Coldfusion 8 so I'm looking for a Java solution. I need to get the first frame of the Flash movie only.
Many thanks in advance.
EDIT: I need to do this on the server in Java/CF at runtime - it's got to be automatic - I'm not looking for screen-grab software! :)
Is this running in a web browser? Or standalone - actually, either works. Simply get it to the point you want, and take a screenshot (Print Screen). That'll giev you an image you can paste into your image app of choice.
IF however you're wanting to code it, you'll need to probably want to try this freeware toolkit:
http://bytescout.com/swftoimage_swf_to_jpg.html
which allows you to take a flash file and convert the first frame to your image type of choice :)
Try swftools. It has utility called swfrender which can export frames into images. And it is open source :).
I'm not an expert but I think you can't without some tricky tools because SWF is a compiled flash program.
UPD: but there are some swf2flv converters, maybe u can use them. It is easier to make a preview from flv format.
UPD 2: I see several answers that recommends you to open it in a browser and make a print screen. If you really want to do this, look at JxBrowser.
AS3 has the capability to store the pixels that represent the entire screen into a BitmapData object, pass that to a JPG/PNG encoder, and post it to the server. There is a document on Adobe's devnet here that explains how the ActionScript works and what is needed with regard to PHP, shouldn't be hard to translate to CF.
here's an .net article:
http://www.codegod.de/WebAppCodeGod/screenshot-of-webpage-with-aspnet-AID398.aspx
basically you create a webpage showing the .swf flash file.
remember to call iecapt.exe including the command line argument --delay=10000 to give the ie activex control 10sec to load the .swf
Probably not what you're looking for but... Alt+Print Screen would work.. :)
More seriously.. find something that can render flash (IE ActiveX control?) and capture the buffer it renders.

Categories

Resources