PDF/A PdfXConformanceException All the fonts must be embedded. This one isn't: Helvetica

0

I'm using JasperReports Studio 6.3.1 and for coding jasperreports-6.3.1.jar, jasperreports-font.6.3.1.jar, itext-2.1.7.js5.jar

I tried to export a very simple report to PDF/A, witch is only using DejaVu Sans Font from the library.

The Preview looks like this:

I get this Exeption:

com.lowagie.text.pdf.PdfXConformanceException: All the fonts must be embedded. This one isn't: Helvetica
    at com.lowagie.text.pdf.internal.PdfXConformanceImp.checkPDFXConformance(Unknown Source)
    at com.lowagie.text.pdf.PdfWriter.addSimple(Unknown Source)
    at com.lowagie.text.pdf.PdfContentByte.setFontAndSize(Unknown Source)
    at com.lowagie.text.pdf.PdfDocument.writeLineToContent(Unknown Source)
    at com.lowagie.text.pdf.ColumnText.go(Unknown Source)
    at com.lowagie.text.pdf.ColumnText.go(Unknown Source)
    at net.sf.jasperreports.engine.export.JRPdfExporter.writePageAnchor(JRPdfExporter.java:1000)
    at net.sf.jasperreports.engine.export.JRPdfExporter.exportReportToStream(JRPdfExporter.java:912)
    at net.sf.jasperreports.engine.export.JRPdfExporter.exportReport(JRPdfExporter.java:537)
    at openkv.jasperreports.demo.FontExtensionDemo.exportToPdfA(FontExtensionDemo.java:48)
    at openkv.jasperreports.demo.FontExtensionDemo.runDemo(FontExtensionDemo.java:40)
    at openkv.jasperreports.demo.FontExtensionDemo.main(FontExtensionDemo.java:32)
</pre>

The Java Source:

package openkv.jasperreports.demo;
 
import java.io.File;
import java.net.URL;
 
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
 
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimplePdfExporterConfiguration;
import net.sf.jasperreports.export.type.PdfaConformanceEnum;
 
public class FontExtensionDemo {
 
    private final static String ICC_FILE = "/openkv/framework/jasperreports/engine/export/sRGB_v4_ICC_preference.icc";
    private final static String reportName = "SimpleFontExtension";
 
