如何导出碧玉报告PDF格式的报告 [英] How to export pdf report in jasper reports

查看:129
本文介绍了如何导出碧玉报告PDF格式的报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想报表导出为PDF格式,它应该问一个下载位置的用户。我该怎么做这Grails的?

这是我的code:

 高清exportToPdf(JasperPrint jasperPrint,字符串路径,请求){    字符串CUR_TIME = System.currentTimeMillis的();
    JRExporter pdfExporter = NULL;    pdfExporter =新JRPdfExporter();    log.debug(导出到文件...+ JasperExportManager.exportReportToPdfFile(jasperPrint,C:\\\\ pdfReportPDF+ CUR_TIME +));    返回;
}

在碧玉控制器:

  / **
   *生成HTML响应。
   * /
  高清generateResponse = {reportDef - >
    如果(reportDef.fileFormat.inline&安培;!&安培;!reportDef.parameters._inline){
      response.setHeader(内容处置,附件;文件名= \\+ reportDef.name + + reportDef.fileFormat.extension +\\,);
      response.contentType = reportDef.fileFormat.mimeTyp
      response.characterEncoding =UTF-8
      response.outputStream<< reportDef.contentStream.toByteArray()
    }其他{
      渲染(文字:reportDef.contentStream,则contentType:reportDef.fileFormat.mimeTyp,编码:reportDef.parameters.encoding reportDef.parameters.encoding:UTF-8);
    }
  }


解决方案

你有没有看了碧玉插件?它似乎已经建立了你的工具。至于要求用户的下载位置的浏览器拥有的部分的控制器上的文件是如何从网页接收。这就是你想要在下载位置控制你的真正的问题?

[更新]

使用位置'C:\\'。是你的服务器的不可以在客户端上,这就是为什么它没有下载

尝试这样的事情...

 高清controllerMethod = {
    高清temp_file = File.createTempFile(JasperReport的,PDF)//< - 你不必使用临时文件,但不要忘了在某一时刻将其删除关闭服务器。    JasperExportManager.exportReportToPdfFile(jasperPrint,temp_file.absolutePath));    response.setContentType(应用程序/ PDF格式)//< - 你必须在某个时候动态地处理这个问题
    response.setHeader(内容处置,附件;文件名= $ {temp_file.getName()})
    response.outputStream<< temp_file.newInputStream()//< - 二进制流副本客户端}

我没有测试过这一点,还有的处理文件和流更好的方式,但我认为你会得到的总体思路。

I want to export a report as pdf and it should ask the user for a download location. How do I do this in grails?

This is my code:

def exportToPdf(JasperPrint jasperPrint,String path,request){

    String cur_time =System.currentTimeMillis();
    JRExporter pdfExporter = null;

    pdfExporter = new JRPdfExporter();

    log.debug("exporting to file..."+JasperExportManager.exportReportToPdfFile(jasperPrint, "C:\\pdfReport"+cur_time+".pdf"));

    return ;
}

In jasper controller:

/**
   * Generate a html response.
   */
  def generateResponse = {reportDef ->
    if (!reportDef.fileFormat.inline && !reportDef.parameters._inline) {
      response.setHeader("Content-disposition", "attachment; filename=\"" + reportDef.name + "." + reportDef.fileFormat.extension + "\"");
      response.contentType = reportDef.fileFormat.mimeTyp
      response.characterEncoding = "UTF-8"
      response.outputStream << reportDef.contentStream.toByteArray()
    } else {
      render(text: reportDef.contentStream, contentType: reportDef.fileFormat.mimeTyp, encoding: reportDef.parameters.encoding ? reportDef.parameters.encoding : 'UTF-8');
    }
  }

解决方案

Have you looked at the Jasper Plugin? It seems to have the tools already built for you. As far as asking the user for a download location the browser has some controller over how files are received from a web page. Is your real issue that you want control over the download location?

[UPDATE]

Using the location 'c:\' is on your server not the client and this is why it is not downloading.

try something like this...

def controllerMethod = {
    def temp_file = File.createTempFile("jasperReport",".pdf") //<-- you don't have to use a temp file but don't forget to delete them off the server at some point.

    JasperExportManager.exportReportToPdfFile(jasperPrint, temp_file.absolutePath));

    response.setContentType("application/pdf")  //<-- you'll have to handle this dynamically at some point
    response.setHeader("Content-disposition", "attachment;filename=${temp_file.getName()}")
    response.outputStream << temp_file.newInputStream() //<-- binary stream copy to client



}

I have not tested this and there are better ways of handling the files and streams but i think you'll get the general idea.

这篇关于如何导出碧玉报告PDF格式的报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