使用JasperReports将html内容导出为pdf [英] Export html content to pdf using JasperReports

查看:1396
本文介绍了使用JasperReports将html内容导出为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 JasperReports 将HTML内容导出为PDF格式,我有一个带有HTML类型值的参数,并且必须将其导出为pdf文件,其中pdf必须解析html内容

解决方案

为了在报告中显示 html 代码,我们可以使用由Jaspersoft开发的 htmlcomponent (正如我在查看源代码后所指出的那样,作者是Narcis Marcu)。
$ b 这个组件有很多限制并且不能在任何情况下为任何复杂的html页面提供帮助。无论如何,下面是解释如何使用这个组件。



借助于 iReport 5.6.0 ,该组件可以轻松添加到报告中。 。在 Jaspersoft Studio JSS )的最新版本中,出于某些原因,此组件已从调色板中删除。



iReport中的 组件





使用JSS



如果您使用 JSS ,没有理由不高兴 - 对 Html 组件的支持仍然存在于 Studio 中。你可以在这个文件夹中找到 htmlcomponent.jar Jaspersoft Studio-6.3.1.final \configuration\org.eclipse.osgi\38\0\\我们可以在 JSS 中使用 Generic 组件。 em>使用 Html 组件的所有功能。





为此,我们应该至少设置 Generic 组件的几个属性:


$ b

通用类型名称应该是: htmlelement
$ b $ em <泛型类型命名空间应该是: http ://jasperreports.sourceforge.net / jasperreports / html



