首页 > 解决方案 > 如何打印/显示 MathML 表达式?

问题描述

目前我正在尝试使用 textFieldmarkup="html"但它不起作用。

例子

<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement positionType="Float" stretchType="RelativeToTallestObject" x="100" y="0" width="450" height="25" isPrintWhenDetailOverflows="true" uuid="1aeaa5e9-4136-4239-a301-2733598340d9">
    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
</reportElement>
<textElement verticalAlignment="Middle" markup="html">
    <paragraph lineSpacing="Single" leftIndent="5" rightIndent="3"/>
</textElement>
<textFieldExpression><![CDATA[$F{question}]]></textFieldExpression>

$F{question} 包含:

"<p id="id"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>A</mi><mo>+</mo><mo>&#160;</mo><mi>B</mi><mo>&#160;</mo><mo>&#160;</mo><msqrt><mi>c</mi><mfenced><mrow><mi>d</mi><mfenced open="[" close="]"><mi>r</mi></mfenced></mrow></mfenced></msqrt><mo>&#160;</mo><mi>&#948;</mi><mo>&#160;</mo><mo>&#8734;</mo><mi mathvariant="normal">&#960;</mi><mo>&#160;</mo></math></p>"

预期结果 :

预期结果

我得到的结果:

我得到的结果

标签: javajasper-reportsmathml

解决方案


textField 不支持MathML,它只支持非常基本的 html,也不能使用 html 组件,因为构建它的 JEditorPane 也不支持 MathML

您将需要一个外部库,例如jeuclid,一旦您在类路径中有这个库,您就可以将 xml 渲染为BufferedImageusing Converter.render,然后在 jasper 报告中显示它。

这可以在没有 java helper 类的情况下完成,如本例所示,因此将所有代码直接写入 jrxml 的 1 行(使用下面的构建器模式),但为了示例清晰,我将使用外部帮助器类。

例子

爪哇

public class MathML {
     public static BufferedImage getImage(String xml, float size) throws IOException, SAXException, ParserConfigurationException {          
        // Load the string to a node
        Element node =  DocumentBuilderFactory
                .newInstance()
                .newDocumentBuilder()
                .parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)))
                .getDocumentElement();              
        //Generate the layout parameter
        MutableLayoutContext params = new LayoutContextImpl(
                LayoutContextImpl.getDefaultLayoutContext());
        params.setParameter(Parameter.MATHSIZE, size);      
        //Render the xml to a BufferedImage
        return Converter.getInstance().render(node, params);
    }
}

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="Blank_A4_12" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8deaea2e-3739-4c4e-b2b5-8c58773ab1a0">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="50" splitType="Stretch">
            <image>
                <reportElement x="0" y="0" width="100" height="50" uuid="9ee17167-a91e-4725-b36e-a4bba5e24acb">
                </reportElement>
                <imageExpression><![CDATA[it.jdd.MathML.getImage("<math xmlns='http://www.w3.org/1998/Math/MathML'><mi>A</mi><mo>+</mo><mo>&#160;</mo><mi>B</mi><mo>&#160;</mo><mo>&#160;</mo><msqrt><mi>c</mi><mfenced><mrow><mi>d</mi><mfenced open='[' close=']'><mi>r</mi></mfenced></mrow></mfenced></msqrt><mo>&#160;</mo><mi>&#948;</mi><mo>&#160;</mo><mo>&#8734;</mo><mi mathvariant='normal'>&#960;</mi><mo>&#160;</mo></math>",70f)]]></imageExpression>
            </image>
        </band>
    </title>
</jasperReport>

输出(导出为 pdf)

导出为 pdf


推荐阅读