-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathCommandLineApp.java
More file actions
547 lines (472 loc) · 21.2 KB
/
CommandLineApp.java
File metadata and controls
547 lines (472 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package technology.tabula;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FilenameFilter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.DefaultParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import technology.tabula.detectors.DetectionAlgorithm;
import technology.tabula.detectors.NurminenDetectionAlgorithm;
import technology.tabula.extractors.BasicExtractionAlgorithm;
import technology.tabula.extractors.SpreadsheetExtractionAlgorithm;
import technology.tabula.writers.CSVWriter;
import technology.tabula.writers.JSONWriter;
import technology.tabula.writers.TSVWriter;
import technology.tabula.writers.Writer;
public class CommandLineApp {
private static String VERSION = "1.0.5";
private static String VERSION_STRING = String.format("tabula %s (c) 2012-2020 Manuel Aristarán", VERSION);
private static String BANNER = "\nTabula helps you extract tables from PDFs\n\n";
private static final int RELATIVE_AREA_CALCULATION_MODE = 0;
private static final int ABSOLUTE_AREA_CALCULATION_MODE = 1;
private Appendable defaultOutput;
private List<Pair<Integer, Rectangle>> pageAreas;
private List<Integer> pages;
private OutputFormat outputFormat;
private String password;
private TableExtractor tableExtractor;
private Integer lineColorFilter;
public CommandLineApp(Appendable defaultOutput, CommandLine line) throws ParseException {
this.defaultOutput = defaultOutput;
this.pageAreas = CommandLineApp.whichAreas(line);
this.pages = CommandLineApp.whichPages(line);
this.outputFormat = CommandLineApp.whichOutputFormat(line);
this.tableExtractor = CommandLineApp.createExtractor(line);
this.lineColorFilter = CommandLineApp.whichLineColorFilter(line);
if (line.hasOption('s')) {
this.password = line.getOptionValue('s');
}
}
public static void main(String[] args) {
CommandLineParser parser = new DefaultParser();
try {
// parse the command line arguments
CommandLine line = parser.parse(buildOptions(), args);
if (line.hasOption('h')) {
printHelp();
System.exit(0);
}
if (line.hasOption('v')) {
System.out.println(VERSION_STRING);
System.exit(0);
}
new CommandLineApp(System.out, line).extractTables(line);
} catch (ParseException exp) {
System.err.println("Error: " + exp.getMessage());
System.exit(1);
}
System.exit(0);
}
public void extractTables(CommandLine line) throws ParseException {
if (line.hasOption('b')) {
if (line.getArgs().length != 0) {
throw new ParseException("Filename specified with batch\nTry --help for help");
}
File pdfDirectory = new File(line.getOptionValue('b'));
if (!pdfDirectory.isDirectory()) {
throw new ParseException("Directory does not exist or is not a directory");
}
extractDirectoryTables(line, pdfDirectory);
return;
}
if (line.getArgs().length != 1) {
throw new ParseException("Need exactly one filename\nTry --help for help");
}
File pdfFile = new File(line.getArgs()[0]);
if (!pdfFile.exists()) {
throw new ParseException("File does not exist");
}
extractFileTables(line, pdfFile);
}
public void extractDirectoryTables(CommandLine line, File pdfDirectory) throws ParseException {
File[] pdfs = pdfDirectory.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".pdf");
}
});
for (File pdfFile : pdfs) {
File outputFile = new File(getOutputFilename(pdfFile));
extractFileInto(pdfFile, outputFile);
}
}
public void extractFileTables(CommandLine line, File pdfFile) throws ParseException {
if (!line.hasOption('o')) {
extractFile(pdfFile, this.defaultOutput);
return;
}
File outputFile = new File(line.getOptionValue('o'));
extractFileInto(pdfFile, outputFile);
}
public void extractFileInto(File pdfFile, File outputFile) throws ParseException {
BufferedWriter bufferedWriter = null;
try {
FileWriter fileWriter = new FileWriter(outputFile.getAbsoluteFile());
bufferedWriter = new BufferedWriter(fileWriter);
outputFile.createNewFile();
extractFile(pdfFile, bufferedWriter);
} catch (IOException e) {
throw new ParseException("Cannot create file " + outputFile);
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
System.out.println("Error in closing the BufferedWriter" + e);
}
}
}
}
private void extractFile(File pdfFile, Appendable outFile) throws ParseException {
PDDocument pdfDocument = null;
try {
pdfDocument = this.password == null ? PDDocument.load(pdfFile) : PDDocument.load(pdfFile, this.password);
PageIterator pageIterator = getPageIterator(pdfDocument);
List<Table> tables = new ArrayList<>();
while (pageIterator.hasNext()) {
Page page = pageIterator.next();
if (tableExtractor.verticalRulingPositions != null) {
for (Float verticalRulingPosition : tableExtractor.verticalRulingPositions) {
page.addRuling(new Ruling(0, verticalRulingPosition, 0.0f, (float) page.getHeight()));
}
}
if (pageAreas != null) {
for (Pair<Integer, Rectangle> areaPair : pageAreas) {
Rectangle area = areaPair.getRight();
if (areaPair.getLeft() == RELATIVE_AREA_CALCULATION_MODE) {
area = new Rectangle((float) (area.getTop() / 100 * page.getHeight()),
(float) (area.getLeft() / 100 * page.getWidth()), (float) (area.getWidth() / 100 * page.getWidth()),
(float) (area.getHeight() / 100 * page.getHeight()));
}
tables.addAll(tableExtractor.extractTables(page.getArea(area)));
}
} else {
tables.addAll(tableExtractor.extractTables(page));
}
}
writeTables(tables, outFile);
} catch (IOException e) {
throw new ParseException(e.getMessage());
} finally {
try {
if (pdfDocument != null) {
pdfDocument.close();
}
} catch (IOException e) {
System.out.println("Error in closing pdf document" + e);
}
}
}
private PageIterator getPageIterator(PDDocument pdfDocument) throws IOException {
ObjectExtractor extractor = new ObjectExtractor(pdfDocument, lineColorFilter);
return (pages == null) ?
extractor.extract() :
extractor.extract(pages);
}
// CommandLine parsing methods
private static OutputFormat whichOutputFormat(CommandLine line) throws ParseException {
if (!line.hasOption('f')) {
return OutputFormat.CSV;
}
try {
return OutputFormat.valueOf(line.getOptionValue('f'));
} catch (IllegalArgumentException e) {
throw new ParseException(String.format(
"format %s is illegal. Available formats: %s",
line.getOptionValue('f'),
Utils.join(",", OutputFormat.formatNames())));
}
}
private static List<Pair<Integer, Rectangle>> whichAreas(CommandLine line) throws ParseException {
if (!line.hasOption('a')) {
return null;
}
String[] optionValues = line.getOptionValues('a');
List<Pair<Integer, Rectangle>> areaList = new ArrayList<Pair<Integer, Rectangle>>();
for (String optionValue : optionValues) {
int areaCalculationMode = ABSOLUTE_AREA_CALCULATION_MODE;
int startIndex = 0;
if (optionValue.startsWith("%")) {
startIndex = 1;
areaCalculationMode = RELATIVE_AREA_CALCULATION_MODE;
}
List<Float> f = parseFloatList(optionValue.substring(startIndex));
if (f.size() != 4) {
throw new ParseException("area parameters must be top,left,bottom,right optionally preceded by %");
}
areaList.add(new Pair<Integer, Rectangle>(areaCalculationMode, new Rectangle(f.get(0), f.get(1), f.get(3) - f.get(1), f.get(2) - f.get(0))));
}
return areaList;
}
private static List<Integer> whichPages(CommandLine line) throws ParseException {
String pagesOption = line.hasOption('p') ? line.getOptionValue('p') : "1";
return Utils.parsePagesOption(pagesOption);
}
private static ExtractionMethod whichExtractionMethod(CommandLine line) {
// -r/--spreadsheet [deprecated; use -l] or -l/--lattice
if (line.hasOption('r') || line.hasOption('l')) {
return ExtractionMethod.SPREADSHEET;
}
// -n/--no-spreadsheet [deprecated; use -t] or -c/--columns or -g/--guess or -t/--stream
if (line.hasOption('n') || line.hasOption('c') || line.hasOption('t')) {
return ExtractionMethod.BASIC;
}
return ExtractionMethod.DECIDE;
}
private static Integer whichLineColorFilter(CommandLine line) throws ParseException {
if (!line.hasOption("line-color-filter")) {
return null;
}
Integer result;
try {
result = Integer.parseInt(line.getOptionValue("line-color-filter"), 16);
} catch (NumberFormatException e) {
throw new ParseException("line-color-filter parameter must be a hexadecimal number");
}
if (result < 0 || result > 0xFFFFFF) {
throw new ParseException("line-color-filter parameter must be at most FFFFFF");
}
return result;
}
private static TableExtractor createExtractor(CommandLine line) throws ParseException {
TableExtractor extractor = new TableExtractor();
extractor.setGuess(line.hasOption('g'));
extractor.setMethod(CommandLineApp.whichExtractionMethod(line));
extractor.setUseLineReturns(line.hasOption('u'));
if (line.hasOption('c')) {
String optionString = line.getOptionValue('c');
if (optionString.startsWith("%")) {
extractor.setVerticalRulingPositionsRelative(true);
optionString = optionString.substring(1);
}
extractor.setVerticalRulingPositions(parseFloatList(optionString));
}
return extractor;
}
// utilities, etc.
public static List<Float> parseFloatList(String option) throws ParseException {
String[] f = option.split(",");
List<Float> rv = new ArrayList<>();
try {
for (final String element : f) {
rv.add(Float.parseFloat(element));
}
return rv;
} catch (NumberFormatException e) {
throw new ParseException("Wrong number syntax");
}
}
private static void printHelp() {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("tabula", BANNER, buildOptions(), "", true);
}
public static Options buildOptions() {
Options o = new Options();
o.addOption("v", "version", false, "Print version and exit.");
o.addOption("h", "help", false, "Print this help text.");
o.addOption("g", "guess", false, "Guess the portion of the page to analyze per page.");
o.addOption("r", "spreadsheet", false, "[Deprecated in favor of -l/--lattice] Force PDF to be extracted using spreadsheet-style extraction (if there are ruling lines separating each cell, as in a PDF of an Excel spreadsheet)");
o.addOption("n", "no-spreadsheet", false, "[Deprecated in favor of -t/--stream] Force PDF not to be extracted using spreadsheet-style extraction (if there are no ruling lines separating each cell)");
o.addOption("l", "lattice", false, "Force PDF to be extracted using lattice-mode extraction (if there are ruling lines separating each cell, as in a PDF of an Excel spreadsheet)");
o.addOption("t", "stream", false, "Force PDF to be extracted using stream-mode extraction (if there are no ruling lines separating each cell)");
o.addOption("i", "silent", false, "Suppress all stderr output.");
o.addOption("u", "use-line-returns", false, "Use embedded line returns in cells. (Only in spreadsheet mode.)");
// o.addOption("d", "debug", false, "Print detected table areas instead of processing.");
o.addOption(Option.builder("b")
.longOpt("batch")
.desc("Convert all .pdfs in the provided directory.")
.hasArg()
.argName("DIRECTORY")
.build());
o.addOption(Option.builder("o")
.longOpt("outfile")
.desc("Write output to <file> instead of STDOUT. Default: -")
.hasArg()
.argName("OUTFILE")
.build());
o.addOption(Option.builder("f")
.longOpt("format")
.desc("Output format: (" + Utils.join(",", OutputFormat.formatNames()) + "). Default: CSV")
.hasArg()
.argName("FORMAT")
.build());
o.addOption(Option.builder("s")
.longOpt("password")
.desc("Password to decrypt document. Default is empty")
.hasArg()
.argName("PASSWORD")
.build());
o.addOption(Option.builder("c")
.longOpt("columns")
.desc("X coordinates of column boundaries. Example --columns 10.1,20.2,30.3. "
+ "If all values are between 0-100 (inclusive) and preceded by '%', input will be taken as % of actual width of the page. "
+ "Example: --columns %25,50,80.6")
.hasArg()
.argName("COLUMNS")
.build());
o.addOption(Option.builder("a")
.longOpt("area")
.desc("-a/--area = Portion of the page to analyze. Example: --area 269.875,12.75,790.5,561. "
+ "Accepts top,left,bottom,right i.e. y1,x1,y2,x2 where all values are in points relative to the top left corner. "
+ "If all values are between 0-100 (inclusive) and preceded by '%', input will be taken as % of actual height or width of the page. "
+ "Example: --area %0,0,100,50. To specify multiple areas, -a option should be repeated. Default is entire page")
.hasArg()
.argName("AREA")
.build());
o.addOption(Option.builder("p")
.longOpt("pages")
.desc("Comma separated list of ranges, or all. Examples: --pages 1-3,5-7, --pages 3 or --pages all. Default is --pages 1")
.hasArg()
.argName("PAGES")
.build());
o.addOption(Option.builder(null)
.longOpt("line-color-filter")
.desc("Only consider lines of this color to be lattice lines. Example: --line-color-filter DEADBE .")
.hasArg()
.argName("COLOR")
.build());
return o;
}
private static class TableExtractor {
private boolean guess = false;
private boolean useLineReturns = false;
private BasicExtractionAlgorithm basicExtractor = new BasicExtractionAlgorithm();
private SpreadsheetExtractionAlgorithm spreadsheetExtractor = new SpreadsheetExtractionAlgorithm();
private boolean verticalRulingPositionsRelative = false;
private List<Float> verticalRulingPositions = null;
private ExtractionMethod method = ExtractionMethod.BASIC;
public TableExtractor() {
}
public void setVerticalRulingPositions(List<Float> positions) {
this.verticalRulingPositions = positions;
}
public void setVerticalRulingPositionsRelative(boolean relative) {
this.verticalRulingPositionsRelative = relative;
}
public void setGuess(boolean guess) {
this.guess = guess;
}
public void setUseLineReturns(boolean useLineReturns) {
this.useLineReturns = useLineReturns;
}
public void setMethod(ExtractionMethod method) {
this.method = method;
}
public List<Table> extractTables(Page page) {
ExtractionMethod effectiveMethod = this.method;
if (effectiveMethod == ExtractionMethod.DECIDE) {
effectiveMethod = spreadsheetExtractor.isTabular(page) ?
ExtractionMethod.SPREADSHEET :
ExtractionMethod.BASIC;
}
switch (effectiveMethod) {
case BASIC:
return extractTablesBasic(page);
case SPREADSHEET:
return extractTablesSpreadsheet(page);
default:
return new ArrayList<>();
}
}
public List<Table> extractTablesBasic(Page page) {
if (guess) {
// guess the page areas to extract using a detection algorithm
// currently we only have a detector that uses spreadsheets to find table areas
DetectionAlgorithm detector = new NurminenDetectionAlgorithm();
List<Rectangle> guesses = detector.detect(page);
List<Table> tables = new ArrayList<>();
for (Rectangle guessRect : guesses) {
Page guess = page.getArea(guessRect);
tables.addAll(basicExtractor.extract(guess));
}
return tables;
}
if (verticalRulingPositions != null) {
List<Float> absoluteRulingPositions;
if (this.verticalRulingPositionsRelative) {
// convert relative to absolute
absoluteRulingPositions = new ArrayList<>(verticalRulingPositions.size());
for (float relative : this.verticalRulingPositions) {
float absolute = (float) (relative / 100.0 * page.getWidth());
absoluteRulingPositions.add(absolute);
}
} else {
absoluteRulingPositions = this.verticalRulingPositions;
}
return basicExtractor.extract(page, absoluteRulingPositions);
}
return basicExtractor.extract(page);
}
public List<Table> extractTablesSpreadsheet(Page page) {
// TODO add useLineReturns
return spreadsheetExtractor.extract(page);
}
}
private void writeTables(List<Table> tables, Appendable out) throws IOException {
Writer writer = null;
switch (outputFormat) {
case CSV:
writer = new CSVWriter();
break;
case JSON:
writer = new JSONWriter();
break;
case TSV:
writer = new TSVWriter();
break;
}
writer.write(out, tables);
}
private String getOutputFilename(File pdfFile) {
String extension = ".csv";
switch (outputFormat) {
case CSV:
extension = ".csv";
break;
case JSON:
extension = ".json";
break;
case TSV:
extension = ".tsv";
break;
}
return pdfFile.getPath().replaceFirst("(\\.pdf|)$", extension);
}
private enum OutputFormat {
CSV,
TSV,
JSON;
static String[] formatNames() {
OutputFormat[] values = OutputFormat.values();
String[] rv = new String[values.length];
for (int i = 0; i < values.length; i++) {
rv[i] = values[i].name();
}
return rv;
}
}
private enum ExtractionMethod {
BASIC,
SPREADSHEET,
DECIDE
}
private class DebugOutput {
private boolean debugEnabled;
public DebugOutput(boolean debug) {
this.debugEnabled = debug;
}
public void debug(String msg) {
if (this.debugEnabled) {
System.err.println(msg);
}
}
}
}