java – 如何将报告导出为PDF / A-1a,PDF / A-1b?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 如何将报告导出为PDF / A-1a,PDF / A-1b?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6347字,纯文字阅读大概需要10分钟。
内容图文
![java – 如何将报告导出为PDF / A-1a,PDF / A-1b?](/upload/InfoBanner/zyjiaocheng/711/a79e16d4a9674f0491ef8b23d714064b.jpg)
在jasper-report中生成PDF / A,包含许多陷阱,并且在某些版本的jasper-report中不受支持.这就是为什么我决定通过这个问答帖子,指出将带图表的简单报告导出为PDF / A所需的步骤和库版本
示例数据(usersRep.csv)
+----------------+--------+
| User | Rep |
+----------------+--------+
| Jon Skeet | 854503 |
| Darin Dimitrov | 652133 |
| BalusC | 639753 |
| Hans Passant | 616871 |
| Me | 5640 |
+----------------+--------+
示例jrxml(reputation.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="reputation" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a88bd694-4f90-41fc-84d0-002b90b2d73e">
<queryString>
<![CDATA[]]>
</queryString>
<field name="User" class="java.lang.String"/>
<field name="Rep" class="java.lang.Long"/>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="100" height="20" uuid="9e7b5f50-5795-4c95-a122-f14f2e3f9366"/>
<box leftPadding="3" bottomPadding="0" rightPadding="3">
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.5" lineStyle="Double"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement verticalAlignment="Middle">
<font fontName="SansSerif" isBold="true"/>
</textElement>
<text><![CDATA[User]]></text>
</staticText>
<staticText>
<reportElement x="100" y="0" width="100" height="20" uuid="4a6f0a2a-d9b5-4e74-a9e8-0f965336f2bf"/>
<box leftPadding="3" bottomPadding="0" rightPadding="3">
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.5" lineStyle="Double"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font fontName="SansSerif" isBold="true"/>
</textElement>
<text><![CDATA[Reputation]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="8ff583b9-88dc-4726-85e1-16d79de78095"/>
<box leftPadding="3" bottomPadding="0" rightPadding="3">
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement verticalAlignment="Middle">
<font fontName="SansSerif"/>
</textElement>
<textFieldExpression><![CDATA[$F{User}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20" uuid="ebd33b2f-7297-41c2-9dc7-78ff472890c4"/>
<box leftPadding="3" bottomPadding="0" rightPadding="3">
<pen lineWidth="0.25"/>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font fontName="SansSerif"/>
</textElement>
<textFieldExpression><![CDATA[$F{Rep}]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="140">
<pieChart>
<chart isShowLegend="false">
<reportElement x="225" y="-670" width="320" height="140" uuid="23bd26a6-04a4-406f-8a1a-5e1b260cb75d"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<pieDataset>
<keyExpression><![CDATA[$F{User}]]></keyExpression>
<valueExpression><![CDATA[$F{Rep}]]></valueExpression>
</pieDataset>
<piePlot>
<plot/>
<itemLabel/>
</piePlot>
</pieChart>
</band>
</pageFooter>
</jasperReport>
要导出为PDF的Java代码(信誉.pdf)
JasperReport report = JasperCompileManager.compileReport("reputation.jrxml");
JRCsvDataSource datasource = new JRCsvDataSource("usersRep.csv");
datasource.setFieldDelimiter(';');
datasource.setUseFirstRowAsHeader(true);
JasperPrint jasperPrint = JasperFillManager.fillReport(report, new HashMap<String, Object>(),datasource);
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("reputation.pdf"));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
configuration.setMetadataAuthor("Me and only me");
exporter.setConfiguration(configuration);
exporter.exportReport();
这会将报告导出为pdf,生成PDF / A-1a需要做什么?
解决方法:
JasperReports Library 4.1.2.3或更高版本is needed(6.0.0中已停止支持,最后请参见NullPointerException).
这些步骤需要生成PDF / A,它们既可以通过java代码实现,也可以通过将jrxml属性设置为root标签(jasper-server support)来实现.我将展示两者,但只需要一种方法.
设置PDF / A一致性
java的
configuration.setPdfaConformance(PdfaConformanceEnum.PDFA_1A); // or PdfaConformanceEnum.PDFA_1B
JRXML
<property name="net.sf.jasperreports.export.pdfa.conformance" value="pdfa1a" />
to avoid
JRPdfaIccProfileNotFoundException: The ICC profile is not
available to the JVM
java的
configuration.setIccProfilePath("srgb.icc");
JRXML
<property name="net.sf.jasperreports.export.pdfa.icc.profile.path" value="srgb.icc" />
使用font-extensions嵌入报告中使用的所有字体
如果你还有错误
com.lowagie.text.pdf.PdfXConformanceException: All the fonts must be embedded. This one isn't: Helvetica
在jrxml中包含一个默认样式,表示字体扩展中包含的fontName,例如
<style name="default" isDefault="true" fontName="DejaVu Sans"/>
删除透明对象和图层(可选内容组),它们是not allowed
to avoid
PdfXConformanceException: Transparency is not allowed
在示例中,图表元素必须是不透明的,并且为了避免标签上的透明度,您可以实现JRChartCustomizer
public class NoTransparencyCustomizer implements JRChartCustomizer{
@Override
public void customize(JFreeChart chart, JRChart jrchart) {
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelShadowPaint(Color.GRAY);
}
}
设置标记和标记语言(PDF / A-1b不需要)
java的
configuration.setTagged(true);
configuration.setTagLanguage("en-us");
JRXML
<property name="net.sf.jasperreports.export.pdf.tagged" value="true" />
<property name="net.sf.jasperreports.export.pdf.tag.language" value="en-us"/>
结果
这是实现上述结果的结果,将fontName切换为DejaVu Sans并使用捆绑的jasperreports-fonts.jar作为font-extension.它已在pdf-tools成功验证PDF / A-1a和PDF / A-1b
对我来说没有一块蛋糕
停止支持
在jasper报告版本6.0.0中,始终抛出com.itextpdf.text.pdf.internal.PdfA1Checker.checkPdfObject中的NullPointerException.这已在6.0.4及以上解决,见Jasper report tracker.
内容总结
以上是互联网集市为您收集整理的java – 如何将报告导出为PDF / A-1a,PDF / A-1b?全部内容,希望文章能够帮你解决java – 如何将报告导出为PDF / A-1a,PDF / A-1b?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。