-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathextract-text-cli.cpp
241 lines (220 loc) · 10.6 KB
/
extract-text-cli.cpp
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
#include <iostream>
#include <string>
#include "EStatusCode.h"
#include "BoxingBase.h"
#include "OutputFile.h"
#include "InputStringStream.h"
#include "OutputStreamTraits.h"
#include "IByteReaderWithPosition.h"
#include "IByteWriterWithPosition.h"
#include "TextExtraction.h"
#include "TableExtraction.h"
#include "lib/text-composition/TextComposer.h"
using namespace std;
using namespace PDFHummus;
static void ShowUsage(const string& name)
{
cerr << "Usage: " << name << " filepath <option(s)>\n"
<< "filepath - pdf file path\n"
<< "Options:\n"
<< "\t-s, --start <d>\t\t\t\tstart text extraction from a page index. use negative numbers to subtract from pages count\n"
<< "\t-e, --end <d>\t\t\t\tend text extraction upto page index. use negative numbers to subtract from pages count\n"
#if (SUPPORT_ICU_BIDI==1)
<< "\t-b, --bidi <RTL|LTR>\t\t\tuse bidi algo to convert visual to logical. provide default direction per document writing direction.\n"
#endif
<< "\t-p, --spacing <BOTH|HOR|VER|NONE>\tadd spaces between pieces of text considering their relative positions. default is BOTH\n"
<< "\t-t, --tables\t\t\t\textract tables instead of text. Each table is represented in CSV\n"
<< "\t-o, --output /path/to/file\t\twrite result to output file (or files for tables export)\n"
<< "\t-q, --quiet\t\t\t\tquiet run. only shows errors and warnings\n"
<< "\t-h, --help\t\t\t\tShow this help message\n"
<< "\t-d, --debug /path/to/file\t\tcreate debug output file\n"
<< endl;
}
static const string BIDI_LTR = "LTR";
static const string BIDI_RTL = "RTL";
static const string SPACING_BOTH = "BOTH";
static const string SPACING_HOR = "HOR";
static const string SPACING_VER = "VER";
static const string SPACING_NONE = "NONE";
static const string scCSVExtension = ".csv";
static const string scDot = ".";
static const Byte scUTF8Bom[3] = {0xEF,0xBB,0xBF};
int main(int argc, char* argv[])
{
if(argc < 2) {
ShowUsage(argv[0]);
return 1;
}
string filePath = argv[1];
bool debugging = false;
string debugPath = "";
bool writeToOutputFile = false;
string outputFilePath = "";
TextComposer::ESpacing spacing = TextComposer::eSpacingBoth;
long startPage = 0;
long endPage = -1;
bool quiet = false;
long bidiFlag = -1;
bool extractTables = false;
for (int i = 2; i < argc; ++i) {
std::string arg = argv[i];
if ((arg == "-h") || (arg == "--help")) {
ShowUsage(argv[0]);
return 0;
} else if ((arg == "-q") || (arg == "--quiet")) {
quiet = true;
} else if ((arg == "-t") || (arg == "--tables")) {
extractTables = true;
} else if ((arg == "-s") || (arg == "--start")) {
if (i + 1 < argc) {
startPage = Long(argv[++i]);
} else {
std::cerr << "--start option requires one argument, which is the page to start." << std::endl;
return 1;
}
} else if ((arg == "-e") || (arg == "--end")) {
if (i + 1 < argc) {
endPage = Long(argv[++i]);
} else {
std::cerr << "--end option requires one argument, which is the page to end." << std::endl;
return 1;
}
#if (SUPPORT_ICU_BIDI==1)
} else if ((arg == "-b") || (arg == "--bidi")) {
if (i + 1 < argc) {
string argString = argv[++i];
if(argString == BIDI_LTR)
bidiFlag = 0;
else if(argString == BIDI_RTL)
bidiFlag = 1;
else {
std::cerr << "--bidi option requires one argument to specify document direction, use LTR or RTL." << std::endl;
return 1;
}
} else {
std::cerr << "--bidi option requires one argument to specify document direction, use LTR or RTL." << std::endl;
return 1;
}
#endif
} else if((arg == "-p") || (arg == "--spacing")) {
if (i + 1 < argc) {
string argString = argv[++i];
if(argString == SPACING_BOTH)
spacing = TextComposer::eSpacingBoth;
else if(argString == SPACING_HOR)
spacing = TextComposer::eSpacingHorizontal;
else if(argString == SPACING_VER)
spacing = TextComposer::eSpacingVertical;
else if(argString == SPACING_NONE)
spacing = TextComposer::eSpacingNone;
else {
std::cerr << "--spacing option requires one argument, which is the spaces addition policy. Use either BOTH, HOR (for horizontal only), VER (for vertical only) or NONE." << std::endl;
return 1;
}
} else {
std::cerr << "--spacing option requires one argument, which is the spaces addition policy. Use either BOTH, HOR (for horizontal only), VER (for vertical only) or NONE." << std::endl;
return 1;
}
} else if((arg == "-d") || (arg == "--debug")) {
debugging = true;
if (i + 1 < argc) {
debugPath = argv[++i];
} else {
std::cerr << "--debug option requires one argument, which is the debug output file path." << std::endl;
return 1;
}
} else if((arg == "-o") || (arg == "--output")) {
writeToOutputFile = true;
if (i + 1 < argc) {
outputFilePath = argv[++i];
} else {
std::cerr << "--output option requires one argument, which is the debug output file path." << std::endl;
return 1;
}
} else {
cerr << "Unrecognized option " << arg << std::endl ;
ShowUsage(argv[0]);
return 1;
}
}
EStatusCode status;
if(debugging) {
TextExtraction textExtraction;
status = textExtraction.DecryptPDFForDebugging(filePath, debugPath);
} else {
if(extractTables) {
TableExtraction tableExtraction;
status = tableExtraction.ExtractTables(filePath, startPage, endPage);
if(status != eSuccess) {
cerr << "Error: " << tableExtraction.LatestError.description.c_str() << endl;
}
ExtractionWarningList::iterator it = tableExtraction.LatestWarnings.begin();
for(; it != tableExtraction.LatestWarnings.end(); ++it) {
cerr << "Warning: " << it->description.c_str() << endl;
}
if(status == eSuccess) {
if(writeToOutputFile) {
size_t extensionPos = outputFilePath.find_last_of(scDot);
string baseOutputFilePath = outputFilePath.substr(0, extensionPos);
string filePath = baseOutputFilePath;
int ordinal = 0;
// writing each table to a separate CSV
TableListList::iterator itPages = tableExtraction.tablesForPages.begin();
for(; itPages != tableExtraction.tablesForPages.end() && status == eSuccess; ++itPages) {
TableList::iterator itTables = itPages->begin();
for(; itTables != itPages->end() && status == eSuccess; ++itTables) {
OutputFile outputFile;
string fileFullPath = filePath + scCSVExtension;
status = outputFile.OpenFile(fileFullPath);
if (status != eSuccess) {
cerr << "Error: Cannot open target file path for writing in" << fileFullPath.c_str() << endl;
} else {
outputFile.GetOutputStream()->Write(scUTF8Bom,3);
string result = tableExtraction.GetTableAsCSVText(*itTables,bidiFlag, spacing);
InputStringStream textStream(result);
OutputStreamTraits streamCopier((IByteWriter*)outputFile.GetOutputStream());
status = streamCopier.CopyToOutputStream(&textStream);
cerr << "Wrote table to " << fileFullPath.c_str() << endl;
}
++ordinal;
filePath = baseOutputFilePath + Int(ordinal).ToString();
}
}
} else if(!quiet) {
cout<<tableExtraction.GetAllAsCSVText(bidiFlag, spacing).c_str();
}
}
} else {
TextExtraction textExtraction;
status = textExtraction.ExtractText(filePath, startPage, endPage);
if(status != eSuccess) {
cerr << "Error: " << textExtraction.LatestError.description.c_str() << endl;
}
ExtractionWarningList::iterator it = textExtraction.LatestWarnings.begin();
for(; it != textExtraction.LatestWarnings.end(); ++it) {
cerr << "Warning: " << it->description.c_str() << endl;
}
if(status == eSuccess) {
if(writeToOutputFile) {
OutputFile outputFile;
status = outputFile.OpenFile(outputFilePath);
if (status != eSuccess) {
cerr << "Error: Cannot open target file path for writing in" << outputFilePath.c_str() << endl;
}
else {
outputFile.GetOutputStream()->Write(scUTF8Bom,3);
string result = textExtraction.GetResultsAsText(bidiFlag, spacing);
InputStringStream textStream(result);
OutputStreamTraits streamCopier((IByteWriter*)outputFile.GetOutputStream());
status = streamCopier.CopyToOutputStream(&textStream);
}
cout <<"Wrote text to " << outputFilePath.c_str() << endl;
}
else if(!quiet) {
cout<<textExtraction.GetResultsAsText(bidiFlag, spacing).c_str();
}
}
}
}
return status == eSuccess ? 0:1;
}