attachments
This commit is contained in:
@@ -2,6 +2,9 @@
|
|||||||
- Enhance Charges/Allowances with reasonCode. #432
|
- Enhance Charges/Allowances with reasonCode. #432
|
||||||
- Fix build warnings from editing and building. #415
|
- Fix build warnings from editing and building. #415
|
||||||
- ZUGFeRDVisualizer.toPDF(): generate PDF/A-3b. #400
|
- ZUGFeRDVisualizer.toPDF(): generate PDF/A-3b. #400
|
||||||
|
- allow access to invoice attachments via ZUGFeRDInvoiceImporter zii.getEmbeddedFilenames()/zii.getEmbeddedFile(filename)
|
||||||
|
and XML (zii.getFileAttachments)
|
||||||
|
|
||||||
|
|
||||||
2.12.0
|
2.12.0
|
||||||
=======
|
=======
|
||||||
|
|||||||
@@ -21,12 +21,7 @@ import java.nio.file.Files;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
@@ -67,6 +62,10 @@ public class ZUGFeRDImporter {
|
|||||||
* map filenames of additional XML files to their contents
|
* map filenames of additional XML files to their contents
|
||||||
*/
|
*/
|
||||||
private final HashMap<String, byte[]> additionalXMLs = new HashMap<>();
|
private final HashMap<String, byte[]> additionalXMLs = new HashMap<>();
|
||||||
|
/**
|
||||||
|
* map filenames of all embedded files in the respective PDF
|
||||||
|
*/
|
||||||
|
private final HashMap<String, byte[]> PDFAttachments = new HashMap<>();
|
||||||
/**
|
/**
|
||||||
* Raw XML form of the extracted data - may be directly obtained.
|
* Raw XML form of the extracted data - may be directly obtained.
|
||||||
*/
|
*/
|
||||||
@@ -106,6 +105,29 @@ public class ZUGFeRDImporter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/***
|
||||||
|
* return the file names of all files embedded into the PDF
|
||||||
|
* @see for XML embedded files please use ZUGFeRDInvoiceImporter.getFileAttachments
|
||||||
|
* @return a Stringset
|
||||||
|
*/
|
||||||
|
public Set<String> getEmbeddedFilenames() {
|
||||||
|
return PDFAttachments.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* returns the file contents of the specified filename embedded into the PDF
|
||||||
|
* @param filename String
|
||||||
|
* @return a bytearray, or null if the filename has not been fond
|
||||||
|
*/
|
||||||
|
public byte[] getEmbeddedFile(String filename) {
|
||||||
|
|
||||||
|
if (PDFAttachments.containsKey(filename)) {
|
||||||
|
return PDFAttachments.get(filename);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts a ZUGFeRD invoice from a PDF document represented by an input stream. Errors are reported via exception handling.
|
* Extracts a ZUGFeRD invoice from a PDF document represented by an input stream. Errors are reported via exception handling.
|
||||||
*
|
*
|
||||||
@@ -174,10 +196,10 @@ public class ZUGFeRDImporter {
|
|||||||
/**
|
/**
|
||||||
* filenames for invoice data (ZUGFeRD v1 and v2, Factur-X)
|
* filenames for invoice data (ZUGFeRD v1 and v2, Factur-X)
|
||||||
*/
|
*/
|
||||||
|
final PDEmbeddedFile embeddedFile = fileSpec.getEmbeddedFile();
|
||||||
if ((filename.equals("ZUGFeRD-invoice.xml") || (filename.equals("zugferd-invoice.xml")) || filename.equals("factur-x.xml")) || filename.equals("xrechnung.xml") || filename.equals("order-x.xml") || filename.equals("cida.xml")) {
|
if ((filename.equals("ZUGFeRD-invoice.xml") || (filename.equals("zugferd-invoice.xml")) || filename.equals("factur-x.xml")) || filename.equals("xrechnung.xml") || filename.equals("order-x.xml") || filename.equals("cida.xml")) {
|
||||||
containsMeta = true;
|
containsMeta = true;
|
||||||
|
|
||||||
final PDEmbeddedFile embeddedFile = fileSpec.getEmbeddedFile();
|
|
||||||
// String embeddedFilename = filePath + filename;
|
// String embeddedFilename = filePath + filename;
|
||||||
// File file = new File(filePath + filename);
|
// File file = new File(filePath + filename);
|
||||||
// System.out.println("Writing " + embeddedFilename);
|
// System.out.println("Writing " + embeddedFilename);
|
||||||
@@ -191,9 +213,9 @@ public class ZUGFeRDImporter {
|
|||||||
// fos.close();
|
// fos.close();
|
||||||
}
|
}
|
||||||
if (filename.startsWith("additional_data")) {
|
if (filename.startsWith("additional_data")) {
|
||||||
final PDEmbeddedFile embeddedFile = fileSpec.getEmbeddedFile();
|
|
||||||
additionalXMLs.put(filename, embeddedFile.toByteArray());
|
additionalXMLs.put(filename, embeddedFile.toByteArray());
|
||||||
}
|
}
|
||||||
|
PDFAttachments.put(filename, embeddedFile.toByteArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,6 +236,7 @@ public class ZUGFeRDImporter {
|
|||||||
|
|
||||||
|
|
||||||
public void setRawXML(byte[] rawXML) throws IOException {
|
public void setRawXML(byte[] rawXML) throws IOException {
|
||||||
|
this.containsMeta = true;
|
||||||
this.rawXML = rawXML;
|
this.rawXML = rawXML;
|
||||||
this.version = null;
|
this.version = null;
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Base64;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -16,14 +17,7 @@ import javax.xml.xpath.XPathExpression;
|
|||||||
import javax.xml.xpath.XPathExpressionException;
|
import javax.xml.xpath.XPathExpressionException;
|
||||||
import javax.xml.xpath.XPathFactory;
|
import javax.xml.xpath.XPathFactory;
|
||||||
|
|
||||||
import org.mustangproject.Allowance;
|
import org.mustangproject.*;
|
||||||
import org.mustangproject.BankDetails;
|
|
||||||
import org.mustangproject.Charge;
|
|
||||||
import org.mustangproject.EStandard;
|
|
||||||
import org.mustangproject.Invoice;
|
|
||||||
import org.mustangproject.Item;
|
|
||||||
import org.mustangproject.TradeParty;
|
|
||||||
import org.mustangproject.XMLTools;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.w3c.dom.Node;
|
import org.w3c.dom.Node;
|
||||||
@@ -33,6 +27,7 @@ public class ZUGFeRDInvoiceImporter extends ZUGFeRDImporter {
|
|||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ZUGFeRDInvoiceImporter.class.getCanonicalName()); // log
|
private static final Logger LOGGER = LoggerFactory.getLogger(ZUGFeRDInvoiceImporter.class.getCanonicalName()); // log
|
||||||
private boolean recalcPrice = false;
|
private boolean recalcPrice = false;
|
||||||
private boolean ignoreCalculationErrors = false;
|
private boolean ignoreCalculationErrors = false;
|
||||||
|
private ArrayList<FileAttachment> fileAttachments=new ArrayList<>();
|
||||||
|
|
||||||
public ZUGFeRDInvoiceImporter() {
|
public ZUGFeRDInvoiceImporter() {
|
||||||
super();
|
super();
|
||||||
@@ -346,6 +341,16 @@ public class ZUGFeRDInvoiceImporter extends ZUGFeRDImporter {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// now handling base64 encoded attachments AttachmentBinaryObject=CII, EmbeddedDocumentBinaryObject=UBL
|
||||||
|
xpr = xpath.compile("//*[local-name()=\"AttachmentBinaryObject\"]|//*[local-name()=\"EmbeddedDocumentBinaryObject\"]");
|
||||||
|
NodeList attachmentNodes = (NodeList) xpr.evaluate(getDocument(), XPathConstants.NODESET);
|
||||||
|
for (int i = 0; i < attachmentNodes.getLength(); i++) {
|
||||||
|
FileAttachment fa=new FileAttachment(attachmentNodes.item(i).getAttributes().getNamedItem("filename").getNodeValue(),attachmentNodes.item(i).getAttributes().getNamedItem("mimeCode").getNodeValue(),"",Base64.getDecoder().decode(attachmentNodes.item(i).getTextContent()));
|
||||||
|
fileAttachments.add(fa);
|
||||||
|
// filename = "Aufmass.png" mimeCode = "image/png"
|
||||||
|
//EmbeddedDocumentBinaryObject cbc:EmbeddedDocumentBinaryObject mimeCode="image/png" filename="Aufmass.png"
|
||||||
|
}
|
||||||
|
|
||||||
// item level charges+allowances are not yet handled but a lower item price will
|
// item level charges+allowances are not yet handled but a lower item price will
|
||||||
// be read,
|
// be read,
|
||||||
// so the invoice remains arithmetically correct
|
// so the invoice remains arithmetically correct
|
||||||
@@ -442,6 +447,15 @@ public class ZUGFeRDInvoiceImporter extends ZUGFeRDImporter {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
*
|
||||||
|
* @return the file attachments embedded in XML using base64,
|
||||||
|
* @see for PDF embedded files use getEmbeddedFilenames()/getEmbeddedFile()
|
||||||
|
*/
|
||||||
|
public List<FileAttachment> getFileAttachments() {
|
||||||
|
return fileAttachments;
|
||||||
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* This will parse a XML into a invoice object
|
* This will parse a XML into a invoice object
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -27,12 +27,16 @@ import org.junit.FixMethodOrder;
|
|||||||
import org.junit.runners.MethodSorters;
|
import org.junit.runners.MethodSorters;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.xml.xpath.XPathExpressionException;
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static org.xmlunit.assertj.XmlAssert.assertThat;
|
import static org.xmlunit.assertj.XmlAssert.assertThat;
|
||||||
|
|
||||||
@@ -41,6 +45,7 @@ import static org.xmlunit.assertj.XmlAssert.assertThat;
|
|||||||
public class XRTest extends TestCase {
|
public class XRTest extends TestCase {
|
||||||
final String TARGET_XML = "./target/testout-XR.xml";
|
final String TARGET_XML = "./target/testout-XR.xml";
|
||||||
final String TARGET_EDGE_XML = "./target/testout-XR-Edge.xml";
|
final String TARGET_EDGE_XML = "./target/testout-XR-Edge.xml";
|
||||||
|
|
||||||
public void testXRExport() {
|
public void testXRExport() {
|
||||||
|
|
||||||
// the writing part
|
// the writing part
|
||||||
@@ -116,6 +121,25 @@ public class XRTest extends TestCase {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Invoice readInvoice = new Invoice();
|
||||||
|
ZUGFeRDInvoiceImporter zii = new ZUGFeRDInvoiceImporter();
|
||||||
|
try {
|
||||||
|
|
||||||
|
zii.setRawXML(zf2p.getXML());
|
||||||
|
zii.extractInto(readInvoice);
|
||||||
|
} catch (ParseException | XPathExpressionException xp) {
|
||||||
|
fail("Exception not expected");
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
List<FileAttachment> attachedFiles=zii.getFileAttachments();
|
||||||
|
assertNotNull(attachedFiles);
|
||||||
|
assertEquals(attachedFiles.size(), 1);
|
||||||
|
|
||||||
|
assertTrue(Arrays.equals(attachedFiles.get(0).getData(), b));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testXRExportWithoutStreet() {
|
public void testXRExportWithoutStreet() {
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import java.nio.file.Files;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
|
||||||
/***
|
/***
|
||||||
@@ -285,5 +286,33 @@ public class ZF2ZInvoiceImporterTest extends ResourceCase {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testing if other files embedded in pdf additionally to the invoice can be read correctly
|
||||||
|
* */
|
||||||
|
public void testDetach() {
|
||||||
|
boolean hasExceptions = false;
|
||||||
|
|
||||||
|
byte[] fileA=null;
|
||||||
|
byte[] fileB=null;
|
||||||
|
|
||||||
|
ZUGFeRDInvoiceImporter zii = new ZUGFeRDInvoiceImporter("./target/testout-ZF2PushAttachments.pdf");
|
||||||
|
for (String filename:zii.getEmbeddedFilenames()
|
||||||
|
) {
|
||||||
|
if (filename.equals("one.pdf")) {
|
||||||
|
fileA=zii.getEmbeddedFile(filename);
|
||||||
|
} else if (filename.equals("two.pdf")) {
|
||||||
|
fileB=zii.getEmbeddedFile(filename);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
byte[] b = {12, 13}; // the sample data that was used to write the files
|
||||||
|
|
||||||
|
assertTrue(Arrays.equals(fileA, b));
|
||||||
|
assertEquals(fileA.length, 2);
|
||||||
|
assertTrue(Arrays.equals(fileB, b));
|
||||||
|
assertEquals(fileB.length, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user