Export html content to pdf using JasperReports

How to export HTML content to PDF using JasperReports , I have a parameter that carries a value of type HTML, and I have to export it to a pdf file, where pdf should parse the html content

+4
source share
1 answer

To display the html code in the report, we can use the htmlcomponent developed by Jaspersoft (since I understand that after studying the source code, the author is Narcis Marku).

This component has many limitations and cannot help you in every case for any html page of any complexity. In any case, the following describes how to use this component.

This component can be easily added to the report using iReport 5.6.0. In recent versions of Jaspersoft Studio (JSS), this component has been removed from the palette for some reason.

Html component in iReport:

Html component in iReport

Using JSS

If you use JSS, there is no reason to be upset - support for the Html component is still present in Studio. You can find htmlcomponent.jar in the folder as follows: Jaspersoft Studio-6.3.1.final\configuration\org.eclipse.osgi\38\0\.cp\lib\ .

We can use a common component in JSS to use all the functions of the Html component.

How to add a common component in JSS

To do this, we must set at least a couple of properties of the Generic component:

Common Component Properties

The generic type name should be: htmlelement
The generic type namespace should be: http://jasperreports.sourceforge.net/jasperreports/html

We can work with these properties of the Html component:

  • scaleType - type of image display. Supports one of the following values: Clip, FillFrame, RetainShape, RealHeight, RealSize
  • horizontalAlign Horizontal image alignment. Supports one of the following values: Left, Center, Right
  • verticalAlign . Vertical image alignment. Supports one of the following values: Upper, Middle, Lower
  • clipOnOverflow
  • evaluationTime
  • evaluationGroup

This is JSS. These properties can be set using the Advanced tab of the Properties component:

Setting properties in JSS

In iReport it is much easier to set the same properties:

Setting properties in iReport

As I said, JSS still supports the html component. If you have a jrxml file with htmlcomponent, you can see all the properties in JSS in the same way as in iReport.

Setting properties in JSS for a template with htmlcomponent

An example of using the html component

We can show this simple html page in pdf format using JRPdfExporter.

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 using the shell (native component) for the HTML component and using the Generic component.

The html code will be passed through the report parameter ( htmlCode in the samples)

Using native component html component.

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 a common component.

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 proper operation (report compilation) we must add htmlcomponent.jar to the classpath!

Output result

The output result for both cases will be the same:

Pdf file created with JRPdfExporter


Additional Information:

+3
source

All Articles