我正在开发一个spring-mvc项目,我正在使用JasperReports进行报告。我有一个链接,生成pdf格式的报告。链接工作正常并生成报告,但问题是pdf报告未显示图像。来自我的jrxml文件的片段:
<frame>
<reportElement x="0" y="0" width="70" height="67" uuid="0af2e978-7c0c-4805-afb4-4069d1297a12"/>
<image onErrorType="Blank" evaluationTime="Report">
<reportElement mode="Opaque" x="0" y="0" width="70" height="67" uuid="c572c836-8d67-480a-b1b1-d603940e6c74"/>
<imageExpression><![CDATA["images/geLogo.jpg"]]></imageExpression>
</image>
</frame>
我再次检查,图像出现在我项目的webapp / images文件夹中。我在jsp页面中使用了相同的图像,它正在工作。
用于从JasperReport生成pdf的代码:
try {
JasperPrint print = service.loadReceipt(RECEIPT_NAME, paymentId);
pdfFile = JasperExportManager.exportReportToPdf(print);
OutputStream outStream = res.getOutputStream();
res.setContentType("application/pdf");
res.addHeader("Content-disposition", "inline; filename=Receipt.pdf");
outStream.write(pdfFile);
outStream.flush();
outStream.close();
}
你能告诉我为什么图像不会出现在pdf中,尽管在JasperReport Designer中以相同的路径显示图像。我在我的eclipse中使用了JasperReports插件来设计报告。我通过war文件在jboss 6.4中部署项目
提前谢谢。
的 ------- -------- UPDATE
我从@ KDavid-Valerio的回答中得到了检查战争项目结构的想法。它与实际的项目结构不同。战争结构看起来像这样:
Project
images
image1.jpg
image2.jpg
WEB-INF
classes
reports
report1.jrxml
report2.jrxml
它似乎仍然不起作用。
的 ------- -------- UPDATE
填写碧玉报告的代码:
public JasperPrint loadReceipt(String reportName, String paymentId, String imagePath) {
HashMap<String, Object> params = new HashMap<String, Object>();
JasperReport report = null;
JasperPrint print = null;
try {
if (jrReportMap == null) {
jrReportMap = new HashMap<String, JasperReport>();
}
if (jrReportMap.get(reportName) == null) {
report = JasperCompileManager.compileReport(reportManager.load(reportName));
jrReportMap.put(reportName, report);
log.info(Logger.EVENT_SUCCESS, "--- Report Compilation done --- " + reportName);
} else {
report = jrReportMap.get(reportName);
log.info(Logger.EVENT_SUCCESS, "--- Report already Compiled --- " + reportName);
}
params.put("paymentId", paymentId);
params.put("realPath", imagePath);
try {
Connection conn = reportDataSource.getConnection();
print = JasperFillManager.fillReport(report, params, conn);
conn.close();
} catch (SQLException e) {
System.err.println("--- SQL ERR - to get connection -----");
log.error(Logger.EVENT_FAILURE, "--- Report already Compiled --- " + reportName);
e.printStackTrace();
}
} catch (JRException e1) {
log.error(Logger.EVENT_FAILURE, "Oops... Something wrong while rendering the report !!!");
e1.printStackTrace();
}
return print;
}
答案 0 :(得分:0)
而不是使用<imageExpression><![CDATA["images/geLogo.jpg"]]></imageExpression>
使用参数。 。E.g,:
<imageExpression><![CDATA[$P{imagePath}]]></imageExpression>
imagePath它是一个String,包含WAR内部图像的相对URL。
例如,如果您的WAR具有以下结构:
YourWebProject
WebContent
reports
report1.jasper
report2.jasper
...
reportn.jasper
images
geLogo.jpg
填充imagePath参数时,必须为其指定相对于报告路径的图像路径。如果你想使用geLogo.jpg图像,它将是:
String imagePath = "../images/geLogo.jpg";
答案 1 :(得分:0)
我使用ServletContext找到了解决这个问题的方法。
getRealPath()
自动装配此依赖关系,然后使用其方法 byte[] pdfFile = null;
String realPath = context.getRealPath("/images/");
try {
JasperPrint print = service.loadReceipt(RECEIPT_NAME, paymentId, realPath);
pdfFile = JasperExportManager.exportReportToPdf(print);
OutputStream outStream = res.getOutputStream();
res.setContentType("application/pdf");
res.addHeader("Content-disposition", "inline; filename=Receipt.pdf");
outStream.write(pdfFile);
outStream.flush();
outStream.close();
}
在部署项目后获取任何目录的真实路径。我发现每次在jboss中部署代码时它都会不断变化。
生成pdf的代码:
public JasperPrint loadReceipt(String reportName, String paymentId, String realPath) {
HashMap<String, Object> params = new HashMap<String, Object>();
JasperReport report = null;
JasperPrint print = null;
try {
if (jrReportMap == null) {
jrReportMap = new HashMap<String, JasperReport>();
}
if (jrReportMap.get(reportName) == null) {
report = JasperCompileManager.compileReport(reportManager.load(reportName));
jrReportMap.put(reportName, report);
log.info(Logger.EVENT_SUCCESS, "--- Report Compilation done --- " + reportName);
} else {
report = jrReportMap.get(reportName);
log.info(Logger.EVENT_SUCCESS, "--- Report already Compiled --- " + reportName);
}
params.put("paymentId", paymentId);
params.put("realPath", realPath);
try {
Connection conn = reportDataSource.getConnection();
print = JasperFillManager.fillReport(report, params, conn);
conn.close();
} catch (SQLException e) {
System.err.println("--- SQL ERR - to get connection -----");
log.error(Logger.EVENT_FAILURE, "--- Report already Compiled --- " + reportName);
e.printStackTrace();
}
} catch (JRException e1) {
log.error(Logger.EVENT_FAILURE, "Oops... Something wrong while rendering the report !!!");
e1.printStackTrace();
}
return print;
}
填写报告的代码:
<frame>
<reportElement x="0" y="0" width="70" height="67" uuid="0af2e978-7c0c-4805-afb4-4069d1297a12"/>
<image onErrorType="Blank" evaluationTime="Report">
<reportElement mode="Opaque" x="0" y="0" width="70" height="67" uuid="c572c836-8d67-480a-b1b1-d603940e6c74"/>
<imageExpression><![CDATA[$P{realPath}+"/geLogo.jpg"]]></imageExpression>
</image>
</frame>
最后是jrxml片段:
1 | 1.12 | 22.22 | 123 | 1234.44 | .55