-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgalapdf.cpp
More file actions
288 lines (247 loc) · 7.16 KB
/
Copy pathgalapdf.cpp
File metadata and controls
288 lines (247 loc) · 7.16 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
/*******************************************************************************
* galapdf action [options] input output *
*******************************************************************************/
#include "galapdf.h"
#include "pdfutil.h"
#include <stdio.h>
#include <string.h>
#include <gtypes.h>
#include <parseargs.h>
#if !defined(_WIN32) && !defined(__WIN32__)
#include <signal.h>
#endif
char cfg_file[1024];
int user_cancelled = 0;
enum pdf_action_type
{
pdf_info,
pdf_fonts,
pdf_to_text,
pdf_extr_imgs,
pdf_to_imgs,
pdf_last_act
};
typedef int (*pdf_action_proc) (int, char**);
typedef struct tag_pdf_action
{
pdf_action_type action_type;
const char* action_name;
pdf_action_proc action_proc;
} pdf_action;
int pdf_info_proc(int argc, char** argv);
int pdf_fonts_proc(int argc, char** argv);
int pdf_to_text_proc(int argc, char** argv);
int pdf_extr_imgs_proc(int argc, char** argv);
int pdf_to_imgs_proc(int argc, char** argv);
pdf_action pdf_actions[pdf_last_act] =
{
{pdf_info, "info", pdf_info_proc},
{pdf_fonts, "fonts", pdf_fonts_proc},
{pdf_to_text, "totext", pdf_to_text_proc},
{pdf_extr_imgs, "extrimgs", pdf_extr_imgs_proc},
{pdf_to_imgs, "toimgs", pdf_to_imgs_proc}
};
int print_pdf_info(char* filename,
char* owner_password,
char* user_password,
char* text_enc_name);
int print_pdf_fonts(char* filename,
char* owner_password,
char* user_password);
int convert_pdf_to_text(char* filename,
char* textfile,
char* owner_password,
char* user_password,
char* text_enc_name,
char* text_eol,
char* range,
GBool phys_layout,
GBool raw_order);
int extract_images_from_pdf(char* filename,
char* target,
char* owner_password,
char* user_password,
char* range,
char* format,
int jpg_quality,
GBool dump_jpg,
GBool tiff_jpg);
int convert_pdf_to_images(char* filename,
char* target,
char* owner_password,
char* user_password,
char* range,
int resolution,
GBool mono,
GBool gray,
char* format,
int jpg_quality,
GBool multi_page,
GBool tiff_jpg);
void signal_trap(int sig)
{
user_cancelled = 1;
}
int main(int argc, char** argv)
{
setvbuf(stdout, NULL, _IONBF, 0);
printf("GalaPDF 1.0.0\n");
int ret_code = gpret_param_err;
#if defined(_WIN32) || defined(__WIN32__)
#define DIR_SEP '\\'
#else
#define DIR_SEP '/'
#endif
strcpy(cfg_file, argv[0]);
int i;
for (i = strlen(cfg_file) - 1; i >= 0; i--)
{
if (cfg_file[i] == DIR_SEP)
{
cfg_file[i + 1] = 0;
strcat(cfg_file, "xpdfrc");
break;
}
}
if (i < 0)
strcpy(cfg_file, "xpdfrc");
#if !defined(_WIN32) && !defined(__WIN32__)
sigset(SIGTERM, signal_trap);
#endif
if (argc >= 3)
{
char* action = argv[1];
for (int i = 0; i < pdf_last_act; i++)
{
if (!strcmp(action, pdf_actions[i].action_name))
{
ret_code = pdf_actions[i].action_proc(argc, argv);
break;
}
}
}
// check for memory leaks
Object::memCheck(stderr);
gMemReport(stderr);
return ret_code;
}
int pdf_info_proc(int argc, char** argv)
{
char owner_password[33] = "\001";
char user_password[33] = "\001";
char text_enc_name[128] = "";
ArgDesc arg_desc[] =
{
{"-opw", argString, owner_password, sizeof(owner_password), ""},
{"-upw", argString, user_password, sizeof(user_password), ""},
{"-enc", argString, text_enc_name, sizeof(text_enc_name), ""},
{0, (ArgKind)0, 0, 0, 0}
};
GBool param_ok = parseArgs(arg_desc, &argc, argv);
if (!param_ok || argc < 3)
return gpret_param_err;
return print_pdf_info(argv[2],
owner_password, user_password, text_enc_name);
}
int pdf_fonts_proc(int argc, char** argv)
{
char owner_password[33] = "\001";
char user_password[33] = "\001";
ArgDesc arg_desc[] =
{
{"-opw", argString, owner_password, sizeof(owner_password), ""},
{"-upw", argString, user_password, sizeof(user_password), ""},
{0, (ArgKind)0, 0, 0, 0}
};
GBool param_ok = parseArgs(arg_desc, &argc, argv);
if (!param_ok || argc < 3)
return gpret_param_err;
return print_pdf_fonts(argv[2], owner_password, user_password);
}
int pdf_to_text_proc(int argc, char** argv)
{
char owner_password[33] = "\001";
char user_password[33] = "\001";
char text_enc_name[128] = "";
char text_eol[16] = "";
char range[1024] = "\0";
GBool phys_layout = gFalse;
GBool raw_order = gFalse;
ArgDesc arg_desc[] =
{
{"-opw", argString, owner_password, sizeof(owner_password), ""},
{"-upw", argString, user_password, sizeof(user_password), ""},
{"-enc", argString, text_enc_name, sizeof(text_enc_name), ""},
{"-eol", argString, &text_eol, sizeof(text_eol), ""},
{"-pr", argString, range, sizeof(range), ""},
{"-raw", argFlag, &raw_order, 0, ""},
{"-layout", argFlag, &phys_layout, 0, ""},
{0, (ArgKind)0, 0, 0, 0}
};
GBool param_ok = parseArgs(arg_desc, &argc, argv);
if (!param_ok || argc < 3)
return gpret_param_err;
return convert_pdf_to_text(argv[2], argc > 3 ? argv[3] : NULL,
owner_password, user_password, text_enc_name,
text_eol, range, phys_layout, raw_order);
}
int pdf_extr_imgs_proc(int argc, char** argv)
{
char owner_password[33] = "\001";
char user_password[33] = "\001";
char format[16] = "";
char range[1024] = "\0";
int jpg_quality = 75;
GBool dump_jpg = gFalse;
GBool tiff_jpg = gFalse;
ArgDesc arg_desc[] =
{
{"-opw", argString, owner_password, sizeof(owner_password), ""},
{"-upw", argString, user_password, sizeof(user_password), ""},
{"-fmt", argString, format, sizeof(format), ""},
{"-pr", argString, range, sizeof(range), ""},
{"-jq", argInt, &jpg_quality, 0, ""},
{"-j", argFlag, &dump_jpg, 0, ""},
{"-tj", argFlag, &tiff_jpg, 0, ""},
{0, (ArgKind)0, 0, 0, 0}
};
GBool param_ok = parseArgs(arg_desc, &argc, argv);
if (!param_ok || argc < 4)
return gpret_param_err;
return extract_images_from_pdf(argv[2], argv[3], owner_password,
user_password, range, format, jpg_quality,
dump_jpg, tiff_jpg);
}
int pdf_to_imgs_proc(int argc, char** argv)
{
char owner_password[33] = "\001";
char user_password[33] = "\001";
char range[1024] = "\0";
int resolution = 150;
int jpg_quality = 75;
GBool mono = gFalse;
GBool gray = gFalse;
GBool multipage = gFalse;
GBool tiff_jpg = gFalse;
char format[16] = "";
ArgDesc arg_desc[] =
{
{"-opw", argString, owner_password, sizeof(owner_password), ""},
{"-upw", argString, user_password, sizeof(user_password), ""},
{"-pr", argString, range, sizeof(range), ""},
{"-r", argInt, &resolution, 0, ""},
{"-mono", argFlag, &mono, 0, ""},
{"-gray", argFlag, &gray, 0, ""},
{"-fmt", argString, format, sizeof(format), ""},
{"-jq", argInt, &jpg_quality, 0, ""},
{"-mp", argFlag, &multipage, 0, ""},
{"-tj", argFlag, &tiff_jpg, 0, ""},
{0, (ArgKind)0, 0, 0, 0}
};
GBool param_ok = parseArgs(arg_desc, &argc, argv);
if (!param_ok || argc < 4)
return gpret_param_err;
return convert_pdf_to_images(argv[2], argv[3], owner_password,
user_password, range, resolution, mono, gray,
format, jpg_quality, multipage, tiff_jpg);
}