5

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>
5
  • I cannot display svg file in other format like html, xhtml etc too
    – AkD
    Jul 5, 2012 at 15:58
  • I have also noticed that if you shift from xml view to design view sometimes the class="net.sf.jasperreports.engine.JRRenderable" is getting removed from <imageExpression> tag. It is due to the fact that the design view recalculates any mess up the xml code sometimes. But I am taking care of that.
    – AkD
    Jul 5, 2012 at 16:09
  • I have also tried preview in older versions, but no luck :(
    – AkD
    Jul 5, 2012 at 17:17
  • I have also asked the same question to ireport bug tracker to know if its a possible bug
    – AkD
    Jul 9, 2012 at 14:31
  • See: stackoverflow.com/a/39320863/59087 Dec 1, 2016 at 18:01

1 Answer 1

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.