Jump to content

image in custom JasperReport .xml template


sledgeas

Recommended Posts

I have put an image, which is in the same directory as the template .xml file ($IREPORT_INSTALLATION/templates)

I refer to it as:
                <image  scaleImage="FillFrame" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        x="0"
                        y="0"
                        width="53"
                        height="44"
                        key="image-1"
                        stretchType="RelativeToBandHeight"
                        isPrintInFirstWholeBand="true"/>
                    <box></box>
                    <graphicElement stretchType="RelativeToBandHeight"/>
                    <imageExpression class="java.lang.String"><![CDATA["image.jpg"]]></imageExpression>
                </image>

I get errors (through e.g. iReport):

Error filling print... Byte data not found at location : .logo.jpg
net.sf.jasperreports.engine.JRException: Byte data not found at location : .logo.jpg     at net.sf.jasperreports.engine.util.JRLoader.loadBytesFromLocation(JRLoader.java:482)
...

Providing absolute path works. I also tried referring to it as ./image.jpg, .\image.jpg but nothing helped.

Thank you in advance.

--
sledge

 

 

Link to comment
Share on other sites

  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

Wrote:
 

Relative paths from .jrxml files to images (or other resources) do not work as you suppose they do (i.e. relative to the .jrxml file).  Instead, all relative paths are considered relative to the JVM current directory.

You can customize the way files are resolved by setting a value for the REPORT_FILE_RESOLVER parameter.

HTH,

Lucian

Thank you, I have resolved it by adding iReport/templates to the class path.

As I could not get any documentation of how to use REPORT_FILE_RESOLVER, but thanks anyway.

Case solved,

--
sledge

 

Link to comment
Share on other sites

  • 8 months later...

This is my code,and it runing succeed.the jasper file and zx.jpg in one folder.

 

Code:
report36.jrxml:<?xml version="1.0" encoding="GBK"?><jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report name" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">        <parameter name="imagePath" class="java.lang.String"/>        <background>  <band/> </background> <title>  <band height="79">   <staticText>    <reportElement x="223" y="26" width="130" height="41"/>    <textElement textAlignment="Center" verticalAlignment="Middle">     <font size="24" isBold="true"/>    </textElement>    <text><![CDATA[测试图片]]></text>   </staticText>  </band> </title> <pageHeader>  <band/> </pageHeader> <columnHeader>  <band height="195">   <image>    <reportElement x="12" y="17" width="516" height="165"/>    <imageExpression class="java.lang.String"><![CDATA[$P{imagePath}]]></imageExpression>   </image>  </band> </columnHeader> <detail>  <band/> </detail> <columnFooter>  <band/> </columnFooter> <pageFooter>  <band/> </pageFooter> <summary>  <band/> </summary></jasperReport>servlet: protected void doPost(HttpServletRequest req, HttpServletResponse resp)   throws ServletException, IOException {  OutputStream out = resp.getOutputStream();  resp.setContentType("text/html;charset=GBK");  String root_path = this.getServletContext().getRealPath("/");  root_path = root_path.replace('', '/');  // 拿到*.jasper文件的路径  String reportFilePath = root_path    + "WEB-INF/classes/com/sunshine/jasper/report36.jasper";    File reportFile = new File(reportFilePath);    if (!reportFile.exists())      throw new IOException("ä¼å…¥çš„模æ¿æ–‡ä»¶ä¸å­˜åœ¨!");        //Connection conn = DB.getConnection();  Map imageMap = new HashMap();  imageMap.put("imagePath", root_path + "WEB-INF/classes/com/sunshine/jasper/zx.jpg");  try {   JasperReport report = (JasperReport) JRLoader.loadObject(reportFilePath);   JasperPrint jasperPrint = JasperFillManager.fillReport(report,imageMap, new JREmptyDataSource());         JRHtmlExporter exporter = new JRHtmlExporter();   req.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,jasperPrint);    exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);    exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,"image?image=");      exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);   exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);   exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GBK");   exporter.exportReport();   out.close();  } catch (Exception ex) {   ex.printStackTrace();  } }
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...