Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
894 views
in Technique[技术] by (71.8m points)

jasper reports - Export html content to pdf using JasperReports

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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.finalconfigurationorg.eclipse.osgi38.cplib.

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

How to add Generic component in JSS

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

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:

Setting properties in JSS

At iReport its much easier to set the same properties:

Setting properties in iReport

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.

Setting properties in JSS for template with htmlcomponent

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:imagessmile.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">
" +
        "<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>");
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:

The pdf file generated with JRPdfExporter


More info:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...