我想在 GWT 中打印一页页面。
但是有人说你必须使用iframe
,因为它有打印方法。
我尝试了这段代码,但它不起作用:
HTML html=new HTML("<iframe id="myframe"></iframe>") ;....
searchButton.setWidth("80px");
searchButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
MyNative.printIframeContent("myframe", "onlineMap");
}
});
map = new SimplePanel();
map.getElement().setId("onlineMap");
map.add(mapView.getMapWidget());
mapView
是返回 GWT MapWidget 的类的实例,通过这种方式,我想将 GWT map 添加到 iframe,然后使用 iframe 的打印功能来打印 google map
MyNative
是一个使用GWT JSNI调用printPage javascript函数的类
function printPage(idMap,idFrame) {
var myElement = document.getElementById(idMap);
var iframe = document.getElementById(idFrame);
var body = iframe.getElementsByTagName('body');
body.innerHTML = myElement;
iframe.contentWindow.print();
}
但是浏览器无法加载 iframe 的正文。
最佳答案
你可以试试这个:
String html = myPanel.getElement().getInnerHTML();
print(html);
public static final native void print(String html) /*-{
top.consoleRef=$wnd.open('','_blank', "");
top.consoleRef.document.write(html);
top.consoleRef.print();
top.consoleRef.document.close()
}-*/;
关于java - 如何在gwt中打印一些元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25485735/