簡體   English   中英

如何將報告導出為 PDF/A-1a、PDF/A-1b?

[英]How can I export report to PDF/A-1a, PDF/A-1b?

在 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 代碼(reputation.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的庫4.1.2.3或更高的需要(在6.0.0看到停止支持NullPointerException末)。

這些步驟是生成 PDF/A 所需要的,它們可以通過 java 代碼或通過將 jrxml property設置為根標簽(jasper-server 支持)來實現。 我將展示兩者,但只有一種方法是必要的

#設置 PDF/A 一致性

爪哇

configuration.setPdfaConformance(PdfaConformanceEnum.PDFA_1A); // or PdfaConformanceEnum.PDFA_1B

xml文件

<property name="net.sf.jasperreports.export.pdfa.conformance" value="pdfa1a" />

#設置ICC配置文件

避免JRPdfaIccProfileNotFoundException: The ICC profile is not available to the JVM

爪哇

configuration.setIccProfilePath("srgb.icc");

xml文件

<property name="net.sf.jasperreports.export.pdfa.icc.profile.path" value="srgb.icc" />

#嵌入報告中使用的所有字體,使用字體擴展

如果還有錯誤

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"/>

#Remove透明對象和層(可選內容組)它們不允許

避免PdfXConformanceException: Transparency is not allowed

例如,圖表元素必須是Opaque ,為了避免標簽上的透明,您可以實現JRChartCustomizer

public class NoTransparencyCustomizer implements JRChartCustomizer{
    @Override
    public void customize(JFreeChart chart, JRChart jrchart) {
        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelShadowPaint(Color.GRAY);       
    }
}

#Set Tagged 和標簽語言(PDF/A-1b 不需要)

爪哇

configuration.setTagged(true);
configuration.setTagLanguage("en-us");

xml文件

<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作為字體擴展。 它已在 PDF/A-1a 和 PDF/A-1b 的pdf-tools上成功驗證

結果

對我來說沒有一塊蛋糕

XMP 屬性與文檔信息不同步

Validating file "reputation.pdf" for conformance level pdfa-1a
dc:description/*[0] :: Missing language qualifier.
dc:title/*[0] :: Missing language qualifier.
The XMP property 'dc:title' is not synchronized with the document information entry 'Title'.
The XMP property 'dc:description' is not synchronized with the document information entry 'Subject'.

此錯誤來自在配置中設置元數據標題或主題時使用較舊的 jasper-reports 庫 <6.2.0。

configuration.setMetadataTitle("Title");
configuration.setMetadataSubject("Subject");

解決方案是刪除這些或將 jasper-reports 更新到 versione 6.2.0 或更高版本,請參閱PDF/A_1A XMP 元數據驗證失敗,如果設置標題和/或主題以獲取更多信息

停止支持在 jasper 報告版本 6.0.0 中,始終拋出NullPointerException at com.itextpdf.text.pdf.internal.PdfA1Checker.checkPdfObjectNullPointerException at com.itextpdf.text.pdf.internal.PdfA1Checker.checkPdfObject 這已在 6.0.4 及更高版本中解決,請參閱Jasper 報告跟蹤器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM