-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdftoroff.c
202 lines (191 loc) · 4.39 KB
/
pdftoroff.c
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
/*
* pdftoroff.c
*
* convert a pdf into various text formats, trying to undo page, column and
* paragraph formatting while retaining italic and bold face
*
* the output format can be:
* -r roff
* -w html
* -p plain TeX
* -f text with \[fontname] for font changes and \\ for backslashes
* -t text
* -s fmt arbitrary struct format as fmt
*
* the format is a comma-separate list of strings
* for example, html can also be generated by:
./pdftoroff -s '
<p>,</p>
,,,,,,<i>,</i>,<b>,</b>,true,\,.,<,>,&' file.pdf
*
* other options:
* -m met method for converting: 0-3
* -o ord method for sorting: 0-2
* -d dis minimal distance between blocks of text in the page
* -i n-m page range
*
*
* todo: see man page, section BUGS
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <poppler.h>
#include "pdftext.h"
/*
* main
*/
int main(int argc, char *argv[]) {
gboolean usage, opterr;
int method = 1, order = 1;
int first = 1, last = 0;
struct measure measure = {8, 25, 80, 30, 40, 6, 20, 15, '-'};
struct format *format;
PopplerRectangle *zone = NULL;
/* arguments */
format = &format_roff;
usage = FALSE;
opterr = FALSE;
while (argc > 1 && argv[1][0] == '-') {
switch(argv[1][1]) {
case 'r':
format = &format_roff;
break;
case 'w':
format = &format_html;
break;
case 'p':
format = &format_tex;
break;
case 'f':
format = &format_textfont;
break;
case 't':
format = &format_text;
break;
case 's':
if (argc - 1 < 2) {
printf("-s requires a format\n");
usage = TRUE;
opterr = TRUE;
break;
}
format = parseformat(argv[2]);
if (format == NULL) {
printf("invalid format: %s\n", argv[2]);
usage = TRUE;
opterr = TRUE;
break;
}
argc--;
argv++;
break;
case 'm':
method = atoi(argv[2]);
if (argc - 1 < 2 || method < 0 || method > 4) {
printf("-m requires a method (0-4)\n");
usage = TRUE;
opterr = TRUE;
break;
}
argc--;
argv++;
break;
case 'd':
if (argc - 1 < 2) {
printf("-d requires a distance\n");
usage = TRUE;
opterr = TRUE;
break;
}
measure.blockdistance = atoi(argv[2]);
argc--;
argv++;
break;
case 'o':
order = atoi(argv[2]);
if (argc - 1 < 2 || order < 0 || order > 2) {
printf("-o requires an algorithm (0-2)\n");
usage = TRUE;
opterr = TRUE;
break;
}
argc--;
argv++;
break;
case 'i':
if (argc - 1 < 2 ||
sscanf(argv[2], "%d:%d", &first, &last) != 2) {
printf("-i requires a page range (n:m)\n");
usage = TRUE;
opterr = TRUE;
break;
}
argc--;
argv++;
break;
case 'b':
if (argc - 1 < 2) {
printf("-b requires a box\n");
usage = TRUE;
opterr = TRUE;
break;
}
zone = rectangle_parse(argv[2]);
if (zone == NULL) {
printf("error parsing box\n");
usage = TRUE;
opterr = TRUE;
break;
}
argc--;
argv++;
break;
case 'n':
zone = poppler_rectangle_new();
zone->x1 = -100;
zone->x2 = -100;
zone->y2 = -1;
break;
case 'v':
debugpar = TRUE;
break;
case 'h':
usage = TRUE;
opterr = FALSE;
break;
default:
printf("option not recognized: %s\n", argv[1]);
usage = TRUE;
opterr = TRUE;
break;
}
argc--;
argv++;
}
if (argc - 1 < 1 || usage) {
printf("pdftoroff converts pdf to various text formats\n");
printf("usage:\n\tpdftoroff [-r|-w|-p|-f|-t|-s fmt]");
printf(" [-m method [-d dist] [-o order]]\n");
printf("\t [-i range] [-b box] [-n] [-v] file.pdf\n");
printf("\t\t-r\t\tconvert to roff (default)\n");
printf("\t\t-w\t\tconvert to html\n");
printf("\t\t-p\t\tconvert to plain TeX\n");
printf("\t\t-f\t\tconvert to text with font changes\n");
printf("\t\t-t\t\tconvert to text\n");
printf("\t\t-s fmt\t\toutput format strings\n");
printf("\t\t-m method\tconversion method (0-3)\n");
printf("\t\t-d distance\tminimal distance between ");
printf("blocks of text\n");
printf("\t\t-o order\tblock sorting algorithm (0-2)\n");
printf("\t\t-i range\tpages to convert (n:m)\n");
printf("\t\t-b box\t\tonly convert characters in box\n");
printf("\t\t-n\t\tdo not convert recurring elements\n");
printf("\t\t-v\t\treason for line breaks\n");
exit(opterr || ! usage ? EXIT_FAILURE : EXIT_SUCCESS);
}
/* show file */
showfile(stdout, argv[1], first - 1, last - 1, zone,
method, order, &measure, format);
return EXIT_SUCCESS;
}