2010-07-11 154 views
2

我有一個RPC服務,其中一種方法是使用Pentaho Reporting Engine生成報告。報告是一個PDF文件。我想要做的是,當用戶請求報告時,報告會發回給他並保存對話或彈出。我在我的服務方法中試過這個:GWT:將PDF文檔從服務器發送到客戶端

Resource res = manager.createDirectly(new URL(reportUrl), MasterReport.class); 
      MasterReport report = (MasterReport) res.getResource(); 
      report.getParameterValues().put("journalName", "FooBar"); 
      this.getThreadLocalResponse().setContentType("application/pdf"); 
      PdfReportUtil.createPDF(report, this.getThreadLocalResponse().getOutputStream()); 

但它不起作用。如何做到這一點?

回答

6

我這樣做有點不同。我有一個單獨的servlet用於生成PDF。在客戶端,這樣做:

Cookies.setCookie(set what ever stuff PDF needs...); 
Window.open(GWT.getModuleBaseURL() + "DownloadPDF", "", ""); 

該servlet,DownloadPDF看起來是這樣的:

public class DownloadPDF extends HttpServlet { 

public void doGet(HttpServletRequest request, HttpServletResponse response) { 
    Cookie[] cookies = request.getCookies(); 
    try { 
     // get cookies, generate PDF. 
     // If PDF is generated to to temp file, read it 
     byte[] bytes = getFile(name); 
    sendPDF(response, bytes, name); 
    } catch (Exception ex) { 
     // do something here 
    } 
} 

byte[] getFile(String filename) { 

    byte[] bytes = null; 

    try { 
     java.io.File file = new java.io.File(filename); 
     FileInputStream fis = new FileInputStream(file); 
     bytes = new byte[(int) file.length()]; 
     fis.read(bytes); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return bytes; 
} 

void sendPDF(HttpServletResponse response, byte[] bytes, String name) throws IOException { 
    ServletOutputStream stream = null; 

    stream = response.getOutputStream(); 
    response.setContentType("application/pdf"); 
    response.addHeader("Content-Type", "application/pdf"); 
    response.addHeader("Content-Disposition", "inline; filename=" + name); 
    response.setContentLength((int) bytes.length); 
    stream.write(bytes); 
    stream.close(); 
} 
} 
+0

完美的作品:)在GWT客戶 – jjczopek 2010-07-12 15:28:15

+0

@BJB及彼字節怎麼寫字節文件, gwt不支持,對吧? – Parvathy 2013-03-13 07:17:13

相關問題