我们可以使用 Html 组件的这些属性:


  • scaleType - 图片显示类型。支持下列其中一个值: Clip,FillFrame,RetainShape,RealHeight,RealSize horizo​​ntalAlign - 水平图像对齐。支持以下值之一: Left,Center,Right

  • verticalAlign - 垂直图像对齐。支持以下值之一: Top,Middle,Bottom

  • clipOnOverflow

  • evaluationTime

  • evaluationGroup
  • em> JSS
    这个属性可以在组件属性的高级标签的帮助下设置:





    iReport 更容易设置相同的属性:





    正如我之前提到的, JSS 仍然具有 htmlcomponent 支持。如果您使用 htmlcomponent 创建了 jrxml 文件,那么您可以像使用 iReport 一样查看 JSS 中的所有属性。





    使用htmlcomponent component



    借助于 JRPdfExporter ,我们可以在 pdf 报告中显示这个简单的 html 页>。



    html 页面:

     <!DOCTYPE html PUBLIC -  // W3C // DTD HTML 4.01 // EN> 
    < html>
    < head>
    < title>基于html的报告样本< / title>
    < style type =text / css>
    body {
    padding-left:11em;
    font-family:格鲁吉亚,Times New Roman,
    Times,serif;
    颜色:紫色;
    背景颜色:#a5d8da
    }

    h1 {
    font-family:Helvetica,Geneva,Arial,
    SunSans-Regular,sans-serif
    }
    < / style>
    < / head>

    < body>
    < h1>这是基于html的报告的示例< / h1>

    < p>仅支持最少的html功能< / p>

    < p>至少支持图片< / p>
    < br />< br />

    < img src ='file:// C:\images\smile.png'alt ='Smile'height ='100'width ='100'>
    < / body>
    < / html>

    我们将尝试使用 htmlcomponent wrapper (本地组件),用于 HTML组件通用组件的帮助。


    $ b

    html 代码将通过报表参数(样本中的 htmlCode )传递



    使用本地 htmlcomponent 组件。



    jrxml file:

     <?xml version =1.0encoding =UTF-8?> 
    < jasperReport xmlns =http://jasperreports.sourceforge.net/jasperreportsxmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http: //jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsdname =Html componentpageWidth =595pageHeight =842columnWidth =555leftMargin =20 rightMargin =20topMargin =20bottomMargin =20>
    < parameter name =htmlCodeclass =java.lang.String/>
    < title>
    < band height =742>
    < componentElement>
    < reportElement x =0y =0width =555height =742/>
    < hc:html xmlns:hc =http://jasperreports.sourceforge.net/htmlcomponentxsi:schemaLocation =http://jasperreports.sourceforge.net/htmlcomponent http://jasperreports.sourceforge。 net / xsd / htmlcomponent.xsdscaleType =RetainShapehorizo​​ntalAlign =LeftverticalAlign =Top>
    < hc:htmlContentExpression><![CDATA [$ P {htmlCode}]]>< / hc:htmlContentExpression>
    < / hc:html>
    < / componentElement>
    < / band>
    < / title>
    < / jasperReport>



    使用 组件。



    jrxml 档:

     <?xml version =1.0编码= UTF-8 >?; 
    < jasperReport xmlns =http://jasperreports.sourceforge.net/jasperreportsxmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http: //jasperreports.sourceforge.net/jasperreports_jasperreports.sourceforge.net/xsd/jasperreport.xsdname =Generic builds htmlpageWidth =595pageHeight =842columnWidth =555leftMargin = 20rightMargin =20topMargin =20bottomMargin =20>
    < parameter name =htmlCodeclass =java.lang.String/>
    < title>
    < band height =742>
    < genericElement>
    < reportElement x =0y =0width =555height =742/>
    < genericElementType namespace =http://jasperreports.sourceforge.net/jasperreports/htmlname =htmlelement/>
    < genericElementParameter name =htmlContent>
    < valueExpression><![CDATA [$ P {htmlCode}]]>< / valueExpression>
    < / genericElementParameter>
    < genericElementParameter name =scaleType>
    < valueExpression><![CDATA [RetainShape]]>< / valueExpression>
    < / genericElementParameter>
    < genericElementParameter name =verticalAlign>
    < valueExpression><![CDATA [Top]]>< / valueExpression>
    < / genericElementParameter>
    < genericElementParameter name =horizo​​ntalAlign>
    < valueExpression><![CDATA [Left]]>< / valueExpression>
    < / genericElementParameter>
    < / genericElement>
    < / band>
    < / title>
    < / jasperReport>

    在这两种情况下,我们都可以使用相同的 Java 代码:

     映射< String,Object> params = new HashMap<>(); 
    params.put(htmlCode,<!DOCTYPE html PUBLIC \ - // W3C // DTD HTML 4.01 // EN \> \\\
    +
    << ; html> \\\
    +
    < head> \\\
    +
    < title>基于html的报告样本< / title> \\\
    +
    < style type = \text / css \> \\\
    +
    body {\ n+
    padding-left:11em; \\\
    +
    font-family:Georgia,\Times New Roman \,\\\
    +
    Times,serif; \\\
    +
    color:purple; \ n+
    background-color:#a5d8da\\\
    +
    } \\\
    +
    \\\
    +
    h1 {\ n +
    font-family:Helvetica,Geneva,Arial,\\\
    +
    SunSans-Regular,sans-serif\\\
    +
    } \+
    < / style> \\\
    +
    < / head> \\\
    +
    \\\
    +
    < body> \ n+
    < h1>这是一个html样本+
    \ n+
    < p>只支持最小的html功能< / p> \\\
    +
    \\\
    +
    < p>至少支持图片< / p> \\\
    +
    < br />< br /> \\\
    +
    < img src ='file:/ C:\\images\\smile.png'alt ='Smile'height ='100'width ='100'> \\\
    +
    < / body> \\\
    +
    < / html>);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,params,new JREmptyDataSource());

    为了有效的工作(报告编译),我们应该添加 htmlcomponent.jar 到classpath!



    输出结果



    两种情况的输出结果都是相同:








    更多信息:


    • 关于使用通用元素的信息 - JasperReports - 通用元素示例

    • Petter Friberg: a href =https://stackoverflow.com/q/41023179/876298>如何使用htmlcomponent将HTML文本与嵌入式图像(base64,data uri)一起导出?

    • htmlcomponent 组件和sam的源代码可以在 jasperreports-6.x \demo \samples\htmlcomponent 文件夹
    • JasperReports库
    • 另一个示例可以在 JasperReports library 包的另一个文件夹中找到 - jasperreports-6.x \demo\samples\genericelement


    How do I export HTML content into PDF using JasperReports, I have a parameter which carries value of the type HTML, and I have to export the same to a pdf file, where the pdf has to parse the html content

    解决方案

    For showing html code in report we can use htmlcomponent developed by Jaspersoft (as I understnd after looking at source code the author is Narcis Marcu).

    This component has a lot of limitations and cannot assist you in every case for any html page of any complexity. Anyway, the below is explains how to use this component.

    This component can be easily added to the report with help of iReport 5.6.0. At last versions of Jaspersoft Studio (JSS) this component was removed from the palette for some reasons.

    The Html component in iReport:

    Using JSS

    If you are using the JSS there is no reason to be upset - the support of Html component is still present in Studio. You can find the htmlcomponent.jar in folder like this: Jaspersoft Studio-6.3.1.final\configuration\org.eclipse.osgi\38\0\.cp\lib\.

    We can use Generic component in JSS for using all functionality of Html component.

    For doing this we should set at least couple properties of Generic component:

    Generic Type Name should be: htmlelement
    Generic Type Namespace should be: http://jasperreports.sourceforge.net/jasperreports/html

    We can operate with this properties of Html component:

    • scaleType - Image displaying type. Supports one of this values: Clip, FillFrame, RetainShape, RealHeight, RealSize
    • horizontalAlign - Horizontal image alignment. Supports one of this values: Left, Center, Right
    • verticalAlign - Vertical image alignment. Supports one of this values: Top, Middle, Bottom
    • clipOnOverflow
    • evaluationTime
    • evaluationGroup

    It JSS this properties can be set with help of Advanced tab of component's Properties:

    At iReport its much easier to set the same properties:

    As I mentioned before the JSS still has the htmlcomponent support. If you have jrxml file with htmlcomponent you can see all properties in JSS in the same way as at iReport.

    Sample of using htmlcomponent component

    We can show this simple html page at pdf report with help of JRPdfExporter.

    The html page:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html>
    <head>
        <title>Sample of html based report</title>
        <style type="text/css">
            body {
                padding-left: 11em;
                font-family: Georgia, "Times New Roman",
                Times, serif;
                color: purple;
                background-color: #a5d8da
            }
    
            h1 {
                font-family: Helvetica, Geneva, Arial,
                SunSans-Regular, sans-serif
            }
        </style>
    </head>
    
    <body>
    <h1>This is a sample of html based report</h1>
    
    <p>Only minimal html features are supported</p>
    
    <p>At least images are supported</p>
    <br/><br/>
    
    <img src='file://C:\images\smile.png' alt='Smile' height='100' width='100'>
    </body>
    </html>
    

    We will try to use htmlcomponent with help of wrapper (native component) for HTML component and with help of Generic component.

    The html code will be passed via report's parameter (htmlCode in samples)

    Using native htmlcomponent component.

    The jrxml file:

    <?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="Html component" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
        <parameter name="htmlCode" class="java.lang.String"/>
        <title>
            <band height="742">
                <componentElement>
                    <reportElement x="0" y="0" width="555" height="742"/>
                    <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="Top">
                        <hc:htmlContentExpression><![CDATA[$P{htmlCode}]]></hc:htmlContentExpression>
                    </hc:html>
                </componentElement>
            </band>
        </title>
    </jasperReport>
    

    Using Generic component.

    The jrxml file:

    <?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="Generic builds html" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
        <parameter name="htmlCode" class="java.lang.String"/>
        <title>
            <band height="742">
                <genericElement>
                    <reportElement x="0" y="0" width="555" height="742"/>
                    <genericElementType namespace="http://jasperreports.sourceforge.net/jasperreports/html" name="htmlelement"/>
                    <genericElementParameter name="htmlContent">
                        <valueExpression><![CDATA[$P{htmlCode}]]></valueExpression>
                    </genericElementParameter>
                    <genericElementParameter name="scaleType">
                        <valueExpression><![CDATA["RetainShape"]]></valueExpression>
                    </genericElementParameter>
                    <genericElementParameter name="verticalAlign">
                        <valueExpression><![CDATA["Top"]]></valueExpression>
                    </genericElementParameter>
                    <genericElementParameter name="horizontalAlign">
                        <valueExpression><![CDATA["Left"]]></valueExpression>
                    </genericElementParameter>
                </genericElement>
            </band>
        </title>
    </jasperReport>
    

    In both cases we can use the same Java code:

    Map<String, Object> params = new HashMap<>();
    params.put("htmlCode", "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n" +
            "<html>\n" +
            "<head>\n" +
            "    <title>Sample of html based report</title>\n" +
            "    <style type=\"text/css\">\n" +
            "        body {\n" +
            "            padding-left: 11em;\n" +
            "            font-family: Georgia, \"Times New Roman\",\n" +
            "            Times, serif;\n" +
            "            color: purple;\n" +
            "            background-color: #a5d8da\n" +
            "        }\n" +
            "\n" +
            "        h1 {\n" +
            "            font-family: Helvetica, Geneva, Arial,\n" +
            "            SunSans-Regular, sans-serif\n" +
            "        }\n" +
            "    </style>\n" +
            "</head>\n" +
            "\n" +
            "<body>\n" +
            "<h1>This is a sample of html based report</h1>\n" +
            "\n" +
            "<p>Only minimal html features are supported</p>\n" +
            "\n" +
            "<p>At least images are supported</p>\n" +
            "<br/><br/>\n" +
            "<img src='file:/C:\\images\\smile.png' alt='Smile' height='100' width='100'>\n" +
            "</body>\n" +
            "</html>");
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());
    

    For valid working (report compiling) we should add htmlcomponent.jar to the classpath!

    Output result

    The output result for both cases will be the same:


    More info:

    这篇关于使用JasperReports将html内容导出为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