toecount can now also merge and extract zugferd PDF files+xml structure

This commit is contained in:
Jochen Stärk
2017-06-10 12:36:58 +02:00
parent 80acdc9972
commit 8612dff895
2 changed files with 116 additions and 58 deletions

View File

@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.mustangproject.ZUGFeRD</groupId> <groupId>org.mustangproject.ZUGFeRD</groupId>
<artifactId>toecount</artifactId> <artifactId>toecount</artifactId>
<version>0.0.9-SNAPSHOT</version> <version>0.1.0-SNAPSHOT</version>
<name>ZUGFeRD PDF checker</name> <name>ZUGFeRD PDF checker</name>
<description>Shows statistics on how many PDFs in a directory are ZUGFeRD-Compliant</description> <description>Shows statistics on how many PDFs in a directory are ZUGFeRD-Compliant</description>
<repositories> <repositories>
@@ -23,6 +23,22 @@
</repository> </repository>
</repositories> </repositories>
<dependencies> <dependencies>
<!-- jaxb?
javax.xml.bind.JAXBException: Provider com.sun.xml.bind.v2.ContextFactory not found
-->
<!-- >dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.5</version>
</dependency -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.5</version>
</dependency>
<dependency> <dependency>
<groupId>org.mustangproject.ZUGFeRD</groupId> <groupId>org.mustangproject.ZUGFeRD</groupId>
<artifactId>mustang</artifactId> <artifactId>mustang</artifactId>

View File

