2014-10-08 43 views
1

这里显示SVG图像的样本报告碧玉报告PDF导出 - 从嵌入的URL

<?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+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMjQ0IiBoZWlnaHQ9IjgyIj48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDEgMzcgYyAwIC0wLjIxIC0wLjUyIC04LjEzIDAgLTEyIGMgMC43OSAtNS45NSAyLjMxIC0xMi42OCA0IC0xOCBjIDAuNDYgLTEuNDQgMS45IC0yLjkgMyAtNCBjIDAuOCAtMC44IDEuOTggLTEuODggMyAtMiBjIDMuOTYgLTAuNDcgOS40IC0wLjUgMTQgMCBjIDcuNjggMC44MyAxNS43NiAyLjM5IDIzIDQgYyAxLjM5IDAuMzEgM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uMDEgNi4wMSAxMS41NiAxMy44MSAxOCAxOSBjIDUuMDcgNC4wOCAxMS44MiA3LjEgMTggMTAgYyA0LjQ3IDIuMDkgOS4yOSAzLjk3IDE0IDUgYyA1LjczIDEuMjUgMTEuNzQgMS4zNSAxOCAyIGMgMy44MiAwLjQgNy4yNSAwLjkyIDExIDEgYyAxMy4yNSAwLjI3IDI2LjcyIDEuMTIgMzkgMCBjIDUuMjggLTAuNDggMTAuOTMgLTMuMDcgMTYgLTUgYyAxLjc2IC0wLjY3IDMuODkgLTEuNjkgNSAtMyBjIDIuMjMgLTIuNjMgNC4zNSAtNi41NCA2IC0xMCBjIDEuNjUgLTMuNDcgMy4xMiAtNy40NyA0IC0xMSBjIDAuMzggLTEuNTEgLTAuMzQgLTMuNDUgMCAtNSBjIDAuOTMgLTQuMjEgMy4wNyAtOC43OSA0IC0xMyBsIDAgLTUiLz48L3N2Zz4="]]></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> 

它会返回错误:

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

而在PDF渲染,但只适用于HTML出口。 正如一些职位的建议,我尝试使用

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

,但它也不能工作。

有人可能会建议一种方法,将在所有类型的出口显示嵌入式图像?

回答

0

我还没有用svg试过它,但我得到它与一个PNG图像工作。 你必须添加导入org.apache.commons.codec.binary.Base64,你应该删除从base64编码数据的标题:data:image/svg+xml;base64,

现在可以解码,并用该表达式传递数据:new ByteArrayInputStream(new Base64().decodeBase64($P{logo}.getBytes("UTF-8")))

在你的情况应该是这样的:在碧玉PDF报告中嵌入SVG的

<?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+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMjQ0IiBoZWlnaHQ9IjgyIj48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDEgMzcgYyAwIC0wLjIxIC0wLjUyIC04LjEzIDAgLTEyIGMgMC43OSAtNS45NSAyLjMxIC0xMi42OCA0IC0xOCBjIDAuNDYgLTEuNDQgMS45IC0yLjkgMyAtNCBjIDAuOCAtMC44IDEuOTggLTEuODggMyAtMiBjIDMuOTYgLTAuNDcgOS40IC0wLjUgMTQgMCBjIDcuNjggMC44MyAxNS43NiAyLjM5IDIzIDQgYyAxLjM5IDAuMzEgMi44MyAxLjE0IDQgMiBjIDMuNzMgMi43MyA3Ljc4IDUuNzggMTEgOSBjIDEuOTUgMS45NSA0LjA2IDQ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Q3IDIuMDkgOS4yOSAzLjk3IDE0IDUgYyA1LjczIDEuMjUgMTEuNzQgMS4zNSAxOCAyIGMgMy44MiAwLjQgNy4yNSAwLjkyIDExIDEgYyAxMy4yNSAwLjI3IDI2LjcyIDEuMTIgMzkgMCBjIDUuMjggLTAuNDggMTAuOTMgLTMuMDcgMTYgLTUgYyAxLjc2IC0wLjY3IDMuODkgLTEuNjkgNSAtMyBjIDIuMjMgLTIuNjMgNC4zNSAtNi41NCA2IC0xMCBjIDEuNjUgLTMuNDcgMy4xMiAtNy40NyA0IC0xMSBjIDAuMzggLTEuNTEgLTAuMzQgLTMuNDUgMCAtNSBjIDAuOTMgLTQuMjEgMy4wNyAtOC43OSA0IC0xMyBsIDAgLTUiLz48L3N2Zz4="]]></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> 
0

我终于解决了问题。

使用原始SVG图像数据而不使用data:image/svg+xml;base64。使用net.sf.jasperreports.renderers.BatikRenderer来渲染SVG图像。 Maven的依赖,包括这个类是以下内容:

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

使用完全合格的类名org.apache.commons.codec.binary.Base64代替Base64。 Maven的依赖,包括这个类是以下内容:

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

碧玉JRXML shold是这样的:

<?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+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMjQ0IiBoZWlnaHQ9IjgyIj48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDEgMzcgYyAwIC0wLjIxIC0wLjUyIC04LjEzIDAgLTEyIGMgMC43OSAtNS45NSAyLjMxIC0xMi42OCA0IC0xOCBjIDAuNDYgLTEuNDQgMS45IC0yLjkgMyAtNCBjIDAuOCAtMC44IDEuOTggLTEuODggMyAtMiBjIDMuOTYgLTAuNDcgOS40IC0wLjUgMTQgMCBjIDcuNjggMC44MyAxNS43NiAyLjM5IDIzIDQgYyAxLjM5IDAuMzEgMi44MyAxLjE0IDQgMiBjIDMuNzMgMi43MyA3Ljc4IDUuNzggMTEgOSBjIDEuOTUgMS45NSA0LjA2IDQuNjUgNSA3IGMgMC44OCAyLjIxIDAuMzkgNS41NiAxIDggYyAwLjM0IDEuMzUgMS44OCAyLjY3IDIgNCBjIDAuNDkgNS4zNSAxLjE5IDEyL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AxMy4yNSAwLjI3IDI2LjcyIDEuMTIgMzkgMCBjIDUuMjggLTAuNDggMTAuOTMgLTMuMDcgMTYgLTUgYyAxLjc2IC0wLjY3IDMuODkgLTEuNjkgNSAtMyBjIDIuMjMgLTIuNjMgNC4zNSAtNi41NCA2IC0xMCBjIDEuNjUgLTMuNDcgMy4xMiAtNy40NyA0IC0xMSBjIDAuMzggLTEuNTEgLTAuMzQgLTMuNDUgMCAtNSBjIDAuOTMgLTQuMjEgMy4wNyAtOC43OSA0IC0xMyBsIDAgLTUiLz48L3N2Zz4="]]></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>