Jasper Reports PDF Export - Displaying SVG image from embedded URL

浪子不回头ぞ 提交于 2021-02-08 08:39:18

问题


Here is the sample report

<?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="sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="80b32d69-a3a5-4132-afef-37bc11966113">
<parameter name="img" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA["data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMjQ0IiBoZWlnaHQ9IjgyIj48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDEgMzcgYyAwIC0wLjIxIC0wLjUyIC04LjEzIDAgLTEyIGMgMC43OSAtNS45NSAyLjMxIC0xMi42OCA0IC0xOCBjIDAuNDYgLTEuNDQgMS45IC0yLjkgMyAtNCBjIDAuOCAtMC44IDEuOTggLTEuODggMyAtMiBjIDMuOTYgLTAuNDcgOS40IC0wLjUgMTQgMCBjIDcuNjggMC44MyAxNS43NiAyLjM5IDIzIDQgYyAxL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3IDMgMiBjIDYuMDEgNi4wMSAxMS41NiAxMy44MSAxOCAxOSBjIDUuMDcgNC4wOCAxMS44MiA3LjEgMTggMTAgYyA0LjQ3IDIuMDkgOS4yOSAzLjk3IDE0IDUgYyA1LjczIDEuMjUgMTEuNzQgMS4zNSAxOCAyIGMgMy44MiAwLjQgNy4yNSAwLjkyIDExIDEgYyAxMy4yNSAwLjI3IDI2LjcyIDEuMTIgMzkgMCBjIDUuMjggLTAuNDggMTAuOTMgLTMuMDcgMTYgLTUgYyAxLjc2IC0wLjY3IDMuODkgLTEuNjkgNSAtMyBjIDIuMjMgLTIuNjMgNC4zNSAtNi41NCA2IC0xMCBjIDEuNjUgLTMuNDcgMy4xMiAtNy40NyA0IC0xMSBjIDAuMzggLTEuNTEgLTAuMzQgLTMuNDUgMCAtNSBjIDAuOTMgLTQuMjEgMy4wNyAtOC43OSA0IC0xMyBsIDAgLTUiLz48L3N2Zz4="]]></defaultValueExpression>
</parameter>
<detail>
    <band height="125" splitType="Stretch">
        <image hAlign="Left" isUsingCache="true">
            <reportElement uuid="80c1fb9b-31c1-4755-941a-f100059e488d" x="11" y="10" width="522" height="83"/>
            <imageExpression><![CDATA[$P{img}]]></imageExpression>
        </image>
    </band>
</detail>
</jasperReport>

It returns the error:

Byte data not found at : data:image/svg+xml;base64, ...

while rendering in PDF, though works only on HTML exports. As suggested in some posts, I tried using

net.sf.jasperreports.renderers.BatikRenderer.getInstance(new java.net.URL("data:..."))

but it didn't work either.

Could someone suggest a method that will display the embedded image in all types of exports?


回答1:


I haven't tried it with a svg but I got it working with a png image. You have to add the import org.apache.commons.codec.binary.Base64 and you should remove the Header from the base64 encoded data: data:image/svg+xml;base64,

Now you can decode and pass the data with this Expression: new ByteArrayInputStream(new Base64().decodeBase64($P{logo}.getBytes("UTF-8"))).

In your case it should look like this:

