GWT:从服务器发送PDF文档到客户端

发布于 2024-09-08 21:01:15 字数 560 浏览 6 评论 0原文

我有一个 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());

但它不起作用。怎样才能做到呢?

I have an RPC service and one of the method is generating a report using Pentaho Reporting Engine. Report is an PDF file. What I'd like to do, is when user request a report, the report is sent back to him and save dialog or sth pops up. I tried this inside my service method:

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());

But it doesn't work. How it can be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿越时光隧道 2024-09-15 21:01:15

我的做法有点不同。我有一个单独的 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();
}
}

I do it a little bit differently. I've got a separate servlet that I use to generate the PDF. On the client, do something like:

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

The servlet, DownloadPDF looks something like this:

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();
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文