使用 JasperReports 将 html 内容导出为 pdf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1091760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:08:47  来源:igfitidea点击:

Export html content to pdf using JasperReports

htmljasper-reportsexport-to-pdf

提问by stanley

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

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

回答by Alex K

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

为了在报告中显示html代码,我们可以使用Jaspersoft开发的htmlcomponent(因为我在查看源代码后明白作者是 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.

这个组件有很多限制,不能在任何情况下为任何复杂的 html 页面提供帮助。不管怎样,下面是解释如何使用这个组件。

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.

iReport 5.6.0 的帮助下,这个组件可以很容易地添加到报告中。在Jaspersoft Studio( JSS) 的最新版本中,由于某些原因,该组件已从调色板中删除。

The Htmlcomponent in iReport:

iReport 中Html组件:

Html component in iReport

iReport 中的 HTML 组件

Using JSS

使用 JSS

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

如果您使用的是JSS,没有理由感到不安 - Studio 中仍然存在对Html组件的支持。你可以找到htmlcomponent.jar在这样的文件夹:。Jaspersoft Studio-6.3.1.final\configuration\org.eclipse.osgi\38\0\.cp\lib\

We can use Genericcomponent in JSSfor using all functionality of Htmlcomponent.

我们可以在JSS 中使用通用组件来使用Html组件的所有功能。

How to add Generic component in JSS

如何在JSS中添加通用组件

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

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

Properties of Generic component

通用组件的属性

Generic Type Nameshould be: htmlelement
Generic Type Namespaceshould be: http://jasperreports.sourceforge.net/jasperreports/html

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

We can operate with this properties of Htmlcomponent:

我们可以对Html组件的这个属性进行操作:

  • 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
  • scaleType- 图像显示类型。支持以下值之一:Clip、FillFrame、RetainShape、RealHeight、RealSize
  • 水平对齐 - 水平图像对齐。支持以下值之一:Left、Center、Right
  • VerticalAlign- 垂直图像对齐。支持以下值之一:Top、Middle、Bottom
  • 剪辑溢出
  • 评估时间
  • 评估组

It JSSthis properties can be set with help of Advancedtab of component's Properties:

JSS这个特性可以帮助设置高级组件属性选项卡:

Setting properties in JSS

在 JSS 中设置属性

At iReportits much easier to set the same properties:

iReport 中,设置相同的属性要容易得多:

Setting properties in iReport

在 iReport 中设置属性

As I mentioned before the JSSstill has the htmlcomponentsupport. If you have jrxmlfile with htmlcomponentyou can see all properties in JSSin the same way as at iReport.

正如我之前提到的,JSS仍然具有htmlcomponent支持。如果您有带有htmlcomponent 的jrxml文件,您可以以与iReport相同的方式查看JSS中的所有属性。

Setting properties in JSS for template with htmlcomponent

使用 htmlcomponent 在 JSS 中为模板设置属性

Sample of using htmlcomponent component

使用 htmlcomponent 组件的示例

We can show this simple htmlpage at pdfreport with help of JRPdfExporter.

我们可以在JRpdfExporter 的帮助下在pdf报告中显示这个简单的html页面。

The htmlpage:

HTML页面:

<!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 htmlcomponentwith help of wrapper(native component) for HTML componentand with help of Genericcomponent.

我们将尝试在HTML 组件包装器(本机组件)和通用组件的帮助下使用htmlcomponent

The htmlcode will be passed via report's parameter (htmlCodein samples)

HTML代码将通过报告的参数传递(htmlCode样品)

Using native htmlcomponentcomponent.

使用原生htmlcomponent组件。

The jrxmlfile:

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="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 Genericcomponent.

使用通用组件。

The jrxmlfile:

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="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 Javacode:

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

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.jarto the classpath!

为了有效工作(报告编译),我们应该将htmlcomponent.jar添加到类路径中!

Output result

输出结果

The output result for both cases will be the same:

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

The pdf file generated with JRPdfExporter

用JRPDFExporter生成的pdf文件



More info:

更多信息: