为什么在将 HTML 传递给 html 组件 (hc:html) 时忽略字体大小?

Why is font-size ignored when passing an HTML to the html component (hc:html)?

我们正在我们的应用程序上构建报告功能,我们通过 html 将其传递到 jasper 以将其导出为 pdf。

我们 运行 遇到了一个问题,即 HTML 代码中指定的字体大小没有被 jasper 导出器 "read" 获取,我们拥有的内容越多,报告越小。

这里是 HTML:

<html>
<head>
    <style>
        body {
            font-family: "Trebuchet MS",Arial;
            color: #222222;
            background: #F4F0E8;
            font-size:9.0pt;
        }
        .Izquierda{
            float:left;
        }
        .Derecha{
            float:right;
        }
        .CabeceraInicio{
            margin-bottom:10px;
        }

        .filaN1{
            margin-top: 2px;
            text-align: left;
            line-height: 14px;
            font-size:9.0pt;
            font-weight: bold;
            margin-bottom:2px;
        }
        div.CabeceraInicio,a {
            color: #000033;
        }
        .infoTarea{
            font-size:9.0pt;
            font-weight: bold;
            color:#000033;
            text-align: left;
        }
        .TextoDescripcion{
            width: 90px;
            color: #777777;
        }
        table {
            border-collapse: collapse;
            font-size:9.0pt;
            line-height: 17px;
        }
        div.pie {
            font-size:7.0pt;
            color: #777777;
            text-align: center;
        }
        .solucion {
            margin-left:30px;
            /*padding-left:5px;*/
            /*border-left: thin solid grey;*/
        }
        .nuevasComunicaciones{
            color: brown;
            margin-left:8px;
        }

/*          } */
/*      @media print{ */
/*          a:after{content:" (" attr(href) ") ";font-size:0.8em;font-weight:normal;} */
/*      } */

    </style>
</head>
<body>
<div class="CabeceraInicio">
    <table style="width:100%;">
        <tr>
            <td>
                        GT <a rel="nofollow" href='http://*IPIntranet*:8080/StsPyme/Pyme/IncidenciasyTareas/SeguimientoIncidencias/MaestroTareasMail.zul?start=yes&emp=*Empresa*&idtarea=*Codigo*&ip=*ip*&cuenta=*cuenta*'>_Acceso *TipoTarea* _Intranet</a>
            <!-- http://*IPIntranet*:8080/StsWeb/Pyme/IncidenciasyTareas/SeguimientoIncidencias/MaestroTareas.zul?start=yes&emp=*Empresa*&idtarea=*Codigo* -->
<!--                GT <a href='http://*IPIntranet*:8080/StsWeb/Pyme/IncidenciasyTareas/SeguimientoIncidencias/SeguimientoTareas.zul'>_Acceso *TipoTarea* _Intranet</a> -->
            </td>
            <td style="text-align:right;">
                        <a href='http://*IPInternet*:8080/StsPyme/Pyme/IncidenciasyTareas/SeguimientoIncidencias/MaestroTareasMail.zul?start=yes&emp=*Empresa*&idtarea=*Codigo*&ip=*ip*&cuenta=*cuenta*'>_Acceso *TipoTarea* _Internet</a>
<!--                <a href='http://*IPInternet*:8080/StsWeb/Pyme/IncidenciasyTareas/SeguimientoIncidencias/SeguimientoTareas.zul'>_Acceso *TipoTarea* _Internet</a> -->
            </td>
        </tr>
    </table>
</div>
<br/>
<table style="width:100%;">
    <tr>
        <td>
            <span class="filaN1 infoTarea">*Accion* *TipoTarea* *FechaAccion* *DiaSemana* *HoraAccion* *NombreUsuarioAccion*</span>
        </td>
        <td style="text-align:right;">
            <span class="filaN1 infoTarea ">*NombreEmpresa*</span>
        </td>
    </tr>
</table>
<hr/>
<br/>
<div class="Izquierda" style="font-weight: bold;">
    *EstadoTarea*
</div>
<table style="width:100%;">
    <tr>
        <td class="infoTarea">
            _Informacion *TipoTarea*
        </td>
        <td style="text-align:right;">
            <span class="TextoDescripcion">_CentroExplotacion</span><span class="TextoValor"> *CentroExplotacion*</span>
        </td>
    </tr>
</table>
<hr/>
<br/>
<div class="InfoTarea" style="width:100%">
    <table style="width:100%">
        <tr>
            <td class="TextoDescripcion" valign="top">
                  _Asunto
            </td>
            <td colspan="3" valign="top">
                 *Asunto*
            </td>
        </tr>
        <tr>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                 _Codigo
            </td>
            <td valign="top" style="width:35%">
                 *Codigo*
            </td>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                 _Solicitante
            </td>
            <td valign="top" style="width:35%">
                 *NombreSolicitante*
            </td>
        </tr>
        <tr>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                 _Ambito
            </td>
            <td valign="top" style="width:35%">
                 *Ambito*
            </td>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                 _Responsable
            </td>
            <td valign="top" style="width:35%">
                  *NombreResponsable*
            </td>
        </tr>
        <tr>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                  _Tipo
            </td>
            <td valign="top" style="width:35%">
                 *Tipo*
            </td>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                 _Grado
            </td>
            <td valign="top" style="width:35%">
                  *Grado*
            </td>
        </tr>
        <tr>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                  _Sub Tipo
            </td>
            <td valign="top" style="width:35%">
                 *SubTipo*
            </td>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                 _Prioridad
            </td>
            <td valign="top" style="width:35%">
                  *Prioridad*
            </td>

        </tr>
        <tr>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                  _Relacion
            </td>
            <td valign="top" style="width:35%">
                 *Relacion*
            </td>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                 _Estado
            </td>
            <td valign="top" style="width:35%">
                 *EstadoTarea*
            </td>
        </tr>
    </table>
    <table style="width:100%;margin-top:15px;">
        <tr>
            <td class="TextoDescripcion" valign="top" style="width:15%;">
                  _NumReferencia
            </td>
            <td valign="top">
                 *NumReferencia*
            </td>
        </tr>
    </table>