    public static void main(String[] args) {
        BasicConfigurator.configure();
        Logger.getRootLogger().setLevel(Level.DEBUG);
 
        FontExtensionDemo demo = new FontExtensionDemo();
        try {
            demo.runDemo();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
 
    public void runDemo() throws Throwable {
        JasperPrint jasperPrint = fillReport();
        exportToPdfA(jasperPrint);
    }
 
    private void exportToPdfA(JasperPrint jasperPrint) throws JRException {
        JRPdfExporter exporter = new JRPdfExporter();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("H:\\" + reportName + ".pdf"));
        exporter.setConfiguration(this.getPdfAExporterConfiguration());
        exporter.exportReport();
    }
 
    private SimplePdfExporterConfiguration getPdfAExporterConfiguration() {
        SimplePdfExporterConfiguration exporterConfiguration = new SimplePdfExporterConfiguration();
        exporterConfiguration.isTagged();
        exporterConfiguration.setPdfaConformance(PdfaConformanceEnum.PDFA_1A);
        exporterConfiguration.setIccProfilePath(getClass().getResource(ICC_FILE).getPath());
        return exporterConfiguration;
    }
 
    private JasperPrint fillReport() throws JRException {
        JRDataSource dataSource = new JREmptyDataSource(1);
        return JasperFillManager.fillReport(getSourceFileName(), null, dataSource);
    }
 
    private String getSourceFileName() {
        String reportBasePath = "/openkv/jasperreports/demo/report";
        URL reportResource = FontExtensionDemo.class.getResource(reportBasePath + "/FontExtension/" + reportName + ".jasper");
        return new File(reportResource.getPath()).getAbsolutePath();
    }
}

The Report Source:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-06-16T11:09:44 -->
<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="SimpleFontExtension" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="33b3b27b-bc99-4208-a8fc-1f05d77efd89">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="103" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="550" height="41" uuid="57a35ef2-2c45-4e6a-a302-9c3b083e2bcb"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="DejaVu Sans" size="34" isBold="true" isItalic="false" isUnderline="true"/>
                </textElement>
                <text><![CDATA[DejaVu Sans Font Demo]]></text>
            </staticText>
            <staticText>
                <reportElement key="" x="68" y="46" width="52" height="19" uuid="cd391c43-485c-4415-a71d-81930765738d"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="DejaVu Sans" size="12" isBold="true" isUnderline="false"/>
                </textElement>
                <text><![CDATA[Bold]]></text>
            </staticText>
            <staticText>
                <reportElement key="" x="68" y="65" width="52" height="19" uuid="18fa45fa-2346-4d7d-8447-f76261754fef"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="DejaVu Sans" size="12" isBold="false" isItalic="true" isUnderline="false"/>
                </textElement>
                <text><![CDATA[Italic]]></text>
            </staticText>
            <staticText>
                <reportElement key="" x="68" y="84" width="52" height="19" uuid="76f3ac47-7018-4227-8de7-34a9a9f3a603"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="DejaVu Sans" size="12" isBold="false" isItalic="false" isUnderline="false"/>
                </textElement>
                <text><![CDATA[Normal]]></text>
            </staticText>
            <staticText>
                <reportElement key="" x="130" y="46" width="110" height="19" uuid="65aae4f2-58c1-446a-941f-657fd7887916"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="DejaVu Sans" size="12" isBold="true" isUnderline="true"/>
                </textElement>
                <text><![CDATA[Bold Underline]]></text>
            </staticText>
            <staticText>
                <reportElement key="" x="130" y="65" width="110" height="19" uuid="d2c5cbbd-d088-40b8-a1bc-fa68156dbf07"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="DejaVu Sans" size="12" isBold="false" isItalic="true" isUnderline="true"/>
                </textElement>
                <text><![CDATA[Italic Underline]]></text>
            </staticText>
            <staticText>
                <reportElement key="" x="130" y="84" width="110" height="19" uuid="0859e64f-c9fd-4286-9303-cf76182b4734"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="DejaVu Sans" size="12" isBold="false" isItalic="false" isUnderline="true"/>
                </textElement>
                <text><![CDATA[Normal Underline]]></text>
            </staticText>
            <staticText>
                <reportElement key="" x="250" y="46" width="80" height="19" uuid="1af0605b-9669-42f9-af2c-41990708c7f8"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="DejaVu Sans" size="12" isBold="true" isItalic="true" isUnderline="false"/>
                </textElement>
                <text><![CDATA[Bold Italic]]></text>
            </staticText>
            <staticText>
                <reportElement key="" x="340" y="46" width="150" height="19" uuid="fddc7585-5d20-4599-b64c-fd7859018020"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="DejaVu Sans" size="12" isBold="true" isItalic="true" isUnderline="true"/>
                </textElement>
                <text><![CDATA[Bold Italic Underline]]></text>
            </staticText>
        </band>
    </detail>
</jasperReport>

If I export the Report without this line:

exporterConfiguration.setPdfaConformance(PdfaConformanceEnum.PDFA_1A);

I can see the unembedded Helvetica Font:

I don't need or use this font in my report. How can I turn off this.

Otto Hahn's picture
Joined: Jan 12 2016 - 12:36am
Last seen: 1 month 4 weeks ago

1 Answer:

0

I am amazed that no one is answering this specific question.
So I try it myself:

create a jasperreports.properties file in the root path of the project:

# loads/override properties from default.jasperreports.properties
net.sf.jasperreports.default.pdf.font.name=net/sf/jasperreports/fonts/dejavu/DejaVuSans.ttf
net.sf.jasperreports.default.pdf.embedded=true

or change the default font by adding this Method and call it before export to PDF/A:

public void setDefaultPdfFontEmbedded() {
    JasperReportsContext jasperReportsContext = DefaultJasperReportsContext.getInstance();
    jasperReportsContext.setProperty("net.sf.jasperreports.default.pdf.font.name", "net/sf/jasperreports/fonts/dejavu/DejaVuSans.ttf");
    jasperReportsContext.setProperty("net.sf.jasperreports.default.pdf.embedded", "true");
}

Please correct me if this is not true!

Otto Hahn's picture
Joined: Jan 12 2016 - 12:36am
Last seen: 1 month 4 weeks ago
Feedback