GWT:将PDF文档从服务器发送到客户端
我有一个RPC服务,其中一个方法是使用Pentaho报告引擎生成报告。报告是一个PDF文件。我想做的是,当用户请求一份报告时,该报告将被发送回他,并弹出保存对话框或其他内容。我在我的服务方法中尝试了以下内容:GWT:将PDF文档从服务器发送到客户端,pdf,gwt,download,pentaho,Pdf,Gwt,Download,Pentaho,我有一个RPC服务,其中一个方法是使用Pentaho报告引擎生成报告。报告是一个PDF文件。我想做的是,当用户请求一份报告时,该报告将被发送回他,并弹出保存对话框或其他内容。我在我的服务方法中尝试了以下内容: Resource res = manager.createDirectly(new URL(reportUrl), MasterReport.class); MasterReport report = (MasterReport) res.getResource()
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());
但它不起作用。怎么做呢?我做的有点不同。我有一个单独的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();
}
}
@BJB在GWT客户端中获取字节如何将字节写入文件,GWT不支持,对吗?