</div>
<hr/>
<br/>
    <div class="InfoTarea" style="width:100%">
        <table style="width:100%">
            <tr>
                <td class="TextoDescripcion" valign="top">
                    <span class="infoTarea">
                        _Solicitud
                    </span>
                </td>
            </tr>
            <tr>
                <td valign="top" style="padding-bottom:10px;">          
                    *Solicitud*
                </td>
            </tr>
            <tr>
                <td class="TextoDescripcion" valign="top">
                    <span class="infoTarea">
                        _Descripcion
                    </span>
                </td>
            </tr>
            <tr>
                <td valign="top" style="padding-bottom:10px;">          
                    *Descripcion*
                </td>
            </tr>
            <tr>
                <td class="TextoDescripcion" valign="top">
                    <span class="infoTarea">
                        _Informe
                    </span>
                </td>
            </tr>
            <tr>
                <td valign="top" style="padding-bottom:10px;">
                    *Informe*
                </td>
            </tr>
            <tr>
                <td class="TextoDescripcion" valign="top" style="width:auto">
                    <span class="infoTarea">
                        _ArchivosAdjuntos
                    </span>
                </td>
            </tr>
            <tr>
                <td valign="top">
                    *TablaArchivosAdjuntos*
                </td>
            </tr>
        </table>
    </div>
<hr/>
<br/>
    <table style="width:100%">
        <tr>
            <td class="TextoDescripcion" valign="top">
                <span class="infoTarea">
                    _Comunicaciones
                </span>
            </td>
        </tr>
        <tr>
            <td valign="top">
            *Comunicaciones*
            </td>
        </tr>
    </table><hr id="HRcomunicaciones"/><br/>
    <table style="width:100%">
        <tr>
            <td class="TextoDescripcion" style="width:99%" valign="top">
                <span class="infoTarea">
                    _OtrasComunicaciones
                </span>
            </td>
        </tr>
        <tr>
            <td valign="top">
                *OtrasComunicaciones*
            </td>
        </tr>
    </table>
        <!--  </table> --><hr/>
    <table style="width:100%">
        <tr>
            <td class="TextoDescripcion" style="width:99%" valign="top">
                <span class="infoTarea">
                    _textoCambioValidacion
                </span>
            </td>
        </tr>
        <tr>
            <td valign="top">
                *textoCambioValidacion*
            </td>
        </tr>
    </table>
<!-- <hr/> -->
<!-- <div class="InfoTarea">
    <table style="width:100%">
        <tr>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                <span class="infoTarea">
                    _Solucion
                </span>
            </td>
        </tr>
        <tr>
            <td valign="top">
                 *Solucion*
            </td>
        </tr>
    </table>
</div> -->
<br>
</body>
</html>

这里是 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="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="htmlCode" class="java.lang.String"/>
    <title>
        <band height="742">
            <componentElement>
                <reportElement x="0" y="0" width="555" height="742" uuid="8544346f-4c98-4069-a041-f6080a75e906"/>
                <hc:html xmlns:hc="http://jasperreports.sourceforge.net/htmlcomponent" xsi:schemaLocation="http://jasperreports.sourceforge.net/htmlcomponent http://jasperreports.sourceforge.net/xsd/htmlcomponent.xsd" scaleType="RealSize" horizontalAlign="Left" verticalAlign="Top">
                    <hc:htmlContentExpression><![CDATA[$P{htmlCode}]]></hc:htmlContentExpression>
                </hc:html>
            </componentElement>
        </band>
    </title>
</jasperReport>

这里是导出的 Java 方法:

public String generarReportePdfAdjunto(String html, ArrayList<String> pathsImagenes){
        HashMap hm = null;
        String jrxmlFileName = "";
        String jasperFileName = "";
        String rutaPrincipal = "";
        InputStream reportStream = null;
        jasperFileName = "/frameHtmlv2.jasper";
        reportStream = funciones.class.getResourceAsStream(jasperFileName);             
        String pdfFileName ="*pathToPdf*";
        hm = new HashMap<>();
        hm.put("htmlCode",html);
        hm.put("scaleType", "RealSize");
        // Generate jasper print, llena el report y renderiza el pdf
          JasperPrint jprint = JasperFillManager.fillReport(reportStream, hm, new JREmptyDataSource());
          JasperExportManager.exportReportToPdfFile(jprint, pdfFileName);
}

html 组件 (hc:html) 使用 JEditorPane 将 html 渲染为图像

如您在文档中所见:

HTML text. The kit used in this case is the class javax.swing.text.html.HTMLEditorKit which provides HTML 3.2 support.

也在这些帖子中看到 Which HTML tags are supported in Swing components? and Which HTML tags are supported in Swing components? 你需要 使用 HTML 3.2

那我的html怎么了?

使用https://validator.w3.org selecting DOCTYPE 3.2, passing your HTML I get 31 Errors, 19 warning(s), for example these

解法:

通过HTML符合HTML3.2