updated filename, PDF/A schema extension for ZF2

This commit is contained in:
Jochen Stärk
2017-10-07 17:48:00 +02:00
parent 55206e7e89
commit a4e6f06e7d
8 changed files with 164 additions and 132 deletions

View File

@@ -4,7 +4,7 @@
<groupId>org.mustangproject.ZUGFeRD</groupId>
<artifactId>mustang</artifactId>
<name>Mustang</name>
<version>1.4.1-SNAPSHOT</version>
<version>1.4.2-SNAPSHOT</version>
<description>The Mustang project is a java library to read and write ZUGFeRD meta data inside your invoice PDFs</description>
<url>http://www.mustangproject.org/</url>
<mailingLists>

View File

@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.mustangproject.ZUGFeRD</groupId>
<artifactId>mustang</artifactId>
<version>1.4.1-SNAPSHOT</version>
<version>1.4.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Mustang</name>
<description>The Mustang project is a java library to read and write ZUGFeRD meta data inside your invoice PDFs</description>

View File

@@ -39,6 +39,15 @@ public class XMPSchemaPDFAExtensions extends PDFAExtensionSchema {
public final String prefix_pdfaSchema = "pdfaSchema";
public final String xmlns_pdfaProperty = "http://www.aiim.org/pdfa/ns/property#";
public final String prefix_pdfaProperty = "pdfaProperty";
public String namespace = null;
public String prefix = null;
public void setZUGFeRDVersion(int ver) {
namespace=ZUGFeRDExporter.getNamespaceForVersion(ver);
prefix=ZUGFeRDExporter.getPrefixForVersion(ver);
}
private DefinedStructuredType addProperty(ArrayProperty parent, String name, String type, String category,
String description) {
@@ -68,17 +77,15 @@ public class XMPSchemaPDFAExtensions extends PDFAExtensionSchema {
}
public XMPSchemaPDFAExtensions(XMPMetadata metadata) {
super(metadata);
setZUGFeRDVersion(2);
attachExtensions(metadata, true);
}
public XMPSchemaPDFAExtensions(XMPMetadata metadata, boolean withZF) {
super(metadata);
setZUGFeRDVersion(2);
attachExtensions(metadata, withZF);
}
public void attachExtensions(XMPMetadata metadata, boolean withZF) {
@@ -98,10 +105,10 @@ public class XMPSchemaPDFAExtensions extends PDFAExtensionSchema {
li.addProperty(pdfa1);
pdfa1 = new TextType(metadata, xmlns_pdfaSchema, prefix_pdfaSchema, PDFASchemaType.NAMESPACE_URI,
"urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#");
namespace);
li.addProperty(pdfa1);
pdfa1 = new TextType(metadata, xmlns_pdfaSchema, prefix_pdfaSchema, PDFASchemaType.PREFIX, "zf");
pdfa1 = new TextType(metadata, xmlns_pdfaSchema, prefix_pdfaSchema, PDFASchemaType.PREFIX, prefix);
li.addProperty(pdfa1);
ArrayProperty newSeq = new ArrayProperty(metadata, xmlns_pdfaSchema, prefix_pdfaSchema,
@@ -112,7 +119,7 @@ public class XMPSchemaPDFAExtensions extends PDFAExtensionSchema {
addProperty(newSeq, "DocumentType", "Text", "external", "INVOICE");
addProperty(newSeq, "Version", "Text", "external", "The actual version of the ZUGFeRD XML schema");
addProperty(newSeq, "ConformanceLevel", "Text", "external",
"The conformance level of the embedded ZUGFeRD data");
"The selected ZUGFeRD profile completeness");
}
}
}

View File

@@ -15,14 +15,14 @@ public class XMPSchemaZugferd extends XMPSchema {
/**
* This is what needs to be added to the RDF metadata - basically the name of the embedded Zugferd file
*/
public XMPSchemaZugferd(XMPMetadata metadata, ZUGFeRDConformanceLevel conformanceLevel) {
super(metadata, "urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#", "zf", "ZUGFeRD Schema");
public XMPSchemaZugferd(XMPMetadata metadata, ZUGFeRDConformanceLevel conformanceLevel, String URN, String prefix, String filename) {
super(metadata, URN, prefix, "ZUGFeRD Schema");
setAboutAsSimple("");
setTextPropertyValue("ConformanceLevel", conformanceLevel.name());
setTextPropertyValue("DocumentType", "INVOICE");
setTextPropertyValue("DocumentFileName", "ZUGFeRD-invoice.xml");
setTextPropertyValue("DocumentFileName", filename);
setTextPropertyValue("Version", "1.0");
}
}

View File

@@ -35,11 +35,10 @@ import java.util.Map;
public class ZUGFeRDExporter implements Closeable {
/**
* * You will need Apache PDFBox. To use the ZUGFeRD exporter, implement
* IZUGFeRDExportableTransaction in yourTransaction (which will require you
* to implement Product, Item and Contact) then call doc =
* PDDocument.load(PDFfilename); // automatically add Zugferd to all
* outgoing invoices ZUGFeRDExporter ze = new ZUGFeRDExporter();
* To use the ZUGFeRD exporter, implement IZUGFeRDExportableTransaction in
* yourTransaction (which will require you to implement Product, Item and
* Contact) then call doc = PDDocument.load(PDFfilename); // automatically add
* Zugferd to all outgoing invoices ZUGFeRDExporter ze = new ZUGFeRDExporter();
* ze.PDFmakeA3compliant(doc, "Your application name",
* System.getProperty("user.name"), true); ze.PDFattachZugferdFile(doc,
* yourTransaction);
@@ -47,67 +46,102 @@ public class ZUGFeRDExporter implements Closeable {
* doc.save(PDFfilename);
*
* @author jstaerk
* @deprecated Use the factory methods {@link #createFromPDFA3(String)}, {@link #createFromPDFA3(InputStream)} or
* the {@link ZUGFeRDExporterFromA1Factory} instead
* @deprecated Use the factory methods {@link #createFromPDFA3(String)},
* {@link #createFromPDFA3(InputStream)} or the
* {@link ZUGFeRDExporterFromA1Factory} instead
*
*/
// // MAIN CLASS
@Deprecated
private PDFAConformanceLevel conformanceLevel = PDFAConformanceLevel.UNICODE;
// BASIC, COMFORT etc - may be set from outside.
@Deprecated
private ZUGFeRDConformanceLevel zUGFeRDConformanceLevel = ZUGFeRDConformanceLevel.EXTENDED;
/**
* Data (XML invoice) to be added to the ZUGFeRD PDF. It may be externally set,
* in which case passing a IZUGFeRDExportableTransaction is not necessary. By
* default it is null meaning the caller needs to pass a
* IZUGFeRDExportableTransaction for the XML to be populated.
*/
IXMLProvider xmlProvider;
@Deprecated
private boolean ignoreA1Errors;
private PDDocument doc;
int ZFVersion;
public ZUGFeRDExporter() {
init();
}
public ZUGFeRDExporter(PDDocument doc2) {
doc=doc2;
doc = doc2;
init();
}
public void setVersion(int ver) {
if (ver==1) {
ZUGFeRD1PullProvider z1p =new ZUGFeRD1PullProvider();
this.xmlProvider=z1p;
public static String getNamespaceForVersion(int ver) {
if (ver == 1) {
return "urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#";
} else if (ver == 2) {
return "urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0#";
} else {
ZUGFeRD2PullProvider z2p =new ZUGFeRD2PullProvider();
this.xmlProvider=z2p;
throw new IllegalArgumentException("Version not supported");
}
}
private void init() {
setVersion(2);
public static String getPrefixForVersion(int ver) {
if (ver == 1) {
return "zf";
} else if (ver == 2) {
return "fx";
} else {
throw new IllegalArgumentException("Version not supported");
}
}
// // MAIN CLASS
@Deprecated
private PDFAConformanceLevel conformanceLevel = PDFAConformanceLevel.UNICODE;
// BASIC, COMFORT etc - may be set from outside.
@Deprecated
private ZUGFeRDConformanceLevel zUGFeRDConformanceLevel = ZUGFeRDConformanceLevel.EXTENDED;
public static String getFilenameForVersion(int ver) {
if (ver == 1) {
return "ZUGFeRD-invoice.xml";
} else if (ver == 2) {
return "factur-x.xml";
} else {
throw new IllegalArgumentException("Version not supported");
}
}
/**
* Data (XML invoice) to be added to the ZUGFeRD PDF. It may be externally
* set, in which case passing a IZUGFeRDExportableTransaction is not
* necessary. By default it is null meaning the caller needs to pass a
* IZUGFeRDExportableTransaction for the XML to be populated.
*/
IXMLProvider xmlProvider;
@Deprecated
private boolean ignoreA1Errors;
private PDDocument doc;
public void setZUGFeRDVersion(int ver) {
if (ver == 1) {
ZUGFeRD1PullProvider z1p = new ZUGFeRD1PullProvider();
this.xmlProvider = z1p;
} else if (ver == 2) {
ZUGFeRD2PullProvider z2p = new ZUGFeRD2PullProvider();
this.xmlProvider = z2p;
} else {
throw new IllegalArgumentException("Version not supported");
}
ZFVersion = ver;
}
private void init() {
setZUGFeRDVersion(2);
}
/**
* All files are PDF/A-3, setConformance refers to the level conformance.
*
* PDF/A-3 has three conformance levels, called "A", "U" and "B".
*
* PDF/A-3-B where B means only visually preservable, U -standard for
* Mustang- means visually and unicode preservable and A means full
* compliance, i.e. visually, unicode and structurally preservable and
* tagged PDF, i.e. useful metainformation for blind people.
* PDF/A-3-B where B means only visually preservable, U -standard for Mustang-
* means visually and unicode preservable and A means full compliance, i.e.
* visually, unicode and structurally preservable and tagged PDF, i.e. useful
* metainformation for blind people.
*
* Feel free to pass "A" as new level if you know what you are doing :-)
*
* @deprecated Use {@link ZUGFeRDExporterFromA1Factory} instead
*/
@Deprecated
@Deprecated
public void setConformanceLevel(PDFAConformanceLevel newLevel) {
if (newLevel == null) {
throw new NullPointerException("pdf conformance level");
@@ -120,14 +154,16 @@ public class ZUGFeRDExporter implements Closeable {
*
* PDF/A-3 has three conformance levels, called "A", "U" and "B".
*
* PDF/A-3-B where B means only visually preservable, U -standard for
* Mustang- means visually and unicode preservable and A means full
* compliance, i.e. visually, unicode and structurally preservable and
* tagged PDF, i.e. useful metainformation for blind people.
* PDF/A-3-B where B means only visually preservable, U -standard for Mustang-
* means visually and unicode preservable and A means full compliance, i.e.
* visually, unicode and structurally preservable and tagged PDF, i.e. useful
* metainformation for blind people.
*
* Feel free to pass "A" as new level if you know what you are doing :-)
*
* @deprecated Use {@link #setConformanceLevel(org.mustangproject.ZUGFeRD.PDFAConformanceLevel)} instead
* @deprecated Use
* {@link #setConformanceLevel(org.mustangproject.ZUGFeRD.PDFAConformanceLevel)}
* instead
*/
@Deprecated
public void setConformanceLevel(String newLevel) {
@@ -142,29 +178,30 @@ public class ZUGFeRDExporter implements Closeable {
xmlProvider.setTest();
}
/**
* @deprecated Use {@link ZUGFeRDExporterFromA1Factory} instead
*/
/**
* @deprecated Use {@link ZUGFeRDExporterFromA1Factory} instead
*/
@Deprecated
public void ignoreA1Errors() {
ignoreA1Errors = true;
}
/**
* @deprecated Use the factory method {@link #createFromPDFA3(String)} instead
*/
/**
* @deprecated Use the factory method {@link #createFromPDFA3(String)} instead
*/
@Deprecated
public void loadPDFA3(String filename) throws IOException {
doc = PDDocument.load(new File(filename));
}
public void loadPDFA3(String filename) throws IOException {
doc = PDDocument.load(new File(filename));
}
public static ZUGFeRDExporter createFromPDFA3(String filename) throws IOException {
return new ZUGFeRDExporter(PDDocument.load(new File(filename)));
}
/**
* @deprecated Use the factory method {@link #createFromPDFA3(InputStream)} instead
*/
/**
* @deprecated Use the factory method {@link #createFromPDFA3(InputStream)}
* instead
*/
@Deprecated
public void loadPDFA3(InputStream file) throws IOException {
doc = PDDocument.load(file);
@@ -175,40 +212,31 @@ public class ZUGFeRDExporter implements Closeable {
}
/**
* Makes A PDF/A3a-compliant document from a PDF-A1 compliant document (on
* the metadata level, this will not e.g. convert graphics to JPG-2000)
*
* @deprecated use the {@link ZUGFeRDExporterFromA1Factory} instead
* Makes A PDF/A3a-compliant document from a PDF-A1 compliant document (on the
* metadata level, this will not e.g. convert graphics to JPG-2000)
*
* @deprecated use the {@link ZUGFeRDExporterFromA1Factory} instead
*
*/
@Deprecated
public PDDocumentCatalog PDFmakeA3compliant(String filename,
String producer, String creator, boolean attachZugferdHeaders)
throws IOException, TransformerException {
public PDDocumentCatalog PDFmakeA3compliant(String filename, String producer, String creator,
boolean attachZugferdHeaders) throws IOException, TransformerException {
doc = createPDFA1Factory()
.setProducer(producer)
.setCreator(creator)
.setAttachZUGFeRDHeaders(attachZugferdHeaders)
.load(filename)
.doc;
doc = createPDFA1Factory().setProducer(producer).setCreator(creator)
.setAttachZUGFeRDHeaders(attachZugferdHeaders).load(filename).doc;
return doc.getDocumentCatalog();
}
/**
* @deprecated use the {@link ZUGFeRDExporterFromA1Factory} instead
*/
* @deprecated use the {@link ZUGFeRDExporterFromA1Factory} instead
*/
@Deprecated
public PDDocumentCatalog PDFmakeA3compliant(InputStream file,
String producer, String creator, boolean attachZugferdHeaders) throws IOException, TransformerException {
public PDDocumentCatalog PDFmakeA3compliant(InputStream file, String producer, String creator,
boolean attachZugferdHeaders) throws IOException, TransformerException {
doc = createPDFA1Factory()
.setProducer(producer)
.setCreator(creator)
.setAttachZUGFeRDHeaders(attachZugferdHeaders)
.load(file)
.doc;
doc = createPDFA1Factory().setProducer(producer).setCreator(creator)
.setAttachZUGFeRDHeaders(attachZugferdHeaders).load(file).doc;
return doc.getDocumentCatalog();
}
@@ -218,9 +246,7 @@ public class ZUGFeRDExporter implements Closeable {
if (ignoreA1Errors) {
factory.ignorePDFAErrors();
}
return factory
.setZUGFeRDConformanceLevel(zUGFeRDConformanceLevel)
.setConformanceLevel(conformanceLevel);
return factory.setZUGFeRDConformanceLevel(zUGFeRDConformanceLevel).setConformanceLevel(conformanceLevel);
}
@Override
@@ -230,27 +256,22 @@ public class ZUGFeRDExporter implements Closeable {
}
}
/**
* Embeds the Zugferd XML structure in a file named ZUGFeRD-invoice.xml.
*
* @param trans
* a IZUGFeRDExportableTransaction that provides the data-model
* to populate the XML. This parameter may be null, if so the XML
* data should hav ebeen set via
* a IZUGFeRDExportableTransaction that provides the data-model to
* populate the XML. This parameter may be null, if so the XML data
* should hav ebeen set via
* <code>setZUGFeRDXMLData(byte[] zugferdData)</code>
*/
public void PDFattachZugferdFile(IZUGFeRDExportableTransaction trans)
throws IOException {
public void PDFattachZugferdFile(IZUGFeRDExportableTransaction trans) throws IOException {
xmlProvider.generateXML(trans);
PDFAttachGenericFile(
doc,
"ZUGFeRD-invoice.xml",
"Alternative",
"Invoice metadata conforming to ZUGFeRD standard (http://www.ferd-net.de/front_content.php?idcat=231&lang=4)",
"text/xml", xmlProvider.getXML());
xmlProvider.generateXML(trans);
String filename = getFilenameForVersion(ZFVersion);
PDFAttachGenericFile(doc, filename, "Alternative",
"Invoice metadata conforming to ZUGFeRD standard (http://www.ferd-net.de/front_content.php?idcat=231&lang=4)",
"text/xml", xmlProvider.getXML());
}
public void export(String ZUGFeRDfilename) throws IOException {
@@ -278,13 +299,11 @@ public class ZUGFeRDExporter implements Closeable {
* the binary data of the file/attachment
* @throws java.io.IOException
*/
public void PDFAttachGenericFile(PDDocument doc, String filename,
String relationship, String description, String subType, byte[] data)
throws IOException {
public void PDFAttachGenericFile(PDDocument doc, String filename, String relationship, String description,
String subType, byte[] data) throws IOException {
PDComplexFileSpecification fs = new PDComplexFileSpecification();
fs.setFile(filename);
COSDictionary dict = fs.getCOSObject();
dict.setName("AFRelationship", relationship);
dict.setString("UF", filename);
@@ -302,14 +321,12 @@ public class ZUGFeRDExporter implements Closeable {
// In addition make sure the embedded file is set under /UF
dict = fs.getCOSObject();
COSDictionary efDict = (COSDictionary) dict
.getDictionaryObject(COSName.EF);
COSDictionary efDict = (COSDictionary) dict.getDictionaryObject(COSName.EF);
COSBase lowerLevelFile = efDict.getItem(COSName.F);
efDict.setItem(COSName.UF, lowerLevelFile);
// now add the entry to the embedded file tree and set in the document.
PDDocumentNameDictionary names = new PDDocumentNameDictionary(
doc.getDocumentCatalog());
PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
PDEmbeddedFilesNameTreeNode efTree = names.getEmbeddedFiles();
if (efTree == null) {
efTree = new PDEmbeddedFilesNameTreeNode();
@@ -330,8 +347,7 @@ public class ZUGFeRDExporter implements Closeable {
doc.getDocumentCatalog().setNames(names);
// AF entry (Array) in catalog with the FileSpec
COSArray cosArray = (COSArray) doc.getDocumentCatalog()
.getCOSObject().getItem("AF");
COSArray cosArray = (COSArray) doc.getDocumentCatalog().getCOSObject().getItem("AF");
if (cosArray == null) {
cosArray = new COSArray();
}
@@ -339,22 +355,22 @@ public class ZUGFeRDExporter implements Closeable {
COSDictionary dict2 = doc.getDocumentCatalog().getCOSObject();
COSArray array = new COSArray();
array.add(fs.getCOSObject()); // see below
dict2.setItem("AF",array);
dict2.setItem("AF", array);
doc.getDocumentCatalog().getCOSObject().setItem("AF", cosArray);
}
/**
* Sets the ZUGFeRD XML data to be attached as a single byte array. This is
* useful for use-cases where the XML has already been produced by some
* external API or component.
* useful for use-cases where the XML has already been produced by some external
* API or component.
*
* @param zugferdData
* XML data to be set as a byte array (XML file in raw form).
*/
public void setZUGFeRDXMLData(byte[] zugferdData) throws IOException {
CustomXMLProvider cus =new CustomXMLProvider();
CustomXMLProvider cus = new CustomXMLProvider();
cus.setXML(zugferdData);
this.xmlProvider=cus;
this.xmlProvider = cus;
PDFattachZugferdFile(null);
}
@@ -363,8 +379,8 @@ public class ZUGFeRDExporter implements Closeable {
*
* @param zUGFeRDConformanceLevel
* the new conformance level
*
* @deprecated Use {@link ZUGFeRDExporterFromA1Factory} instead
*
* @deprecated Use {@link ZUGFeRDExporterFromA1Factory} instead
*/
@Deprecated
public void setZUGFeRDConformanceLevel(ZUGFeRDConformanceLevel zUGFeRDConformanceLevel) {

View File

@@ -9,13 +9,11 @@ import org.apache.pdfbox.preflight.PreflightDocument;
import org.apache.pdfbox.preflight.exception.ValidationException;
import org.apache.pdfbox.preflight.parser.PreflightParser;
import org.apache.pdfbox.preflight.utils.ByteArrayDataSource;
import org.apache.pdfbox.util.Version;
import org.apache.xmpbox.XMPMetadata;
import org.apache.xmpbox.schema.AdobePDFSchema;
import org.apache.xmpbox.schema.DublinCoreSchema;
import org.apache.xmpbox.schema.PDFAIdentificationSchema;
import org.apache.xmpbox.schema.XMPBasicSchema;
import org.apache.xmpbox.type.BadFieldValueException;
import org.apache.xmpbox.xml.XmpSerializer;
import javax.activation.DataSource;
@@ -28,6 +26,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.GregorianCalendar;
import java.util.zip.ZipFile;
public class ZUGFeRDExporterFromA3Factory implements IExporterFactory {
protected boolean ignorePDFAErrors = false;
@@ -41,6 +40,7 @@ public class ZUGFeRDExporterFromA3Factory implements IExporterFactory {
protected PDMetadata metadata = null;
protected PDFAIdentificationSchema pdfaid = null;
protected XMPMetadata xmp = null;
protected int ZFVersion=2;
/**
* Makes A PDF/A3a-compliant document from a PDF-A1 compliant document (on the
* metadata level, this will not e.g. convert graphics to JPG-2000)
@@ -56,6 +56,10 @@ public class ZUGFeRDExporterFromA3Factory implements IExporterFactory {
}
public void setZUGFeRDversion(int version) {
ZFVersion=version;
}
public void prepareDocument(PDDocument doc) throws IOException {
String fullProducer = producer + " (via mustangproject.org " + org.mustangproject.ZUGFeRD.Version.VERSION + ")";
@@ -102,7 +106,7 @@ public class ZUGFeRDExporterFromA3Factory implements IExporterFactory {
PDDocument doc = PDDocument.load(pdfBinary);
prepareDocument(doc);
zugFeRDExporter = new ZUGFeRDExporter(doc);
zugFeRDExporter.setZUGFeRDVersion(ZFVersion);
return zugFeRDExporter;
}
@@ -164,7 +168,7 @@ public class ZUGFeRDExporterFromA3Factory implements IExporterFactory {
public void addXMP(XMPMetadata metadata) {
if (attachZugferdHeaders) {
XMPSchemaZugferd zf = new XMPSchemaZugferd(metadata, zugferdConformanceLevel);
XMPSchemaZugferd zf = new XMPSchemaZugferd(metadata, zugferdConformanceLevel, ZUGFeRDExporter.getNamespaceForVersion(ZFVersion), ZUGFeRDExporter.getPrefixForVersion(ZFVersion), ZUGFeRDExporter.getFilenameForVersion(ZFVersion) );
metadata.addSchema(zf);
}

View File

@@ -89,7 +89,7 @@ public class ZUGFeRDImporter {
* currently (in the release candidate of version 1) only one
* attached file with the name ZUGFeRD-invoice.xml is allowed
* */
if (filename.equals("ZUGFeRD-invoice.xml")) { //$NON-NLS-1$
if ((filename.equals("ZUGFeRD-invoice.xml")||filename.equals("factur-x.xml"))) { //$NON-NLS-1$
containsMeta = true;
PDComplexFileSpecification fileSpec = efMap.get(filename);
@@ -284,6 +284,11 @@ public class ZUGFeRDImporter {
}
ndList = document.getElementsByTagNameNS("*","SpecifiedTradePaymentTerms"); //$NON-NLS-1$
if (ndList.getLength()==0) {
// try ZF1, it can not harm
ndList = document.getElementsByTagNameNS("*","SpecifiedCITradePaymentTerms"); //$NON-NLS-1$
}
for (int bookingIndex = 0; bookingIndex < ndList
.getLength(); bookingIndex++) {

View File

@@ -410,7 +410,7 @@ public class MustangReaderWriterEdgeTest extends TestCase implements IZUGFeRDExp
.setCreator(System.getProperty("user.name"))
.ignorePDFAErrors()
.load(SOURCE_PDF)) {
ze.setVersion(1);
ze.setZUGFeRDVersion(1);
ze.PDFattachZugferdFile(this);
ze.export(TARGET_PDF);
}