How to embed and display an SVG image in iReport

AkD picture AkD · Jul 5, 2012 · Viewed 7.5k times · Source

I am using iReport 4.5.0, and want to embed an SVG image to generate a PDF.

When using iReport's preview option to run the report, the image won't get rendered to the PDF (i.e. there's an empty space where the image is supposed to be).

I do not see any exceptions in the logs during execution, what's going on?

Here's the image tag code snippet:

<?xml version="1.0" encoding="UTF-8"?>
<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="reportSVGTest" language="groovy" pageWidth="595" 
        pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" 
        topMargin="20" bottomMargin="20">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="50">
            <staticText>
                <reportElement x="81" y="13" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[Hello]]></text>
            </staticText>
        </band>
    </title>
    <detail>
        <band height="443" splitType="Stretch">
            <image hAlign="Center" vAlign="Middle" isUsingCache="true" isLazy="true">
                <reportElement x="12" y="17" width="512" height="403"/>
                <imageExpression  class="net.sf.jasperreports.engine.JRRenderable">
                    <![CDATA[
                        net.sf.jasperreports.renderers.BatikRenderer.getInstance(
                            new java.io.File("C:\\Users\\akshayd\\Desktop\\polygons1.svg"))
                    ]]>
                </imageExpression>
            </image>
        </band>
    </detail>
</jasperReport>

Answer

Tomao44 picture Tomao44 · Jan 14, 2014

I wanted to embed a SVG text in my report, and this topic helped me succeed.
I am using Jaspersoft Studio with full support for JasperReports 5.5.0.
Here is how i did :

  • Create a image element
  • Choose Custom expression and let expression empty
  • Modify JRXML source: add a tag imageExpression in the tag image
<image>
  <reportElement x="0" y="60" width="50" height="50" uuid="c0d0856e-0bb1-4634-b8b0-   6f7ace0e3ff3"/>
  <imageExpression><![CDATA[net.sf.jasperreports.renderers.BatikRenderer.getInstanceFromText("<svg:svg xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.0\" width=\"88\" height=\"21\" id=\"svg\"><svg:g id=\"layer1\"><svg:path id=\"path1\" d=\"M 61.799149,2.0342178 L 67.119461,2.0342178 L 67.119461,19.213905 L 61.799149,19.213905 L 61.799149,2.0342178 z\" style=\"text-anchor:start;fill:#FF0000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;\" /></svg:g> </svg:svg>")]]></imageExpression>
</image>

So, i use the method getInstanceFromText of BatikRenderer.
Do not forget to protect double quotes in your SVG text.