<?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="sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="80b32d69-a3a5-4132-afef-37bc11966113">
<import value="org.apache.commons.codec.binary.Base64"/>
<parameter name="img" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA["PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMjQ0IiBoZWlnaHQ9IjgyIj48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDEgMzcgYyAwIC0wLjIxIC0wLjUyIC04LjEzIDAgLTEyIGMgMC43OSAtNS45NSAyLjMxIC0xMi42OCA0IC0xOCBjIDAuNDYgLTEuNDQgMS45IC0yLjkgMyAtNCBjIDAuOCAtMC44IDEuOTggLTEuODggMyAtMiBjIDMuOTYgLTAuNDcgOS40IC0wLjUgMTQgMCBjIDcuNjggMC44MyAxNS43NiAyLjM5IDIzIDQgYyAxLjM5IDAuMzEgMi44MyAxLjE0IDQgMiBjIDMuNzMgMi43MyA3Ljc4IDUuNzggMTEgOSBjIDEuOTUgMS45NSA0LjA2IDQuNjUgNSA3IGMgMC44OCAyLjIxIDAuMzkgNS41NiAxIDggYyAwLjM0I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zQgMS4zNSAxOCAyIGMgMy44MiAwLjQgNy4yNSAwLjkyIDExIDEgYyAxMy4yNSAwLjI3IDI2LjcyIDEuMTIgMzkgMCBjIDUuMjggLTAuNDggMTAuOTMgLTMuMDcgMTYgLTUgYyAxLjc2IC0wLjY3IDMuODkgLTEuNjkgNSAtMyBjIDIuMjMgLTIuNjMgNC4zNSAtNi41NCA2IC0xMCBjIDEuNjUgLTMuNDcgMy4xMiAtNy40NyA0IC0xMSBjIDAuMzggLTEuNTEgLTAuMzQgLTMuNDUgMCAtNSBjIDAuOTMgLTQuMjEgMy4wNyAtOC43OSA0IC0xMyBsIDAgLTUiLz48L3N2Zz4="]]></defaultValueExpression>
</parameter>
<detail>
    <band height="125" splitType="Stretch">
        <image hAlign="Left" isUsingCache="true">
            <reportElement uuid="80c1fb9b-31c1-4755-941a-f100059e488d" x="11" y="10" width="522" height="83"/>
            <imageExpression><![CDATA[new ByteArrayInputStream(new Base64().decodeBase64($P{img}.getBytes("UTF-8")))]]></imageExpression>
        </image>
    </band>
</detail>
</jasperReport>



回答2:


I have finally solved problem of embedding SVG in Jasper PDF report.

Use raw SVG image data without data:image/svg+xml;base64.

Use net.sf.jasperreports.renderers.BatikRenderer to render SVG image. Maven dependency including this class is following:

<dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>batik-bridge</artifactId>
    <version>1.7</version>
</dependency>

Use fully qualified class names org.apache.commons.codec.binary.Base64 instead of Base64. Maven dependency including this class is following:

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.10</version>
</dependency>

Jasper jrxml shold look like this:

<?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="sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="80b32d69-a3a5-4132-afef-37bc11966113">
<parameter name="img" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA["PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMjQ0IiBoZWlnaHQ9IjgyIj48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDEgMzcgYyAwIC0wLjIxIC0wLjUyIC04LjEzIDAgLTEyIGMgMC43OSAtNS45NSAyLjMxIC0xMi42OCA0IC0xOCBjIDAuNDYgLTEuNDQgMS45IC0yLjkgMyAtNCBjIDAuOCAtMC44IDEuOTggLTEuODggMyAtMiBjIDMuOTYgLTAuNDcgOS40IC0wLjUgMTQgMCBjIDcuNjggMC44MyAxNS43NiAyLjM5IDIzIDQgYyAxLjM5IDAuMzEgMi44MyAxLjE0IDQgMiBjIDMuNzMgMi43MyA3Ljc4IDUuNzggMTEgOSBjIDEuOTUgMS45NSA0LjA2IDQuNjUgNSA3IGMgMC44OCAyLjIxIDAuMzkgNS41NiAxIDggYyAwLjM0IDEuMzUgMS44OCAyLjY3IDIgNCBjIDAuNDkgNS4zNSAxLjE5IDEyLjkgMCAxOCBjIC0wLjkyIDMuOTQgLTguNTYgMTAuMzggLTcgMTIgYyAzLjkyIDQuMDggMjEuMjUgOS43IDMyIDE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c2IC0wLjY3IDMuODkgLTEuNjkgNSAtMyBjIDIuMjMgLTIuNjMgNC4zNSAtNi41NCA2IC0xMCBjIDEuNjUgLTMuNDcgMy4xMiAtNy40NyA0IC0xMSBjIDAuMzggLTEuNTEgLTAuMzQgLTMuNDUgMCAtNSBjIDAuOTMgLTQuMjEgMy4wNyAtOC43OSA0IC0xMyBsIDAgLTUiLz48L3N2Zz4="]]></defaultValueExpression>
</parameter>
<detail>
    <band height="125" splitType="Stretch">
        <image hAlign="Left" isUsingCache="true">
            <reportElement uuid="80c1fb9b-31c1-4755-941a-f100059e488d" x="11" y="10" width="522" height="83"/>
            <imageExpression><![CDATA[net.sf.jasperreports.renderers.BatikRenderer.getInstance(new org.apache.commons.codec.binary.Base64().decodeBase64($P{img}))]]></imageExpression>
        </image>
    </band>
</detail>
</jasperReport>


来源:https://stackoverflow.com/questions/26261394/jasper-reports-pdf-export-displaying-svg-image-from-embedded-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!