2

我有一个 RPC 服务,其中一种方法是使用 Pentaho Reporting Engine 生成报告。报告是 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());

但它不起作用。怎么做?

4

1 回答 1

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();
}
}
于 2010-07-12T02:01:23.570 回答