I have a project requirement to render HTML and capture the rendered image as a file on a headless CentOS 5.4 server. My specific requirements are:
1) Input will be a URL to the page to render (or file:// URL to a local HTML file), output will be an image file containing the rendered image of the page
2) CSS must be supported by the renderer up to CSS level-1
3) Static images in the page must be displayed properly in the rendered image, including any transparent color in .GIF or .PNG files
4) Must be able to run on a headless CentOS 5.4 server.
5) Solution must not depend on any product or component that is not free for commercial use
I do not need to do anything with the page other than get the screen capture and save it to an image. The hard part seems to be doing this in a headless environment. Some of the solutions that I've seen discussed need a display in order to work.
Any suggestions?
Thanks in advance,
Jim
SOLUTION:
I ended up using the Standard Widget Toolkit library (www.eclipse.org/swt) for its embedded browser capabilities. This allowed me to programmatically open a window with a browser control in it, render the page, then capture the content of the window to an image file (usually a PNG). The only downside to this approach that cannot be avoided is a "flicker" when the window must be made visible for a moment in order to do the screen capture. I can live with it. The rest was just code to initialize the SWT objects that obtain system resources, a listener to check for a successful completion of the page load, and some code to clean up a hung connection (when the page load never completes for whatever reason).
I got a teammate to play around with xvfb (X virtual framebuffer) on CentOS Linux. The initial tests appear to work, so it looks like the advice from the poster below (who said to try xvfb) may be a viable solution for the headless server part of my issue.
You can start a virtual X-windows environment using xvfb . You can now start a regular browser to render the page and use a screencapture utility to capture the content of the window. It is not pretty, but straghtforward to do.
WebKit is open-source and embeddable, maybe you can use this in a small native app to render on a canvas and save it to disk?
Related
I was just wondering how to take a screenshot of my webpage. Lets say i have an interface around all borders, and in the middle is the frame that shows websites. How would it be possible to take a screenshot of that frame. I would not need any borders and interface included in that snap. Only the frame, how can it be accomplished?
I have included the picture which represents the layout of a website, so no need to include the entire layout but just this segment in the frame. How to capture that frame in an image programmatically? What tools and programming language can do this for me?
PhantomJS is a fully programmable headless browser. It is able to produce PNG, JPEG, GIF and PDF files from the web pages it opens.
Use document.querySelector(), then element.getBoundingClientRect() DOM APIs to get the rectangle containing the element you want to rasterize, then set webpage.clipRect property to it and call webpage.render() method to produce the image. See this answer for an example.
I do not know if you really have no limitations on the technology you wish to use, but phantomJS (headless browser) can certainly be use here. Example: How to render part of a page with PhantomJS
I realise that there are other questions on this topic, all of which I have attempted to implement the answers of and failed. I would like to end up with a full blown answer, ideally with a demonstration, on how to, in Windows and only Windows set the desktop background instantly without having the user log in and out or lock and log back in. The approach that involves using the runtime console to push the registry entries about and then rundll32 user32.dll call UpdatePerUserSystemPreferences has yet to work for me. The desired behaviour is achieved when the user right clicks an image file in explorer and selects 'Set as desktop background'. I'd like to do that programatically even if it devolves to the level of opening a hidden explorer window and right-clicking a file in it as long as the user sees nothing of it. The application in question updates the user's desktop background image with useful system information. The image to be used will be generated and regenerated every five seconds.
Can I change my Windows desktop wallpaper programmatically in Java/Groovy?
So the correct way is with JNA if you are not familiar with JNA or if you haven't used JNA the link above is for you. Otherwise you are right about using natives but you never stated how you are using them so I am just taking shots in the dark here.
Note: I need a solution that uploads images from the System Clipboard specifically, as opposed to uploading files via drag and drop.
What I am looking for is essentially exactly how Google do it in GMail:
Select an image in Word or Paint (for example) and copy it to the clipboard.
Switch to GMail and press CTRL+V in a new mail.
A little "busy icon" appears at the cursor while the image is taken from the clipboard and uploaded to the server.
The image is then displayed in the mail as a standard img element.
I know this is already possible in Chrome, but I need a cross-browser solution and even GMail doesn't support this functionality in IE11 at time of writing this (April 2014).
I've noticed that JIRA (Atlassian) uses a Java Applet for its "Attach Screenshot" functionality but I would rather avoid using an Applet if possible, unless it really is the only option.
Accessing the system clipboard while being cross-browser seems to be a real nightmare and something never done to this day. For java applet you can check Liveconnect but its far from being perfect.
is JavaFX capable of loading and embedding OCX Controls? I would like to use the Internet explorer in my Application, because the WebKit browser lacks support for ActiveX. And since I have to rely on third party ActiveX Controls (I know they are evil), I am bound to the IE.
This question is not purely related to "Can I use OCX in Java" because I would like to have some kind of component for JavaFX I would like to use.
Or do I have to rely on libraries posted here:
Use a .OCX Control in Java
Thanks in advance,
Sven
You have to rely on the libraries posted in the link you supplied in your question.
Or you need to write your own JNI interface.
If you do access OCX components, you may need to place them in their own window rather than sharing a window created by a JavaFX stage as you may run into issues with the JavaFX renderer conflicting writes on the window portions in which the OCX components are displayed - you would have to try it and see what happens to confirm.
The easier way to do this may be to request that users of your application set their default browser to IE, and then call the JavaFX hostservices.showDocument(uri) api to launch an IE page which embeds the OCX components you need.
Another alternative is to:
Embed your JavaFX application as an applet in a web page.
Also embed your activex controls in the web page.
On display of the web page, check the browser and, if it isn't IE, tell they user that the must be using Internet Explorer.
Have the user accept any miscellaneous security warnings the Browser displays.
Have the user (or perhaps JavaScript) resize the browser window to fullscreen (see IE9 Full Screen Mode or Full Screen Browsers with JavaScript).
Communicate between your active X controls and java app using javascript invoked via calls from the javafx/javascript bridge and control your active X controls via VBScript embedded in the page.
It would probably work, but it is such an ugly Frankenstein solution . . .
If you can convert your media to a format which JavaFX supports and just play it back direct through JavaFX, that would be much preferable, or it may also turn out that due to a deep ActiveX integration requirement that JavaFX is not the best solution to your problem and you could be better off with something like Silverlight.
Is it possible to take a screen shot (using the Print Screen) and paste it from the System Clipboard directly into a web pages text area field (which I would create)? Do I need a browser plug-in? Can Flash do this?
**The solution only has to work in Internet Explorer.
This is not possible without a plugin.
It's not possible in Flash or Silverlight either, although it is possible in AIR.
I think you will have to write a custom ActiveX Control in order to do this. The control should automatically take a screenshot and send captured image to server-side code..
There is one more option - create a java applet, which will do the job, and also will help you to avoid cross browser issue.