如何使用html组件导出包含嵌入图像(base64,data uri)的HTML文本?

时间:2016-12-07 16:58:01

标签: java image jasper-reports base64

我们有来自数据库的HTML,我们想用这个HTML创建一个Word报告。此HTML来自用户,可能只包含格式化文本(粗体,斜体...),或以base64方式编码的文本+嵌入图像。

示例:

<b>My photo :</b>
<img src="data:image/png;base64,iVBORw0K... " />

我们知道,对于textFieldmarkup="html",它不适用于img个html标记。使用Jasper Studio 6.3.1,我们尝试使用<hc:html/>,它适用于硬盘驱动器上的图像,但它不适用于嵌入式图像,说base64加密有语法错误。

jrxml文件:

<?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="StylesReport" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" uuid="53f914b8-f951-4433-971d-6b1819430c56">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <property name="net.sf.jasperreports.export.html.embed.image" value="true"/>
    <import value="org.apache.commons.codec.binar.*"/>
    <title>
        <band height="742">
            <textField hyperlinkType="Reference" hyperlinkTarget="Blank">
                <reportElement x="0" y="30" width="515" height="30" uuid="c2015c9b-9130-4f39-a09e-c341c91d3794"/>
                <textElement textAlignment="Center">
                    <font size="18"/>
                </textElement>
                <textFieldExpression><![CDATA["HTML  Element  Report"]]></textFieldExpression>
            </textField>
            <componentElement>
                <reportElement x="0" y="100" width="230" height="110" backcolor="#ADD8E6" uuid="332dd551-e8cd-4cb0-a11f-7325f481017b"/>
                <hc:html xmlns:hc="http://jasperreports.sourceforge.net/htmlcomponent" xsi:schemaLocation="http://jasperreports.sourceforge.net/htmlcomponent http://jasperreports.sourceforge.net/xsd/htmlcomponent.xsd" scaleType="RetainShape" horizontalAlign="Left" verticalAlign="Middle">
                    <hc:htmlContentExpression><![CDATA["<p style='background-color:yellow;font-family:verdana;font-size:50px;'>Hi, I am through HTML CODE using HTML Component</p><br><br><img src='data:image/gif;base64,/9j/dkjdkdkdkddk(INSERTIMAGEHEREITSTOOLONG!!!)' alt='Japser Architecture' height='1000' width='1800'>"]]></hc:htmlContentExpression>
                </hc:html>
            </componentElement>
        </band>
    </title>
</jasperReport>

是否可以使用Jasper执行此操作?我们不想从数据库中解析HTML来做很多事情,我们真的希望在报告中按原样编写HTML。

1 个答案:

答案 0 :(得分:6)

这是一个很好的挑战,问题是它不受支持,但并不意味着它无法完成。

html组件使用JEditorPane将html呈现为图像,并且没有默认的协议处理程序,用于&#34;数据:&#34;

answer这个优秀的Joop Eggen显示了如何添加一个output

实施例

jrxml (htmlComponentBase64.jrxml)

