Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加自定义JSON格式输出 #494

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Tabula helps you extract tables from PDFs
between 0-100 (inclusive) and preceded by '%',
input will be taken as % of actual width of
the page. Example: --columns %25,50,80.6
-f,--format <FORMAT> Output format: (CSV,TSV,JSON). Default: CSV
-f,--format <FORMAT> Output format: (CSV,TSV,JSON,SJSON). Default: CSV
-g,--guess Guess the portion of the page to analyze per
page.
-h,--help Print this help text.
Expand Down Expand Up @@ -67,6 +67,7 @@ Tabula helps you extract tables from PDFs
-u,--use-line-returns Use embedded line returns in cells. (Only in
spreadsheet mode.)
-v,--version Print version and exit.
-tn, --tableNames 筛选要输出的表
```

It also includes a debugging tool, run `java -cp ./target/tabula-1.0.5-jar-with-dependencies.jar technology.tabula.debug.Debug -h` for the available options.
Expand Down
105 changes: 59 additions & 46 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,27 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
<configuration>
<gpgArguments>
<arg>--pinentry-mode</arg>
<arg>loopback</arg>
</gpgArguments>
</configuration>
</execution>
</executions>
</plugin>
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-gpg-plugin</artifactId>-->
<!--<version>1.6</version>-->
<!--<executions>-->
<!--<execution>-->
<!--<id>sign-artifacts</id>-->
<!--<phase>verify</phase>-->
<!--<goals>-->
<!--<goal>sign</goal>-->
<!--</goals>-->
<!--<configuration>-->
<!--<gpgArguments>-->
<!--<arg>&#45;&#45;pinentry-mode</arg>-->
<!--<arg>loopback</arg>-->
<!--</gpgArguments>-->
<!--</configuration>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
Expand All @@ -159,33 +160,43 @@
<manifest>
<mainClass>technology.tabula.CommandLineApp</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<!-- Travis build workaround -->
<argLine>-Xms1024m -Xmx2048m</argLine>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<!-- Travis build workaround -->
<argLine>-Xms1024m -Xmx2048m</argLine>
</configuration>
</plugin>
<!-- download source jars and link them when running eclipse:eclipse -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.10</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
</plugins>
</build>
</plugin>
<!-- download source jars and link them when running eclipse:eclipse -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.10</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
Expand Down Expand Up @@ -221,6 +232,7 @@
</execution>
</executions>
</plugin>
<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
Expand All @@ -235,6 +247,7 @@
</execution>
</executions>
</plugin>
-->
</plugins>
</build>
</profile>
Expand Down
40 changes: 33 additions & 7 deletions src/main/java/technology/tabula/CommandLineApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javafx.scene.control.Tab;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
Expand All @@ -17,14 +19,12 @@
import org.apache.commons.cli.DefaultParser;
import org.apache.pdfbox.pdmodel.PDDocument;

import org.locationtech.jts.util.StringUtil;
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;
import technology.tabula.writers.*;


public class CommandLineApp {
Expand All @@ -44,13 +44,16 @@ public class CommandLineApp {
private OutputFormat outputFormat;
private String password;
private TableExtractor tableExtractor;
private Map<String, List<String>> tableMap;


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.tableMap = CommandLineApp.whichTableMap(line);

if (line.hasOption('s')) {
this.password = line.getOptionValue('s');
Expand Down Expand Up @@ -165,6 +168,10 @@ private void extractFile(File pdfFile, Appendable outFile) throws ParseException
while (pageIterator.hasNext()) {
Page page = pageIterator.next();

if (page == null) {
continue;
}

if (tableExtractor.verticalRulingPositions != null) {
for (Float verticalRulingPosition : tableExtractor.verticalRulingPositions) {
page.addRuling(new Ruling(0, verticalRulingPosition, 0.0f, (float) page.getHeight()));
Expand Down Expand Up @@ -200,7 +207,8 @@ private void extractFile(File pdfFile, Appendable outFile) throws ParseException
}

private PageIterator getPageIterator(PDDocument pdfDocument) throws IOException {
ObjectExtractor extractor = new ObjectExtractor(pdfDocument);
List<String>tableNames=new ArrayList<String>(tableMap.keySet());
ObjectExtractor extractor = new ObjectExtractor(pdfDocument, tableNames);
return (pages == null) ?
extractor.extract() :
extractor.extract(pages);
Expand Down Expand Up @@ -249,9 +257,17 @@ private static List<Pair<Integer, Rectangle>> whichAreas(CommandLine line) throw

private static List<Integer> whichPages(CommandLine line) throws ParseException {
String pagesOption = line.hasOption('p') ? line.getOptionValue('p') : "1";
String tableName = line.hasOption("tn") ? line.getOptionValue("tn") : "";
if (!"".equals(tableName) && "1".equals(pagesOption))
pagesOption = "all";
return Utils.parsePagesOption(pagesOption);
}

private static Map<String,List<String>> whichTableMap(CommandLine line) throws ParseException{
String pagesOption = line.hasOption("tn") ? line.getOptionValue("tn") : "";
return Utils.parseTableMapOption(pagesOption);
}

private static ExtractionMethod whichExtractionMethod(CommandLine line) {
// -r/--spreadsheet [deprecated; use -l] or -l/--lattice
if (line.hasOption('r') || line.hasOption('l')) {
Expand Down Expand Up @@ -363,7 +379,12 @@ public static Options buildOptions() {
.hasArg()
.argName("PAGES")
.build());

o.addOption(Option.builder("tn")
.longOpt("tableNames")
.desc("Comma separated list of TableName, or all. Examples: --tableName table1,table2")
.hasArg()
.argName("TABLENAMES")
.build());
return o;
}

Expand Down Expand Up @@ -467,6 +488,9 @@ private void writeTables(List<Table> tables, Appendable out) throws IOException
case JSON:
writer = new JSONWriter();
break;
case SJSON:
writer = new SJSONWriter(tableMap);
break;
case TSV:
writer = new TSVWriter();
break;
Expand All @@ -481,6 +505,7 @@ private String getOutputFilename(File pdfFile) {
extension = ".csv";
break;
case JSON:
case SJSON:
extension = ".json";
break;
case TSV:
Expand All @@ -493,7 +518,8 @@ private String getOutputFilename(File pdfFile) {
private enum OutputFormat {
CSV,
TSV,
JSON;
JSON,
SJSON;

static String[] formatNames() {
OutputFormat[] values = OutputFormat.values();
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/technology/tabula/ObjectExtractor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package technology.tabula;

import java.io.IOException;
import java.util.List;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
Expand All @@ -9,8 +10,11 @@ public class ObjectExtractor implements java.io.Closeable {

private final PDDocument pdfDocument;

public ObjectExtractor(PDDocument pdfDocument) {
private final List<String> tableNames;

public ObjectExtractor(PDDocument pdfDocument, List<String> tableNames) {
this.pdfDocument = pdfDocument;
this.tableNames = tableNames;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
Expand All @@ -19,13 +23,21 @@ protected Page extractPage(Integer pageNumber) throws IOException {
throw new java.lang.IndexOutOfBoundsException("Page number does not exist.");
}
PDPage page = pdfDocument.getPage(pageNumber - 1);

ObjectExtractorStreamEngine streamEngine = new ObjectExtractorStreamEngine(page);
streamEngine.processPage(page);

TextStripper textStripper = new TextStripper(pdfDocument, pageNumber);
textStripper.process();

String tableName = "";
//TODO 判断表名是否存在
if (tableNames != null){
//采用文本包含方式判断表名,后续需优化
tableName = Utils.findTableName(tableNames, pdfTextStripper.getContent());
if ("".equals(tableName)) {
return null;
}
}
Utils.sort(textStripper.getTextElements(), Rectangle.ILL_DEFINED_ORDER);

float width, height;
Expand All @@ -49,6 +61,8 @@ protected Page extractPage(Integer pageNumber) throws IOException {
.withMinCharWidth(textStripper.getMinCharWidth())
.withMinCharHeight(textStripper.getMinCharHeight())
.withIndex(textStripper.getSpatialIndex())
.withContent(pdfTextStripper.getContent())
.withTableName(tableName)
.build();
}

Expand Down
Loading