-1

we have admin dashboard build in gwt and deployed on google app engine for java. On the dashboard there is a feature called "my card" where a blood donor can see his blood donor registration card with us.

Currently, we are creating and storing this card on google storage and when someone goes to "My Card" we render the card using iFrame in our dashboard.

We want to give the ability to print this card. Please tell how to do it?

just to add on i tried Print.it jar but seems like it is obsolete and does not play nice with gwt anymore

Vik
  • 8,721
  • 27
  • 83
  • 168
  • From a mobile device or a standard browser? GWT renders as HTML, so the browser's default print functionality will print it as it is shown. No extra libraries needed. – Roddy of the Frozen Peas Jul 25 '12 at 04:48
  • from standard browser... well i dont want the whole page to be printed... only the html rendered in the iframe in user dashboard. – Vik Jul 25 '12 at 05:03
  • See if this helps you http://stackoverflow.com/questions/472951/how-do-i-print-an-iframe-from-javascript-in-safari-chrome/473270#473270 Idea should be easily translatable to GWT. – krishnakumarp Jul 25 '12 at 05:40
  • 1
    Then why don't you generate a new popup window, render your GWT there, and let the user print (via browser) the popup? Just for that one screen of course, don't need to move app functionality there. Similar to what happens if you look for a "print version" of an online newspaper article. – Roddy of the Frozen Peas Jul 25 '12 at 08:34

2 Answers2

3

Add this script to the iframe content page's tag

<script type="text/javascript">
function printPage() {focus();print(); }
</script>

Add this native method to your GWT class

public native void printIframeContent(String id)/*-{
    var iframe = $doc.getElementById(id);
    var ifWin = iframe.contentWindow || iframe;
    iframe.focus();
    ifWin.printPage();
    return false;
}-*/;

The action handler for print button's click event.

public void onClick(ClickEvent event) {
    printIframeContent("printiframe"); // Use the correct id for your iframe
}

Code is derived from this discussion

Community
  • 1
  • 1
krishnakumarp
  • 8,967
  • 3
  • 49
  • 55
  • i created the frame in my gwt module as Frame frame = new Frame(); frame.setUrl("http://commondatastorage.googleapis.com/bloodcard/160001_25-Jul-2012"); but when i call the method as u suggested as printIframeContent(frame.getElement().getId()); it seems to be returning null. so what is the right way to pass the id? – Vik Jul 25 '12 at 21:10
  • 1
    set the id on the Frame like Frame frame = new Frame("http://commondatastorage.googleapis.com/bloodcard/160001_25-Jul-2012"); frame.getElement().setId("printiframe"); – krishnakumarp Jul 26 '12 at 06:24
  • i did this but now i get exception at java.lang.Thread.run(Thread.java:722) Caused by: com.google.gwt.core.client.JavaScriptException: (TypeError): Property 'printPage' of object [object Window] is not a function – Vik Jul 26 '12 at 18:48
0

Here is a simple Printer class for GWT. It prints out the page.

import com.google.gwt.user.client.ui.UIObject;

import com.google.gwt.user.client.Element;

public class Printer {

public static native void it(String html) /*-{
    var frame = $doc.getElementById('__printingFrame');
    if (!frame) {
        $wnd.alert("Error: Can't find printing frame.");
        return;
    }
    frame = frame.contentWindow;
    var doc = frame.document;
    doc.open();
    doc.write(html);
    doc.close();
    frame.focus();
    frame.print();
}-*/;
public static void it(UIObject obj) {
    it("", obj.getElement().toString());
}
public static void it(Element element) {
    it("", element.toString());
}
public static void it(String style, String it) {
    it("<it><header>"+style+"</header><body>"+it+"</body></it>");
}
public static void it(String style, UIObject obj) {
    it(style, obj.getElement().toString());
}
public static void it(String style, Element element) {
    it(style, element.toString());
}

}

Burhan ARAS
  • 2,517
  • 25
  • 19