我們有管理儀表板內置GWT和部署在谷歌應用程序引擎的Java。儀表板上有一項名爲「我的名片」的功能,獻血者可以在這裏看到他的獻血者登記卡。gwt的打印能力
目前,我們正在谷歌存儲中創建和存儲此卡,當有人轉到「我的卡」時,我們會在儀表板中使用iFrame呈現卡。
我們想給予打印此卡的能力。請告訴如何做?
只是加上我試圖Print.it罐子,但好像它是過時的,不與GWT玩好了
我們有管理儀表板內置GWT和部署在谷歌應用程序引擎的Java。儀表板上有一項名爲「我的名片」的功能,獻血者可以在這裏看到他的獻血者登記卡。gwt的打印能力
目前,我們正在谷歌存儲中創建和存儲此卡,當有人轉到「我的卡」時,我們會在儀表板中使用iFrame呈現卡。
我們想給予打印此卡的能力。請告訴如何做?
只是加上我試圖Print.it罐子,但好像它是過時的,不與GWT玩好了
這個腳本添加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
}
我在我的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
在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
我做到了這一點,但現在我得到異常 在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
衍生這裏是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());
}
}
從移動設備或一標準的瀏覽器? GWT呈現爲HTML,因此瀏覽器的默認打印功能將按照顯示的那樣打印出來。不需要額外的庫。 – 2012-07-25 04:48:43
從標準瀏覽器......以及我不希望整個頁面被打印......只有在用戶儀表板中的iframe呈現的HTML。 – Vik 2012-07-25 05:03:42
看看這是否可以幫助你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