1 //========================================================================
5 // Copyright 1997-2002 Glyph & Cog, LLC
7 //========================================================================
14 #include "parseargs.h"
17 #include "GlobalParams.h"
26 #include "TextOutputDev.h"
27 #include "CharTypes.h"
28 #include "UnicodeMap.h"
32 static void printInfoString(FILE *f, Dict *infoDict, char *key,
33 char *text1, char *text2, UnicodeMap *uMap);
34 static void printInfoDate(FILE *f, Dict *infoDict, char *key, char *fmt);
36 static int firstPage = 1;
37 static int lastPage = 0;
38 static GBool rawOrder = gFalse;
39 static GBool htmlMeta = gFalse;
40 static char textEncName[128] = "";
41 static char textEOL[16] = "";
42 static char ownerPassword[33] = "";
43 static char userPassword[33] = "";
44 static GBool quiet = gFalse;
45 static char cfgFileName[256] = "";
46 static GBool printVersion = gFalse;
47 static GBool printHelp = gFalse;
49 static ArgDesc argDesc[] = {
50 {"-f", argInt, &firstPage, 0,
51 "first page to convert"},
52 {"-l", argInt, &lastPage, 0,
53 "last page to convert"},
54 {"-raw", argFlag, &rawOrder, 0,
55 "keep strings in content stream order"},
56 {"-htmlmeta", argFlag, &htmlMeta, 0,
57 "generate a simple HTML file, including the meta information"},
58 {"-enc", argString, textEncName, sizeof(textEncName),
59 "output text encoding name"},
60 {"-eol", argString, textEOL, sizeof(textEOL),
61 "output end-of-line convention (unix, dos, or mac)"},
62 {"-opw", argString, ownerPassword, sizeof(ownerPassword),
63 "owner password (for encrypted files)"},
64 {"-upw", argString, userPassword, sizeof(userPassword),
65 "user password (for encrypted files)"},
66 {"-q", argFlag, &quiet, 0,
67 "don't print any messages or errors"},
68 {"-cfg", argString, cfgFileName, sizeof(cfgFileName),
69 "configuration file to use in place of .xpdfrc"},
70 {"-v", argFlag, &printVersion, 0,
71 "print copyright and version info"},
72 {"-h", argFlag, &printHelp, 0,
73 "print usage information"},
74 {"-help", argFlag, &printHelp, 0,
75 "print usage information"},
76 {"--help", argFlag, &printHelp, 0,
77 "print usage information"},
78 {"-?", argFlag, &printHelp, 0,
79 "print usage information"},
83 int main(int argc, char *argv[]) {
86 GString *textFileName;
87 GString *ownerPW, *userPW;
88 TextOutputDev *textOut;
99 ok = parseArgs(argDesc, &argc, argv);
100 if (!ok || argc < 2 || argc > 3 || printVersion || printHelp) {
101 fprintf(stderr, "pdftotext version %s\n", xpdfVersion);
102 fprintf(stderr, "%s\n", xpdfCopyright);
104 printUsage("pdftotext", "<PDF-file> [<text-file>]", argDesc);
108 fileName = new GString(argv[1]);
111 globalParams = new GlobalParams(cfgFileName);
112 if (textEncName[0]) {
113 globalParams->setTextEncoding(textEncName);
116 if (!globalParams->setTextEOL(textEOL)) {
117 fprintf(stderr, "Bad '-eol' value on command line\n");
121 globalParams->setErrQuiet(quiet);
124 // get mapping to output encoding
125 if (!(uMap = globalParams->getTextEncoding())) {
126 error(-1, "Couldn't get text encoding");
132 if (ownerPassword[0]) {
133 ownerPW = new GString(ownerPassword);
137 if (userPassword[0]) {
138 userPW = new GString(userPassword);
142 doc = new PDFDoc(fileName, ownerPW, userPW);
154 // check for copy permission
155 if (!doc->okToCopy()) {
156 error(-1, "Copying of text from this document is not allowed.");
161 // construct text file name
163 textFileName = new GString(argv[2]);
165 p = fileName->getCString() + fileName->getLength() - 4;
166 if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF")) {
167 textFileName = new GString(fileName->getCString(),
168 fileName->getLength() - 4);
170 textFileName = fileName->copy();
172 textFileName->append(htmlMeta ? ".html" : ".txt");
179 if (lastPage < 1 || lastPage > doc->getNumPages()) {
180 lastPage = doc->getNumPages();
185 if (!textFileName->cmp("-")) {
188 if (!(f = fopen(textFileName->getCString(), "wb"))) {
189 error(-1, "Couldn't open text file '%s'", textFileName->getCString());
194 fputs("<html>\n", f);
195 fputs("<head>\n", f);
196 doc->getDocInfo(&info);
198 printInfoString(f, info.getDict(), "Title", "<title>", "</title>\n",
200 printInfoString(f, info.getDict(), "Subject",
201 "<meta name=\"Subject\" content=\"", "\">\n", uMap);
202 printInfoString(f, info.getDict(), "Keywords",
203 "<meta name=\"Keywords\" content=\"", "\">\n", uMap);
204 printInfoString(f, info.getDict(), "Author",
205 "<meta name=\"Author\" content=\"", "\">\n", uMap);
206 printInfoString(f, info.getDict(), "Creator",
207 "<meta name=\"Creator\" content=\"", "\">\n", uMap);
208 printInfoString(f, info.getDict(), "Producer",
209 "<meta name=\"Producer\" content=\"", "\">\n", uMap);
210 printInfoDate(f, info.getDict(), "CreationDate",
211 "<meta name=\"CreationDate\" content=\"\">\n");
212 printInfoDate(f, info.getDict(), "LastModifiedDate",
213 "<meta name=\"ModDate\" content=\"\">\n");
216 fputs("</head>\n", f);
217 fputs("<body>\n", f);
225 textOut = new TextOutputDev(textFileName->getCString(), rawOrder, htmlMeta);
226 if (textOut->isOk()) {
227 doc->displayPages(textOut, firstPage, lastPage, 72, 0, gFalse);
235 // write end of HTML file
237 if (!textFileName->cmp("-")) {
240 if (!(f = fopen(textFileName->getCString(), "ab"))) {
241 error(-1, "Couldn't open text file '%s'", textFileName->getCString());
246 fputs("</pre>\n", f);
247 fputs("</body>\n", f);
248 fputs("</html>\n", f);
266 // check for memory leaks
267 Object::memCheck(stderr);
273 static void printInfoString(FILE *f, Dict *infoDict, char *key,
274 char *text1, char *text2, UnicodeMap *uMap) {
282 if (infoDict->lookup(key, &obj)->isString()) {
284 s1 = obj.getString();
285 if ((s1->getChar(0) & 0xff) == 0xfe &&
286 (s1->getChar(1) & 0xff) == 0xff) {
293 while (i < obj.getString()->getLength()) {
295 u = ((s1->getChar(i) & 0xff) << 8) |
296 (s1->getChar(i+1) & 0xff);
299 u = s1->getChar(i) & 0xff;
302 n = uMap->mapUnicode(u, buf, sizeof(buf));
303 fwrite(buf, 1, n, f);
310 static void printInfoDate(FILE *f, Dict *infoDict, char *key, char *fmt) {
314 if (infoDict->lookup(key, &obj)->isString()) {
315 s = obj.getString()->getCString();
316 if (s[0] == 'D' && s[1] == ':') {