@@ -7,49 +7,55 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import javax.xml.bind.JAXBException;
import javax.xml.transform.TransformerException;
import org.mustangproject.ZUGFeRD.ZUGFeRDExporter;
import org.mustangproject.ZUGFeRD.ZUGFeRDImporter;
import com.sanityinc.jargs.CmdLineParser; import com.sanityinc.jargs.CmdLineParser;
import com.sanityinc.jargs.CmdLineParser.Option; import com.sanityinc.jargs.CmdLineParser.Option;
public class Toecount { public class Toecount {
//build with: /opt/local/bin/mvn clean compile assembly:single // build with: /opt/local/bin/mvn clean compile assembly:single
private static void printUsage() { private static void printUsage() {
System.err System.err.println(getUsage());
.println(getUsage());
} }
private static String getUsage() { private static String getUsage() {
return "Usage: Toecount [-d,--directory] [-l,--listfromstdin] [-i,--ignorefileextension] [-h,--help]\r\n"; return "Usage: Toecount [-d,--directory] [-l,--listfromstdin] [-i,--ignorefileextension] | [-c,--combine] | [-e,--extract] | [-h,--help]\r\n";
} }
private static void printHelp() { private static void printHelp() {
System.out System.out.println("Mustangproject.org's Toecount 0.1.0 \r\n"
.println("Mustangproject.org's Toecount 0.0.7 \r\n"
+ "A Apache Public License command line tool for statistics on PDF invoices with\r\n" + "A Apache Public License command line tool for statistics on PDF invoices with\r\n"
+ "ZUGFeRD Metadata (http://www.zugferd.de)\r\n" + "ZUGFeRD Metadata (http://www.zugferd.org)\r\n" + "\r\n" + getUsage() + "Count operations"
+ "\r\n" + "\t--directory= count ZUGFeRD files in directory to be scanned\r\n"
+ getUsage()
+ "\t--directory= can specify a single PDF or a directory to be scanned\r\n"
+ "\t\tIf it is a directory, it will recurse.\r\n" + "\t\tIf it is a directory, it will recurse.\r\n"
+ "\t--listfromstdin allows to specify a list of linefeed separated files on runtime.\r\n" + "\t--listfromstdin=count ZUGFeRD files from a list of linefeed separated files on runtime.\r\n"
+ "\t\tIt will start once a blank line has been entered.\r\n" + "\t\tIt will start once a blank line has been entered.\r\n"
+ "\t--ignorefileextension check *.* instead of *.pdf files"); + "\t--ignorefileextension=if PDF files are counted check *.* instead of *.pdf files"
+ "Merge operations"
+ "\t--combine= combine ZUGFeRD-invoice.xml and invoice.pdf to invoice.zugferd.pdf\r\n"
+ "\t--extract= extract invoice.zugferd.pdf to ZUGFeRD-invoice.xml\r\n"
);
} }
// /opt/local/bin/mvn clean compile assembly:single // /opt/local/bin/mvn clean compile assembly:single
public static void main(String[] args) { public static void main(String[] args) {
CmdLineParser parser = new CmdLineParser(); CmdLineParser parser = new CmdLineParser();
Option<String> dirnameOption = parser.addStringOption('d', "directory"); Option<String> dirnameOption = parser.addStringOption('d', "directory");
Option<Boolean> filesFromStdInOption = parser.addBooleanOption('l', Option<Boolean> filesFromStdInOption = parser.addBooleanOption('l', "listfromstdin");
"listfromstdin"); Option<Boolean> ignoreFileExtOption = parser.addBooleanOption('i', "ignorefileextension");
Option<Boolean> ignoreFileExtOption = parser.addBooleanOption('i', Option<Boolean> combineOption = parser.addBooleanOption('c', "combine");
"ignorefileextension"); Option<Boolean> extractOption = parser.addBooleanOption('e', "extract");
Option<Boolean> helpOption = parser.addBooleanOption('h', Option<Boolean> helpOption = parser.addBooleanOption('h', "help");
"help");
if (args.length==0) { if (args.length == 0) {
printUsage(); printUsage();
System.exit(2); System.exit(2);
} }
try { try {
parser.parse(args); parser.parse(args);
@@ -61,63 +67,99 @@ public class Toecount {
String directoryName = parser.getOptionValue(dirnameOption); String directoryName = parser.getOptionValue(dirnameOption);
Boolean filesFromStdIn = parser.getOptionValue(filesFromStdInOption, Boolean filesFromStdIn = parser.getOptionValue(filesFromStdInOption, Boolean.FALSE);
Boolean.FALSE);
Boolean combineRequested = parser.getOptionValue(combineOption, Boolean.FALSE);
Boolean helpRequested = parser.getOptionValue(helpOption,
Boolean.FALSE); Boolean extractRequested = parser.getOptionValue(extractOption, Boolean.FALSE);
Boolean helpRequested = parser.getOptionValue(helpOption, Boolean.FALSE);
Boolean ignoreFileExt = parser.getOptionValue(ignoreFileExtOption, Boolean.FALSE);
Boolean ignoreFileExt = parser.getOptionValue(ignoreFileExtOption,
Boolean.FALSE);
if (helpRequested) { if (helpRequested) {
printHelp(); printHelp();
} }
StatRun sr=new StatRun(); if (((directoryName!= null) && (directoryName.length() > 0)) || filesFromStdIn.booleanValue()) {
if (ignoreFileExt) {
sr.ignoreFileExtension();
}
if (directoryName != null) {
Path startingDir = Paths.get(directoryName);
if (Files.isRegularFile(startingDir)) { StatRun sr = new StatRun();
String filename = startingDir.toString(); if (ignoreFileExt) {
FileChecker fc = new FileChecker(filename, sr); sr.ignoreFileExtension();
}
fc.checkForZUGFeRD(); if (directoryName != null) {
System.out.print(fc.getOutputLine()); Path startingDir = Paths.get(directoryName);
if (Files.isRegularFile(startingDir)) {
String filename = startingDir.toString();
FileChecker fc = new FileChecker(filename, sr);
fc.checkForZUGFeRD();
System.out.print(fc.getOutputLine());
} else if (Files.isDirectory(startingDir)) {
FileTraverser pf = new FileTraverser(sr);
try {
Files.walkFileTree(startingDir, pf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (Files.isDirectory(startingDir)) {
FileTraverser pf = new FileTraverser(sr);
try {
Files.walkFileTree(startingDir, pf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
} }
} if (filesFromStdIn) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
if (filesFromStdIn) { String s;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); try {
String s;
try {
while ((s = in.readLine()) != null && s.length() != 0) { while ((s = in.readLine()) != null && s.length() != 0) {
FileChecker fc = new FileChecker(s, sr); FileChecker fc = new FileChecker(s, sr);
fc.checkForZUGFeRD(); fc.checkForZUGFeRD();
System.out.print(fc.getOutputLine()); System.out.print(fc.getOutputLine());
} }
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
}
System.out.println(sr.getSummaryLine());
} else if (combineRequested) {
/*
* ZUGFeRDExporter ze= new ZUGFeRDExporterFromA1Factory()
* .setProducer("toecount")
* .setCreator(System.getProperty("user.name"))
* .loadFromPDFA1("invoice.pdf");
*/
try {
ZUGFeRDExporter ze = new ZUGFeRDExporter();
ze.PDFmakeA3compliant("./invoice.pdf", "Toecount", System.getProperty("user.name"), true);
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();
}
System.out.println("Written to invoice.ZUGFeRD.pdf");
} else if (extractRequested) {
ZUGFeRDImporter zi = new ZUGFeRDImporter();
zi.extract("invoice.zugferd.pdf");
try {
Files.write(Paths.get("./ZUGFeRD-invoice.xml"), zi.getRawXML());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Written to ZUGFeRD-invoice.xml");
} }
System.out.println(sr.getSummaryLine());
} }
} }