updated reading of invoice attachments

This commit is contained in:
jstaerk
2024-08-08 15:33:00 +02:00
parent 234a43ff77
commit 23244cffa6
6 changed files with 28 additions and 42 deletions

View File

@@ -40,10 +40,7 @@ import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
import org.apache.pdfbox.pdmodel.common.PDNameTreeNode;
import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
import org.mustangproject.EStandard;
import org.mustangproject.Item;
import org.mustangproject.Product;
import org.mustangproject.XMLTools;
import org.mustangproject.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
@@ -65,7 +62,7 @@ public class ZUGFeRDImporter {
/**
* map filenames of all embedded files in the respective PDF
*/
private final HashMap<String, byte[]> PDFAttachments = new HashMap<>();
private final ArrayList<FileAttachment> PDFAttachments = new ArrayList<>();
/**
* Raw XML form of the extracted data - may be directly obtained.
*/
@@ -107,25 +104,13 @@ 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
* @see for XML embedded files please use ZUGFeRDInvoiceImporter.getFileAttachmentsXML
* @return a ArrayList of FileAttachments, empty if none
*/
public Set<String> getEmbeddedFilenames() {
return PDFAttachments.keySet();
public List<FileAttachment> getFileAttachmentsPDF() {
return PDFAttachments;
}
/***
* 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;
}
/**
@@ -196,6 +181,7 @@ public class ZUGFeRDImporter {
/**
* 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")) {
containsMeta = true;
@@ -215,7 +201,7 @@ public class ZUGFeRDImporter {
if (filename.startsWith("additional_data")) {
additionalXMLs.put(filename, embeddedFile.toByteArray());
}
PDFAttachments.put(filename, embeddedFile.toByteArray());
PDFAttachments.add(new FileAttachment(filename, embeddedFile.getSubtype(), "Data", embeddedFile.toByteArray()));
}
}

View File

@@ -345,7 +345,7 @@ public class ZUGFeRDInvoiceImporter extends ZUGFeRDImporter {
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()));
FileAttachment fa=new FileAttachment(attachmentNodes.item(i).getAttributes().getNamedItem("filename").getNodeValue(),attachmentNodes.item(i).getAttributes().getNamedItem("mimeCode").getNodeValue(),"Data", 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"
@@ -449,10 +449,10 @@ public class ZUGFeRDInvoiceImporter extends ZUGFeRDImporter {
/***
*
* @return the file attachments embedded in XML using base64,
* @see for PDF embedded files use getEmbeddedFilenames()/getEmbeddedFile()
* @return the file attachments embedded in XML (using base64) decoded as byte array,
* @see for PDF embedded files in FX use getFileAttachmentsPDF()
*/
public List<FileAttachment> getFileAttachments() {
public List<FileAttachment> getFileAttachmentsXML() {
return fileAttachments;
}

View File

@@ -133,7 +133,7 @@ public class XRTest extends TestCase {
} catch (IOException e) {
throw new RuntimeException(e);
}
List<FileAttachment> attachedFiles=zii.getFileAttachments();
List<FileAttachment> attachedFiles=zii.getFileAttachmentsXML();
assertNotNull(attachedFiles);
assertEquals(attachedFiles.size(), 1);

View File

@@ -21,6 +21,7 @@
*/
package org.mustangproject.ZUGFeRD;
import org.mustangproject.FileAttachment;
import org.mustangproject.Invoice;
import javax.xml.xpath.XPathExpressionException;
@@ -296,15 +297,12 @@ public class ZF2ZInvoiceImporterTest extends ResourceCase {
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);
for (FileAttachment fa:zii.getFileAttachmentsPDF()) {
if (fa.getFilename().equals("one.pdf")) {
fileA=fa.getData();
} else if (fa.getFilename().equals("two.pdf")) {
fileB=fa.getData();
}
}
byte[] b = {12, 13}; // the sample data that was used to write the files