动态报告 - “.toPdf(<<filepath>>)”函数在 Java 中不起作用

Dynamic Reports - ".toPdf(<<filepath>>)" function is not working in Java

我使用的是 Java 8,动态报告版本是 6.0.0。 在 pom.xml 中,我添加了这个依赖项:

<dependency>
    <groupId>net.sourceforge.dynamicreports</groupId>
    <artifactId>dynamicreports-core</artifactId>
    <version>6.0.0</version>
</dependency>

这段代码是生成报表参数:

JasperReportBuilder report = new JasperReportBuilder();
report.setPageMargin(DynamicReports.margin().setLeft(30).setRight(30).setTop(10).setBottom(10));
report.setPageFormat(PageType.A4, PageOrientation.PORTRAIT);
report.title(cmp.subreport(subReport1));

这里,变量subreport1包含了所有的报表数据。他们被验证是正确的。

生成所有报告数据后,这是我执行的将报告详细信息写入文件的代码:

FileOutputStream st = new FileOutputStream("<<DUMMY_FILE_PATH>>");
report.toPdf(st);

在出现 .toPdf 的这一行,抛出了以下异常:

Exception in thread "main" java.lang.AbstractMethodError: javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)V
    at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.<init>(SimpleFontExtensionHelper.java:149)
    at net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.getInstance(SimpleFontExtensionHelper.java:131)
    at net.sf.jasperreports.engine.fonts.FontExtensionsRegistry.ensureFontExtensions(FontExtensionsRegistry.java:80)
    at net.sf.jasperreports.engine.fonts.FontExtensionsRegistry.getExtensions(FontExtensionsRegistry.java:57)
    at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getExtensions(DefaultExtensionsRegistry.java:130)
    at net.sf.jasperreports.engine.DefaultJasperReportsContext.getExtensions(DefaultJasperReportsContext.java:277)
    at net.sf.jasperreports.engine.fonts.FontUtil.getFontInfo(FontUtil.java:191)
    at net.sf.jasperreports.engine.fonts.FontUtil.getFontInfo(FontUtil.java:291)
    at net.sf.jasperreports.engine.fonts.FontUtil.getAwtFontFromBundles(FontUtil.java:476)
    at net.sf.jasperreports.engine.fonts.FontUtil.getAwtFontFromBundles(FontUtil.java:466)
    at net.sf.dynamicreports.design.transformation.StyleResolver.getFont(StyleResolver.java:122)
    at net.sf.dynamicreports.design.transformation.StyleResolver.getFont(StyleResolver.java:100)
    at net.sf.dynamicreports.design.transformation.StyleResolver.getFontHeight(StyleResolver.java:69)
    at net.sf.dynamicreports.design.transformation.TemplateTransform.getTextFieldHeight(TemplateTransform.java:1391)
    at net.sf.dynamicreports.design.transformation.ComponentTransform.textField(ComponentTransform.java:395)
    at net.sf.dynamicreports.design.transformation.ComponentTransform.component(ComponentTransform.java:155)
    at net.sf.dynamicreports.design.transformation.ComponentTransform.list(ComponentTransform.java:303)
    at net.sf.dynamicreports.design.transformation.BandTransform.band(BandTransform.java:231)
    at net.sf.dynamicreports.design.transformation.BandTransform.transform(BandTransform.java:86)
    at net.sf.dynamicreports.design.base.DRDesignReport.transform(DRDesignReport.java:155)
    at net.sf.dynamicreports.design.base.DRDesignReport.<init>(DRDesignReport.java:127)
    at net.sf.dynamicreports.design.base.DRDesignReport.<init>(DRDesignReport.java:111)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperReportDesign(JasperReportBuilder.java:299)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.getJasperParameters(JasperReportBuilder.java:346)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperPrint(JasperReportBuilder.java:363)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.export(JasperReportBuilder.java:896)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toPdf(JasperReportBuilder.java:735)
    at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toPdf(JasperReportBuilder.java:724)

请告诉我这个错误的解决方法。

我找到了实际的解决方案。 xerces jar 和动态报告 jar 之间存在版本不匹配。通过添加以下依赖项解决了这个问题:

<dependency>
    <groupId>xerces</groupId>
    <artifactId>xercesImpl</artifactId>
    <version>2.11.0</version>
</dependency>