2013-03-18 71 views
1

我可以打开生成的PDF格式碧玉报告,在新标签页中Window.open():打印生成的PDF与GWT

Window.open("report.pdf?traceCode=" + traceCode, "_BLANK", ""); 

如果我想通过点击一个按钮,打印的PDF文件,是我应该做的? (Window.print()将打印整个页面)

+0

不推荐,但有可能只与其他实用工具 - http://stackoverflow.com/questions/205180/ how-to-print-a-pdf-from-the-browser – SSR 2013-03-18 17:08:02

回答

0

如果该窗口包含非HTML页面,您将无法调用窗口对象的print()方法(或任何其他方法) 。

让用户download该文件。

参考this

4

您可以通过使用本机的打印方法,你可以调用Window.Print创建自己的类,并在您的应用程序打印按钮的点击,()。

GWT UIObject可用于此目的和方法,它将被转换为字符串。

public class NTPrint { 
    /** 
    * If true, use a Timer instead to print the internal frame 
    */ 
    public static boolean USE_TIMER = true; 

    /** 
    * Time in seconds to wait before printing the internal frame when using Timer 
    */ 
    public static int TIMER_DELAY = 1; 

    public static native void it() /*-{ 
     $wnd.print(); 
    }-*/; 

    public static void it(String html) { 
     try{ 
      buildFrame(html); 
      if (USE_TIMER) { 
       Timer timer = new Timer() { 
        public void run() { 
         printFrame(); 
        } 
       }; 
       timer.schedule(TIMER_DELAY * 1000); 
      } else { 
       printFrame(); 
      } 
     } 
     catch (Throwable exc) { 
      CommonUtil.printStackTrace(exc); 
      Window.alert(exc.getMessage()); 
     } 
    } 

    /** 
    * This method will be called when you pass Widget without style. 
    * @param uiObjects 
    */ 
    public static void it(UIObject...uiObjects) { 
     StringBuffer objString= new StringBuffer(); 
     for(UIObject obj: uiObjects) 
       objString.append(obj.toString()); 
      it("", objString.toString()); 
    } 

}

打印按钮的的OnClick,

/** 
     * prints all forms 
     * @param documentInfo 
     */ 
     public static void printForms(List<FormTransaction> formTransactions, String documentInfo, List<CellTransaction> cellTransactionList, boolean isComment, Document document) { 
       String style = "<link rel='styleSheet' type='text/css' href='v4workflow/css/style.css'>" 
           + "<link rel='styleSheet' type='text/css' href='v4workflow/css/gwtcontrols.css'>" 
           + "<style type='text/css' media='print'>" 
           + "@media print {" 
           + ".footerText{font-size:13px; font-weight:normal;margin-top:0px;}" 
           + ".break {page-break-after:always}" 
           + "}" + "</style>"; 
       List<UIObject> uiObjects = new ArrayList<UIObject>(); 
       NTPrintLayout printLayout = new NTPrintLayout(); 
       printLayout.setSelectedDocument(null); 
       uiObjects.add(createHeader()); 
       NTPrint.it(style,uiObjects); 
     } 

public static FlexTable createHeader(){ 
       FlexTable flexTable = new FlexTable(); 
       flexTable.setWidth("100%"); 
       return flexTable; 
     } 
+0

+1对于UIobjects上的迭代器想法。 – 2013-03-18 13:45:34

+0

我无法确定PDF文件的打印位置。上面的答案是否打印表单? – SSR 2013-03-18 17:09:46

+0

我试过了,但样式没有加载 – united 2013-03-22 12:51:17