somename
somename

Reputation: 1018

How to refer to a .jrtx template file from Jasper Report (.jrxml)?

I am using this example to apply a jasper template to jasper report. I am not able to find out how can I refer to jasper template from .jrxml file. The .jrxml file and .jrtx file are at the same location. In the Jasper I am referring to the template using following syntax.

<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="QCReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20"
              topMargin="20" bottomMargin="20" uuid="0ea707c5-78f5-4f37-a065-a228202cb59e">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <import value="org.apache.commons.lang.StringUtils"/>
    ..
    <template><![CDATA["JasperCustomStyle.jrtx"]]></template>
    ..

But it gives me Byte data not found at: JasperCustomStyle.jrtx. exception. Can someone please help me figure out how to refer to a jasper template from .jrxml file?

Detailed Exception

Caused by: net.sf.jasperreports.engine.JRException: Byte data not found at: JasperCustomStyle.jrtx.
    at net.sf.jasperreports.repo.RepositoryUtil.getBytesFromLocation(RepositoryUtil.java:210)
    at net.sf.jasperreports.engine.xml.JRXmlTemplateLoader.loadTemplate(JRXmlTemplateLoader.java:106)
    at net.sf.jasperreports.engine.fill.JRFillReportTemplate.loadTemplate(JRFillReportTemplate.java:111)
    at net.sf.jasperreports.engine.fill.JRFillReportTemplate.evaluate(JRFillReportTemplate.java:89)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.collectTemplates(JRBaseFiller.java:740)

The jrtx file looks like below.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperTemplate PUBLIC "-//JasperReports//DTD Template//EN" "http://jasperreports.sourceforge.net/dtds/jaspertemplate.dtd">

<jasperTemplate>
    <style name="New style" mode="Opaque" forecolor="#000000" backcolor="#FFC800" fill="Solid" hAlign="Center" vAlign="Middle" isBold="true" isUnderline="true">
        <box>
            <topPen lineWidth="1.0"/>
            <leftPen lineWidth="1.0"/>
            <bottomPen lineWidth="1.0"/>
            <rightPen lineWidth="1.0"/>
        </box>
    </style>
</jasperTemplate>

Upvotes: 4

Views: 4555

Answers (2)

madroid
madroid

Reputation: 334

Anyone wondering here for the solution, As of Jasper studio 6.11.0 we should use below code to import style component from external source to be added to jrxml file:

<template><![CDATA["http://linktomyStylesheet/Mystylesheet.jrtx"]]></template>

Upvotes: 0

Petter Friberg
Petter Friberg

Reputation: 21710

Caused by: net.sf.jasperreports.engine.JRException: Byte data not found at: JasperCustomStyle.jrtx.

which means that the template can not be found.

You need to pass the correct path so that jasper can find the file, this is normally done by using parameters

Example

<template>$P{templateLocation} + "JasperCustomStyle.jrtx"</template>
<parameter name="templateLocation"/>

To parameter templateLocation you pass the absolute location of the folder that contains the templates (include the final /)

or if an URL (in web application)

<template class="java.net.URL">$P{templateURL}</template>
<parameter name="templateURL" class="java.net.URL"/>

To templateURL you pass the URL to the template, in web application you can also easily check that the template is available passing same URL to browser

Upvotes: 5

Related Questions