<?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="StylesReport" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" uuid="53f914b8-f951-4433-971d-6b1819430c56">
    <title>
        <band height="742">
            <textField hyperlinkType="Reference" hyperlinkTarget="Blank">
                <reportElement x="0" y="30" width="515" height="30" uuid="c2015c9b-9130-4f39-a09e-c341c91d3794"/>
                <textElement textAlignment="Center">
                    <font size="18"/>
                </textElement>
                <textFieldExpression><![CDATA["HTML  Element  Report"]]></textFieldExpression>
            </textField>
            <componentElement>
                <reportElement x="0" y="100" width="230" height="110" backcolor="#ADD8E6" uuid="332dd551-e8cd-4cb0-a11f-7325f481017b"/>
                <hc:html xmlns:hc="http://jasperreports.sourceforge.net/htmlcomponent" xsi:schemaLocation="http://jasperreports.sourceforge.net/htmlcomponent http://jasperreports.sourceforge.net/xsd/htmlcomponent.xsd" scaleType="RetainShape" horizontalAlign="Left" verticalAlign="Middle">
                    <hc:htmlContentExpression><![CDATA["<p style='background-color:yellow;font-family:verdana;font-size:50px;'>Hi, I am through HTML CODE using HTML Component</p><br><br><img src='data:image/gif;base64,R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj8wZAKUsAOzZz+UMAOsJAP/Z2ccMDA8PD/95eX5NWvsJCOVNQPtfX/8zM8+QePLl38MGBr8JCP+zs9myn/8GBqwpAP/GxgwJCPny78lzYLgjAJ8vAP9fX/+MjMUcAN8zM/9wcM8ZGcATEL+QePdZWf/29uc/P9cmJu9MTDImIN+/r7+/vz8/P8VNQGNugV8AAF9fX8swMNgTAFlDOICAgPNSUnNWSMQ5MBAQEJE3QPIGAM9AQMqGcG9vb6MhJsEdGM8vLx8fH98AANIWAMuQeL8fABkTEPPQ0OM5OSYdGFl5jo+Pj/+pqcsTE78wMFNGQLYmID4dGPvd3UBAQJmTkP+8vH9QUK+vr8ZWSHpzcJMmILdwcLOGcHRQUHxwcK9PT9DQ0O/v70w5MLypoG8wKOuwsP/g4P/Q0IcwKEswKMl8aJ9fX2xjdOtGRs/Pz+Dg4GImIP8gIH0sKEAwKKmTiKZ8aB/f39Wsl+LFt8dgUE9PT5x5aHBwcP+AgP+WltdgYMyZfyywz78AAAAAAAD///8AAP9mZv///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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cKE0ywc0ELm6KBCCJo8DIPFeCWNGcyqNFE06ToAfV0HBRgxsvLThHn1oddQMrXj5DyAQgjEHSAJMWZwS3HPxT/QMbabI/iBCliMLEJKX2EEkomBAUCxRi42VDADxyTYDVogV+wSChqmKxEKCDAYFDFj4OmwbY7bDGdBhtrnTQYOigeChUmc1K3QTnAUfEgGFgAWt88hKA6aCRIXhxnQ1yg3BCayK44EWdkUQcBByEQChFXfCB776aQsG0BIlQgQgE8qO26X1h8cEUep8ngRBnOy74E9QgRgEAC8SvOfQkh7FDBDmS43PmGoIiKUUEGkMEC/PJHgxw0xH74yx/3XnaYRJgMB8obxQW6kL9QYEJ0FIFgByfIL7/IQAlvQwEpnAC7DtLNJCKUoO/w45c44GwCXiAFB/OXAATQryUxdN4LfFiwgjCNYg+kYMIEFkCKDs6PKAIJouyGWMS1FSKJOMRB/BoIxYJIUXFUxNwoIkEKPAgCBZSQHQ1A2EWDfDEUVLyADj5AChSIQW6gu10bE/JG2VnCZGfo4R4d0sdQoBAHhPjhIB94v/wRoRKQWGRHgrhGSQJxCS+0pCZbEhAAOw==' alt='Japser Architecture' height='1000' width='1800'>"]]></hc:htmlContentExpression>
                </hc:html>
            </componentElement>
        </band>
    </title>
</jasperReport>

<强>的java

添加Joop Eggen的协议处理程序(记得将他归因于也可能是一个不错的upvote)并执行此java代码,在这种情况下导出为pdf

public class TestReport {
    public static void main(String[] args) throws JRException {

        Handler.install(); //Install Joop's protocol handler

         //Compile report and fill, no datasource needed
        JasperReport report = JasperCompileManager.compileReport("htmlComponentBase64.jrxml");
        JasperPrint jasperPrint = JasperFillManager.fillReport(report, new HashMap<String, Object>());

        //Export to pdf
        JRPdfExporter exporter = new JRPdfExporter();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("pdf/htmlcomponentbase64.pdf"));
        SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
        exporter.setConfiguration(configuration);
        exporter.exportReport();

    }
}

享受结果

feature request

不是最好的报告,但会显示图像

  

我在jasper-reports社区中添加了enter image description here,以便在将来的版本中包含数据协议处理程序