2012-07-25 38 views
-1

我们有管理仪表板内置GWT和部署在谷歌应用程序引擎的Java。仪表板上有一项名为“我的名片”的功能,献血者可以在这里看到他的献血者登记卡。gwt的打印能力

目前,我们正在谷歌存储中创建和存储此卡,当有人转到“我的卡”时,我们会在仪表板中使用iFrame呈现卡。

我们想给予打印此卡的能力。请告诉如何做?

只是加上我试图Print.it罐子,但好像它是过时的,不与GWT玩好了

+0

从移动设备或一标准的浏览器? GWT呈现为HTML,因此浏览器的默认打印功能将按照显示的那样打印出来。不需要额外的库。 – 2012-07-25 04:48:43

+0

从标准浏览器......以及我不希望整个页面被打印......只有在用户仪表板中的iframe呈现的HTML。 – Vik 2012-07-25 05:03:42

+0

看看这是否可以帮助你http://stackoverflow.com/questions/472951/how-do-i-print-an-iframe-from-javascript-in-safari-chrome/473270#473270想法应该很容易翻译成GWT 。 – krishnakumarp 2012-07-25 05:40:49

回答

2

这个脚本添加iframe内容页面的标签

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

添加此本地方法GWT类

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

打印按钮的单击事件的操作处理程序。

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

代码从this discussion

+0

我在我的gwt模块中创建帧为 Frame frame = new Frame(); frame.setUrl(“http://commondatastorage.googleapis.com/bloodcard/160001_25-Jul-2012”); 但当我调用方法为你建议作为printIframeContent(frame.getElement()。getId());它似乎返回null。那么通过id的正确方法是什么? – Vik 2012-07-25 21:10:22

+1

在Frame上设置frame frame = new Frame(“http://commondatastorage.googleapis.com/bloodcard/160001_25-Jul-2012”); frame.getElement()。setId(“printiframe”); – krishnakumarp 2012-07-26 06:24:48

+0

我做到了这一点,但现在我得到异常 在java.lang.Thread.run(Thread.java:722) 引起:com.google.gwt.core.client.JavaScriptException:(TypeError):Property'printPage'of object [object Window]不是一个函数 – Vik 2012-07-26 18:48:35

0

衍生这里是GWT一个简单的打印机类。它打印出页面。

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

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

公共类打印机{

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()); 
} 

}