我沒有使用PDF插件。我只是使用Jasper的Java API。一切工作正常。 PDF將在單獨的窗口中打開。我遇到的問題是,當沒有找到數據時,它仍會在瀏覽器中打開一個空白標籤。我想攔截這個,並顯示一條消息,而不是沒有數據,但沒有我試過的工作。使用Grails控制器生成PDF之前驗證數據是否存在
這裏是從控制器的代碼片段:
projAccomps = projAccomps.findAll { ProjectAccomplishment pa ->
pa.workDate >= startDate && pa.workDate <= endDate
}
// I'd like to be able to check right here for no data
// and send back a message instead
//
// if (projAccomps.size() < 1) ...
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(projAccomps)
String jasperFile =
grailsApplication.mainContext
.getResource('reports/accomplishment-listing.jasper')
.file
.getAbsoluteFile()
// Report parameter
Map<String, String> reportParam = new HashMap<String, String>()
String projectQuery = "All projects"
String dateRange = ""
Boolean allProjects = true
if (params.project) {
projectQuery = project.name
allProjects = false
}
if (params.startDate) {
dateRange = startDate.format('MM/dd/yyyy')
}
if (params.endDate) {
dateRange += " - " + endDate.format('MM/dd/yyyy')
}
String reportCriteria = "Work Order: ${wo.workOrderNumber}, Project(s): ${projectQuery}, Dates: ${dateRange}"
reportParam['report_criteria'] = reportCriteria
reportParam['all_projects'] = allProjects
JasperPrint print = JasperFillManager.fillReport(jasperFile, reportParam, ds);
byte[] bytes = JasperExportManager.exportReportToPdf(print)
render(file: bytes, contentType: 'application/pdf')
我試過Jquery.ajax和想抓PDF數據或功能成功的消息,但是這是一個災難。
任何人有任何想法如何做到這一點?
以上是通過ajax調用的嗎? –