be able to start with pdf a/3 files as well
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package org.mustangproject.ZUGFeRD;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.xmpbox.XMPMetadata;
|
||||
|
||||
public interface IExporterFactory {
|
||||
public ZUGFeRDExporter load(String pdfFilename) throws IOException;
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
* @param pdfBinary
|
||||
* binary of a PDF/A1 compliant document
|
||||
*/
|
||||
public ZUGFeRDExporter load(byte[] pdfBinary) throws IOException;
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
* @param pdfSource
|
||||
* source to read a PDF/A1 compliant document from
|
||||
* @throws IOException
|
||||
*/
|
||||
public ZUGFeRDExporter load(InputStream pdfSource) throws IOException;
|
||||
public void prepareDocument(PDDocument doc) throws IOException;
|
||||
public void addXMP(XMPMetadata metadata);
|
||||
|
||||
public IExporterFactory setCreator(String creator);
|
||||
public IExporterFactory setConformanceLevel(PDFAConformanceLevel newLevel);
|
||||
public IExporterFactory setProducer(String producer) ;
|
||||
public IExporterFactory ignorePDFAErrors();
|
||||
public IExporterFactory setZUGFeRDConformanceLevel(ZUGFeRDConformanceLevel zugferdConformanceLevel);
|
||||
public IExporterFactory setAttachZUGFeRDHeaders(final boolean attachZugferdHeaders);
|
||||
|
||||
}
|
||||
@@ -194,8 +194,8 @@ public class ZUGFeRDExporter implements Closeable {
|
||||
doc = createPDFA1Factory()
|
||||
.setProducer(producer)
|
||||
.setCreator(creator)
|
||||
.setAttachZugferdHeaders(attachZugferdHeaders)
|
||||
.loadFromPDFA1(filename)
|
||||
.setAttachZUGFeRDHeaders(attachZugferdHeaders)
|
||||
.load(filename)
|
||||
.doc;
|
||||
|
||||
return doc.getDocumentCatalog();
|
||||
@@ -211,20 +211,20 @@ public class ZUGFeRDExporter implements Closeable {
|
||||
doc = createPDFA1Factory()
|
||||
.setProducer(producer)
|
||||
.setCreator(creator)
|
||||
.setAttachZugferdHeaders(attachZugferdHeaders)
|
||||
.loadFromPDFA1(file)
|
||||
.setAttachZUGFeRDHeaders(attachZugferdHeaders)
|
||||
.load(file)
|
||||
.doc;
|
||||
|
||||
return doc.getDocumentCatalog();
|
||||
}
|
||||
|
||||
private ZUGFeRDExporterFromA1Factory createPDFA1Factory() {
|
||||
private IExporterFactory createPDFA1Factory() {
|
||||
ZUGFeRDExporterFromA1Factory factory = new ZUGFeRDExporterFromA1Factory();
|
||||
if (ignoreA1Errors) {
|
||||
factory.ignoreA1Errors();
|
||||
factory.ignorePDFAErrors();
|
||||
}
|
||||
return factory
|
||||
.setZugferdConformanceLevel(zUGFeRDConformanceLevel)
|
||||
.setZUGFeRDConformanceLevel(zUGFeRDConformanceLevel)
|
||||
.setConformanceLevel(conformanceLevel);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,126 +1,19 @@
|
||||
package org.mustangproject.ZUGFeRD;
|
||||
|
||||
import org.apache.pdfbox.io.IOUtils;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
|
||||
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
|
||||
import org.apache.pdfbox.pdmodel.common.PDMetadata;
|
||||
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;
|
||||
import javax.activation.FileDataSource;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
public class ZUGFeRDExporterFromA1Factory {
|
||||
private boolean ignoreA1Errors = false;
|
||||
private ZUGFeRDConformanceLevel zugferdConformanceLevel = ZUGFeRDConformanceLevel.EXTENDED;
|
||||
private PDFAConformanceLevel conformanceLevel = PDFAConformanceLevel.UNICODE;
|
||||
private String producer = "mustangproject";
|
||||
private String creator = "mustangproject";
|
||||
private boolean attachZugferdHeaders = true;
|
||||
private static boolean includeXMPinOptionalXPacket=true;
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
* @param pdfFilename
|
||||
* filename of an PDF/A1 compliant document
|
||||
*/
|
||||
public ZUGFeRDExporter loadFromPDFA1(String pdfFilename) throws IOException {
|
||||
ensurePDFIsValidA1(new FileDataSource(pdfFilename));
|
||||
PDDocument doc = PDDocument.load(new File(pdfFilename));
|
||||
makePDFA3compliant(doc);
|
||||
return new ZUGFeRDExporter(doc);
|
||||
}
|
||||
import javax.xml.transform.TransformerException;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.xmpbox.type.BadFieldValueException;
|
||||
|
||||
public class ZUGFeRDExporterFromA1Factory extends ZUGFeRDExporterFromA3Factory implements IExporterFactory {
|
||||
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
* @param pdfBinary
|
||||
* binary of a PDF/A1 compliant document
|
||||
* Make a PDF A/3 from the PDF A/1
|
||||
*/
|
||||
public ZUGFeRDExporter loadFromPDFA1(byte[] pdfBinary) throws IOException {
|
||||
ensurePDFIsValidA1(new ByteArrayDataSource(new ByteArrayInputStream(pdfBinary)));
|
||||
ZUGFeRDExporter zugFeRDExporter;
|
||||
PDDocument doc = PDDocument.load(pdfBinary);
|
||||
makePDFA3compliant(doc);
|
||||
zugFeRDExporter = new ZUGFeRDExporter(doc);
|
||||
|
||||
return zugFeRDExporter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
* @param pdfSource
|
||||
* source to read a PDF/A1 compliant document from
|
||||
*/
|
||||
public ZUGFeRDExporter loadFromPDFA1(InputStream pdfSource) throws IOException {
|
||||
return loadFromPDFA1(readAllBytes(pdfSource));
|
||||
}
|
||||
|
||||
private void ensurePDFIsValidA1(final DataSource dataSource) throws IOException {
|
||||
if (!ignoreA1Errors && !isValidA1(dataSource)) {
|
||||
throw new IOException("File is not a valid PDF/A-1 input file");
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] readAllBytes(InputStream in) throws IOException {
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
IOUtils.copy(in, buffer);
|
||||
return buffer.toByteArray();
|
||||
}
|
||||
|
||||
private void makePDFA3compliant(PDDocument doc) throws IOException {
|
||||
String fullProducer = producer + " (via mustangproject.org " + org.mustangproject.ZUGFeRD.Version.VERSION + ")";
|
||||
|
||||
PDDocumentCatalog cat = doc.getDocumentCatalog();
|
||||
PDMetadata metadata = new PDMetadata(doc);
|
||||
cat.setMetadata(metadata);
|
||||
|
||||
XMPMetadata xmp = XMPMetadata.createXMPMetadata();
|
||||
|
||||
PDFAIdentificationSchema pdfaid = new PDFAIdentificationSchema(xmp);
|
||||
|
||||
xmp.addSchema(pdfaid);
|
||||
|
||||
DublinCoreSchema dc = xmp.createAndAddDublinCoreSchema();
|
||||
|
||||
dc.addCreator(creator);
|
||||
|
||||
XMPBasicSchema xsb = xmp.createAndAddXMPBasicSchema();
|
||||
|
||||
xsb.setCreatorTool(creator);
|
||||
xsb.setCreateDate(GregorianCalendar.getInstance());
|
||||
// PDDocumentInformation pdi=doc.getDocumentInformation();
|
||||
PDDocumentInformation pdi = new PDDocumentInformation();
|
||||
pdi.setProducer(fullProducer);
|
||||
pdi.setAuthor(creator);
|
||||
doc.setDocumentInformation(pdi);
|
||||
|
||||
AdobePDFSchema pdf = xmp.createAndAddAdobePDFSchema();
|
||||
pdf.setProducer(fullProducer);
|
||||
|
||||
public void prepareDocument(PDDocument doc) throws IOException {
|
||||
super.prepareDocument(doc);
|
||||
/*
|
||||
* // Mandatory: PDF/A3-a is tagged PDF which has to be expressed using a //
|
||||
* MarkInfo dictionary (PDF A/3 Standard sec. 6.7.2.2) PDMarkInfo markinfo = new
|
||||
@@ -159,132 +52,4 @@ public class ZUGFeRDExporterFromA1Factory {
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] serializeXmpMetadata(XMPMetadata xmpMetadata) throws TransformerException {
|
||||
XmpSerializer serializer = new XmpSerializer();
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
|
||||
String prefix="";
|
||||
String suffix="";
|
||||
if (includeXMPinOptionalXPacket) {
|
||||
prefix="<?xpacket begin=\"\uFEFF\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>";
|
||||
suffix="<?xpacket end=\"w\"?>";
|
||||
|
||||
}
|
||||
try {
|
||||
buffer.write(prefix.getBytes("UTF-8"));
|
||||
serializer.serialize(xmpMetadata, buffer, false);
|
||||
buffer.write(suffix.getBytes("UTF-8"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return buffer.toByteArray();
|
||||
}
|
||||
|
||||
private static boolean isValidA1(DataSource dataSource) throws IOException {
|
||||
return getA1ParserValidationResult(new PreflightParser(dataSource));
|
||||
}
|
||||
|
||||
/**
|
||||
* This will add both the RDF-indication which embedded file is Zugferd and the
|
||||
* neccessary PDF/A schema extension description to be able to add this
|
||||
* information to RDF
|
||||
*
|
||||
* @param metadata
|
||||
*/
|
||||
private void addXMP(XMPMetadata metadata) {
|
||||
|
||||
if (attachZugferdHeaders) {
|
||||
XMPSchemaZugferd zf = new XMPSchemaZugferd(metadata, zugferdConformanceLevel);
|
||||
|
||||
metadata.addSchema(zf);
|
||||
}
|
||||
|
||||
XMPSchemaPDFAExtensions pdfaex = new XMPSchemaPDFAExtensions(metadata, attachZugferdHeaders);
|
||||
|
||||
metadata.addSchema(pdfaex);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the ZUGFeRD conformance level (override).
|
||||
*
|
||||
* @param zugferdConformanceLevel
|
||||
* the new conformance level
|
||||
*/
|
||||
public ZUGFeRDExporterFromA1Factory setZugferdConformanceLevel(ZUGFeRDConformanceLevel zugferdConformanceLevel) {
|
||||
this.zugferdConformanceLevel = zugferdConformanceLevel;
|
||||
return this;
|
||||
}
|
||||
|
||||
private static boolean getA1ParserValidationResult(PreflightParser parser) throws IOException {
|
||||
/*
|
||||
* Parse the PDF file with PreflightParser that inherits from the
|
||||
* NonSequentialParser. Some additional controls are present to check a set of
|
||||
* PDF/A requirements. (Stream length consistency, EOL after some Keyword...)
|
||||
*/
|
||||
parser.parse();
|
||||
|
||||
try (PreflightDocument document = parser.getPreflightDocument()) {
|
||||
/*
|
||||
* Once the syntax validation is done, the parser can provide a
|
||||
* PreflightDocument (that inherits from PDDocument) This document process the
|
||||
* end of PDF/A validation.
|
||||
*/
|
||||
|
||||
document.validate();
|
||||
|
||||
// Get validation result
|
||||
return document.getResult().isValid();
|
||||
} catch (ValidationException e) {
|
||||
/*
|
||||
* the parse method can throw a SyntaxValidationException if the PDF file can't
|
||||
* be parsed. In this case, the exception contains an instance of
|
||||
* ValidationResult
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* All files are PDF/A-3, setConformance refers to the level conformance.
|
||||
*
|
||||
* PDF/A-3 has three coformance 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.
|
||||
*
|
||||
* Feel free to pass "A" as new level if you know what you are doing :-)
|
||||
*
|
||||
*
|
||||
*/
|
||||
public ZUGFeRDExporterFromA1Factory setConformanceLevel(PDFAConformanceLevel newLevel) {
|
||||
conformanceLevel = newLevel;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ZUGFeRDExporterFromA1Factory ignoreA1Errors() {
|
||||
this.ignoreA1Errors = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ZUGFeRDExporterFromA1Factory setAttachZugferdHeaders(final boolean attachZugferdHeaders) {
|
||||
this.attachZugferdHeaders = attachZugferdHeaders;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ZUGFeRDExporterFromA1Factory setCreator(String creator) {
|
||||
this.creator = creator;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ZUGFeRDExporterFromA1Factory setProducer(String producer) {
|
||||
this.producer = producer;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,256 @@
|
||||
package org.mustangproject.ZUGFeRD;
|
||||
|
||||
import org.apache.pdfbox.io.IOUtils;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
|
||||
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
|
||||
import org.apache.pdfbox.pdmodel.common.PDMetadata;
|
||||
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;
|
||||
import javax.activation.FileDataSource;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
public class ZUGFeRDExporterFromA3Factory implements IExporterFactory {
|
||||
protected boolean ignorePDFAErrors = false;
|
||||
protected ZUGFeRDConformanceLevel zugferdConformanceLevel = ZUGFeRDConformanceLevel.EXTENDED;
|
||||
protected PDFAConformanceLevel conformanceLevel = PDFAConformanceLevel.UNICODE;
|
||||
protected String producer = "mustangproject";
|
||||
protected String creator = "mustangproject";
|
||||
protected boolean attachZugferdHeaders = true;
|
||||
|
||||
|
||||
protected PDMetadata metadata = null;
|
||||
protected PDFAIdentificationSchema pdfaid = null;
|
||||
protected XMPMetadata xmp = null;
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
* @param pdfFilename
|
||||
* filename of an PDF/A1 compliant document
|
||||
*/
|
||||
public ZUGFeRDExporter load(String pdfFilename) throws IOException {
|
||||
ensurePDFIsValidPDFA(new FileDataSource(pdfFilename));
|
||||
PDDocument doc = PDDocument.load(new File(pdfFilename));
|
||||
prepareDocument(doc);
|
||||
return new ZUGFeRDExporter(doc);
|
||||
}
|
||||
|
||||
|
||||
public void prepareDocument(PDDocument doc) throws IOException {
|
||||
String fullProducer = producer + " (via mustangproject.org " + org.mustangproject.ZUGFeRD.Version.VERSION + ")";
|
||||
|
||||
PDDocumentCatalog cat = doc.getDocumentCatalog();
|
||||
metadata = new PDMetadata(doc);
|
||||
cat.setMetadata(metadata);
|
||||
|
||||
xmp = XMPMetadata.createXMPMetadata();
|
||||
|
||||
pdfaid = new PDFAIdentificationSchema(xmp);
|
||||
|
||||
xmp.addSchema(pdfaid);
|
||||
|
||||
DublinCoreSchema dc = xmp.createAndAddDublinCoreSchema();
|
||||
|
||||
dc.addCreator(creator);
|
||||
|
||||
XMPBasicSchema xsb = xmp.createAndAddXMPBasicSchema();
|
||||
|
||||
xsb.setCreatorTool(creator);
|
||||
xsb.setCreateDate(GregorianCalendar.getInstance());
|
||||
// PDDocumentInformation pdi=doc.getDocumentInformation();
|
||||
PDDocumentInformation pdi = new PDDocumentInformation();
|
||||
pdi.setProducer(fullProducer);
|
||||
pdi.setAuthor(creator);
|
||||
doc.setDocumentInformation(pdi);
|
||||
|
||||
AdobePDFSchema pdf = xmp.createAndAddAdobePDFSchema();
|
||||
pdf.setProducer(fullProducer);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
* @param pdfBinary
|
||||
* binary of a PDF/A1 compliant document
|
||||
*/
|
||||
public ZUGFeRDExporter load(byte[] pdfBinary) throws IOException {
|
||||
ensurePDFIsValidPDFA(new ByteArrayDataSource(new ByteArrayInputStream(pdfBinary)));
|
||||
ZUGFeRDExporter zugFeRDExporter;
|
||||
PDDocument doc = PDDocument.load(pdfBinary);
|
||||
prepareDocument(doc);
|
||||
zugFeRDExporter = new ZUGFeRDExporter(doc);
|
||||
|
||||
return zugFeRDExporter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
* @param pdfSource
|
||||
* source to read a PDF/A1 compliant document from
|
||||
*/
|
||||
public ZUGFeRDExporter load(InputStream pdfSource) throws IOException {
|
||||
return load(readAllBytes(pdfSource));
|
||||
}
|
||||
|
||||
private void ensurePDFIsValidPDFA(final DataSource dataSource) throws IOException {
|
||||
if (!ignorePDFAErrors && !isValidA1(dataSource)) {
|
||||
throw new IOException("File is not a valid PDF/A input file");
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] readAllBytes(InputStream in) throws IOException {
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
IOUtils.copy(in, buffer);
|
||||
return buffer.toByteArray();
|
||||
}
|
||||
|
||||
public byte[] serializeXmpMetadata(XMPMetadata xmpMetadata) throws TransformerException {
|
||||
XmpSerializer serializer = new XmpSerializer();
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
|
||||
String prefix="<?xpacket begin=\"\uFEFF\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>";
|
||||
String suffix="<?xpacket end=\"w\"?>";
|
||||
|
||||
try {
|
||||
buffer.write(prefix.getBytes("UTF-8")); // see https://github.com/ZUGFeRD/mustangproject/issues/44
|
||||
serializer.serialize(xmpMetadata, buffer, false);
|
||||
buffer.write(suffix.getBytes("UTF-8"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return buffer.toByteArray();
|
||||
}
|
||||
|
||||
private static boolean isValidA1(DataSource dataSource) throws IOException {
|
||||
return getPDFAParserValidationResult(new PreflightParser(dataSource));
|
||||
}
|
||||
|
||||
/**
|
||||
* This will add both the RDF-indication which embedded file is Zugferd and the
|
||||
* neccessary PDF/A schema extension description to be able to add this
|
||||
* information to RDF
|
||||
*
|
||||
* @param metadata
|
||||
*/
|
||||
public void addXMP(XMPMetadata metadata) {
|
||||
|
||||
if (attachZugferdHeaders) {
|
||||
XMPSchemaZugferd zf = new XMPSchemaZugferd(metadata, zugferdConformanceLevel);
|
||||
|
||||
metadata.addSchema(zf);
|
||||
}
|
||||
|
||||
XMPSchemaPDFAExtensions pdfaex = new XMPSchemaPDFAExtensions(metadata, attachZugferdHeaders);
|
||||
|
||||
metadata.addSchema(pdfaex);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the ZUGFeRD conformance level (override).
|
||||
*
|
||||
* @param zugferdConformanceLevel
|
||||
* the new conformance level
|
||||
*/
|
||||
public IExporterFactory setZUGFeRDConformanceLevel(ZUGFeRDConformanceLevel zugferdConformanceLevel) {
|
||||
this.zugferdConformanceLevel = zugferdConformanceLevel;
|
||||
return this;
|
||||
}
|
||||
|
||||
private static boolean getPDFAParserValidationResult(PreflightParser parser) throws IOException {
|
||||
/*
|
||||
* Parse the PDF file with PreflightParser that inherits from the
|
||||
* NonSequentialParser. Some additional controls are present to check a set of
|
||||
* PDF/A requirements. (Stream length consistency, EOL after some Keyword...)
|
||||
*/
|
||||
parser.parse();
|
||||
|
||||
try (PreflightDocument document = parser.getPreflightDocument()) {
|
||||
/*
|
||||
* Once the syntax validation is done, the parser can provide a
|
||||
* PreflightDocument (that inherits from PDDocument) This document process the
|
||||
* end of PDF/A validation.
|
||||
*/
|
||||
|
||||
document.validate();
|
||||
|
||||
// Get validation result
|
||||
return document.getResult().isValid();
|
||||
} catch (ValidationException e) {
|
||||
/*
|
||||
* the parse method can throw a SyntaxValidationException if the PDF file can't
|
||||
* be parsed. In this case, the exception contains an instance of
|
||||
* ValidationResult
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* All files are PDF/A-3, setConformance refers to the level conformance.
|
||||
*
|
||||
* PDF/A-3 has three coformance 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.
|
||||
*
|
||||
* Feel free to pass "A" as new level if you know what you are doing :-)
|
||||
*
|
||||
*
|
||||
*/
|
||||
public IExporterFactory setConformanceLevel(PDFAConformanceLevel newLevel) {
|
||||
conformanceLevel = newLevel;
|
||||
return this;
|
||||
}
|
||||
|
||||
public IExporterFactory ignorePDFAErrors() {
|
||||
this.ignorePDFAErrors = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public IExporterFactory setAttachZUGFeRDHeaders(final boolean attachZugferdHeaders) {
|
||||
this.attachZugferdHeaders = attachZugferdHeaders;
|
||||
return this;
|
||||
}
|
||||
|
||||
public IExporterFactory setCreator(String creator) {
|
||||
this.creator = creator;
|
||||
return this;
|
||||
}
|
||||
|
||||
public IExporterFactory setProducer(String producer) {
|
||||
this.producer = producer;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import javax.xml.transform.TransformerException;
|
||||
|
||||
import org.mustangproject.ZUGFeRD.ZUGFeRDExporter;
|
||||
import org.mustangproject.ZUGFeRD.ZUGFeRDExporterFromA1Factory;
|
||||
import org.mustangproject.ZUGFeRD.ZUGFeRDExporterFromA3Factory;
|
||||
import org.mustangproject.ZUGFeRD.ZUGFeRDImporter;
|
||||
|
||||
import com.sanityinc.jargs.CmdLineParser;
|
||||
@@ -43,7 +44,6 @@ public class Toecount {
|
||||
+ "\t--extract= extract invoice.zugferd.pdf to ZUGFeRD-invoice.xml\r\n"
|
||||
+ "\t--upgrade= upgrade ZUGFeRD-invoice.pdf to ZUGFeRD-2-invoice.xml\r\n"
|
||||
+ "\t--a3only= upgrade from PDF/A1 to A3 only \r\n"
|
||||
|
||||
|
||||
);
|
||||
}
|
||||
@@ -60,11 +60,6 @@ public class Toecount {
|
||||
Option<Boolean> upgradeOption = parser.addBooleanOption('u', "upgrade");
|
||||
Option<Boolean> a3onlyOption = parser.addBooleanOption('a', "a3only");
|
||||
|
||||
if (args.length == 0) {
|
||||
printUsage();
|
||||
System.exit(2);
|
||||
|
||||
}
|
||||
try {
|
||||
parser.parse(args);
|
||||
} catch (CmdLineParser.OptionException e) {
|
||||
@@ -93,7 +88,7 @@ public class Toecount {
|
||||
printHelp();
|
||||
}
|
||||
|
||||
if (((directoryName != null) && (directoryName.length() > 0)) || filesFromStdIn.booleanValue()) {
|
||||
else if (((directoryName != null) && (directoryName.length() > 0)) || filesFromStdIn.booleanValue()) {
|
||||
|
||||
StatRun sr = new StatRun();
|
||||
if (ignoreFileExt) {
|
||||
@@ -147,19 +142,15 @@ public class Toecount {
|
||||
* .loadFromPDFA1("invoice.pdf");
|
||||
*/
|
||||
try {
|
||||
ZUGFeRDExporter ze = new ZUGFeRDExporter();
|
||||
ze.PDFmakeA3compliant("./invoice.pdf", "Toecount", System.getProperty("user.name"), true);
|
||||
ZUGFeRDExporter ze = new ZUGFeRDExporterFromA3Factory().setProducer("Toecount").setCreator(System.getProperty("user.name")).load("./invoice.pdf");
|
||||
ze.setZUGFeRDXMLData(Files.readAllBytes(Paths.get("./ZUGFeRD-invoice.xml")));
|
||||
ze.PDFattachZugferdFile(null);
|
||||
|
||||
ze.export("invoice.ZUGFeRD.pdf");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
// } catch (JAXBException e) {
|
||||
// e.printStackTrace();
|
||||
} catch (TransformerException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// } catch (JAXBException e) {
|
||||
// e.printStackTrace();
|
||||
}
|
||||
System.out.println("Written to invoice.ZUGFeRD.pdf");
|
||||
} else if (extractRequested) {
|
||||
ZUGFeRDImporter zi = new ZUGFeRDImporter();
|
||||
@@ -178,7 +169,8 @@ public class Toecount {
|
||||
* .loadFromPDFA1("invoice.pdf");
|
||||
*/
|
||||
try {
|
||||
ZUGFeRDExporter ze = new ZUGFeRDExporterFromA1Factory().setAttachZugferdHeaders(false).loadFromPDFA1("./invoice.pdf");
|
||||
ZUGFeRDExporter ze = new ZUGFeRDExporterFromA1Factory().setAttachZUGFeRDHeaders(false)
|
||||
.load("./invoice.pdf");
|
||||
|
||||
ze.export("invoice.a3.pdf");
|
||||
} catch (IOException e) {
|
||||
@@ -301,6 +293,11 @@ public class Toecount {
|
||||
}
|
||||
System.out.println("Written to ZUGFeRD-2-invoice.xml");
|
||||
|
||||
} else {
|
||||
// no argument or argument unknown
|
||||
printUsage();
|
||||
System.exit(2);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user