JasperReport 在转换为 pdf 时不显示图像
JasperReport not displaying image when converted to pdf
我正在开发一个 spring-mvc 项目,我正在使用 JasperReports 进行报告。我有一个 link 可以生成 pdf 格式的报告。 Link 工作正常并生成了报告,但问题是 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();
}
尽管在 JasperReport Designer 中以相同的路径显示图像,但你能告诉我为什么图像没有出现在 pdf 中吗? 我在我的 eclipse 中使用了 JasperReports 插件来设计报告。我通过 war 文件在 jboss 6.4 中部署项目。
提前谢谢你。
--------更新--------
我从@KDavid-Valerio 的回答中得到了检查 war 内部项目结构的想法。它与实际的项目结构不同。 war 结构看起来像这样:
Project
images
image1.jpg
image2.jpg
WEB-INF
classes
reports
report1.jrxml
report2.jrxml
好像还是不行。
--------更新--------
填充jasper报告的代码:
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;
}
而不是使用<imageExpression><![CDATA["images/geLogo.jpg"]]></imageExpression>
使用一个参数。
例如:
<imageExpression><![CDATA[$P{imagePath}]]></imageExpression>
imagePath 它是一个字符串,包含 WAR.
中图像的相对 URL
例如,如果您的 WAR 具有以下结构:
YourWebProject
WebContent
reports
report1.jasper
report2.jasper
...
reportn.jasper
images
geLogo.jpg
当您填写 imagePath 参数时,您必须为其提供相对于报告路径的图像路径。如果你想使用 geLogo.jpg 图片,它将是:
String imagePath = "../images/geLogo.jpg";
我找到了使用 ServletContext 解决此问题的方法。
@Autowired
ServletContext context;
自动装配此依赖项,然后使用其方法 getRealPath()
在项目部署后获取任何目录的真实路径。我发现每次我在 jboss.
中部署代码时它都会不断变化
生成pdf的代码:
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();
}
填写报告的代码:
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;
}
最后是 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[$P{realPath}+"/geLogo.jpg"]]></imageExpression>
</image>
</frame>
我正在开发一个 spring-mvc 项目,我正在使用 JasperReports 进行报告。我有一个 link 可以生成 pdf 格式的报告。 Link 工作正常并生成了报告,但问题是 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();
}
尽管在 JasperReport Designer 中以相同的路径显示图像,但你能告诉我为什么图像没有出现在 pdf 中吗? 我在我的 eclipse 中使用了 JasperReports 插件来设计报告。我通过 war 文件在 jboss 6.4 中部署项目。
提前谢谢你。
--------更新--------
我从@KDavid-Valerio 的回答中得到了检查 war 内部项目结构的想法。它与实际的项目结构不同。 war 结构看起来像这样:
Project
images
image1.jpg
image2.jpg
WEB-INF
classes
reports
report1.jrxml
report2.jrxml
好像还是不行。
--------更新--------
填充jasper报告的代码:
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;
}
而不是使用<imageExpression><![CDATA["images/geLogo.jpg"]]></imageExpression>
使用一个参数。 例如:
<imageExpression><![CDATA[$P{imagePath}]]></imageExpression>
imagePath 它是一个字符串,包含 WAR.
中图像的相对 URL例如,如果您的 WAR 具有以下结构:
YourWebProject
WebContent
reports
report1.jasper
report2.jasper
...
reportn.jasper
images
geLogo.jpg
当您填写 imagePath 参数时,您必须为其提供相对于报告路径的图像路径。如果你想使用 geLogo.jpg 图片,它将是:
String imagePath = "../images/geLogo.jpg";
我找到了使用 ServletContext 解决此问题的方法。
@Autowired
ServletContext context;
自动装配此依赖项,然后使用其方法 getRealPath()
在项目部署后获取任何目录的真实路径。我发现每次我在 jboss.
生成pdf的代码:
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();
}
填写报告的代码:
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;
}
最后是 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[$P{realPath}+"/geLogo.jpg"]]></imageExpression>
</image>
</frame>