To print a widget or an entire page with GWT, you can use JSNI and the JavaScript print function.
A good practice is to put all the content you want to print inside a DIV tag, for instance :
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder" xmlns:g="urn:import:com.google.gwt.user.client.ui" xmlns:p1="urn:import:com.mycompany.gwtproject.client.common.ui.composite">
<ui:style>
/* Add CSS here. See the GWT docs on UI Binder for more details */
.important {
font-weight: bold;
}
</ui:style>
...
<div id="printAll">
<g:Label text="Some title" styleName="pageTitle" />
<div style="height:30px"></div>
<p1:DetailAgency ui:field="detailAgency " />
<div style="height:30px"></div>
<p1:DetailCountry ui:field="detailCountry" />
<div style="height:15px"></div>
</div>
<div style="float:left;margin-right:20px">
<g:Button text="Print" ui:field="printBtn" />
</div>
An id is assigned to that DIV tag (line 10) and a button is added to the page. It calls the Java print method from the Print class:
import com.mycompany.gwtproject.client.service.print.Print;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
...
public class MyViewImpl extends Composite implements MyView {
...
@UiField
Button printBtn;
@UiHandler("printBtn")
void onPrintBtnClick(ClickEvent event) {
Element element = DOM.getElementById("printAll");
Print.it(element);
}
This class Print.java contains several methods it that help you print the window, a widget, etc. In the end, they call the JavaScript print() function :
...
public static native void printFrame() /*-{
var frame = $doc.getElementById('__printingFrame');
frame = frame.contentWindow;
frame.focus();
frame.print();
}-*/;
...
You can apply CSS to the printed page. If you work with uiBinder and want to print a widget that has no id defined in the XML template, you can assign one in the corresponding Java class :
... <g:VerticalPanel ui:field="myDetails"> <p1:DetailAgency ui:field="detailAgency " /> <p1:DetailCountry ui:field="detailCountry " /> </g:VerticalPanel> ...
import com.mycompany.gwtproject.client.service.print.Print;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
...
public class MyView2Impl extends Composite implements MyView2 {
@UiField
VerticalPanel myDetails;
@UiHandler("printBtn")
void onPrintBtnClick(ClickEvent event) {
printBtn.getElement().setId("details");
Element element = DOM.getElementById("details");
Print.it(element);
}
This will fire the print window :

Links :
http://code.google.com/p/gwt-print-it/source/browse/trunk/src/br/com/freller/tool/client/Print.java