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;
96 ok = parseArgs(argDesc, &argc, argv);
97 if (!ok || argc < 2 || argc > 3 || printVersion || printHelp) {
98 fprintf(stderr, "pdftotext version %s\n", xpdfVersion);
99 fprintf(stderr, "%s\n", xpdfCopyright);
101 printUsage("pdftotext", "<PDF-file> [<text-file>]", argDesc);
105 fileName = new GString(argv[1]);
108 globalParams = new GlobalParams(cfgFileName);
109 if (textEncName[0]) {
110 globalParams->setTextEncoding(textEncName);
113 if (!globalParams->setTextEOL(textEOL)) {
114 fprintf(stderr, "Bad '-eol' value on command line\n");
118 globalParams->setErrQuiet(quiet);
121 // get mapping to output encoding
122 if (!(uMap = globalParams->getTextEncoding())) {
123 error(-1, "Couldn't get text encoding");
129 if (ownerPassword[0]) {
130 ownerPW = new GString(ownerPassword);
134 if (userPassword[0]) {
135 userPW = new GString(userPassword);
139 doc = new PDFDoc(fileName, ownerPW, userPW);
150 // check for copy permission
151 if (!doc->okToCopy()) {
152 error(-1, "Copying of text from this document is not allowed.");
156 // construct text file name
158 textFileName = new GString(argv[2]);
160 p = fileName->getCString() + fileName->getLength() - 4;
161 if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF")) {
162 textFileName = new GString(fileName->getCString(),
163 fileName->getLength() - 4);
165 textFileName = fileName->copy();
167 textFileName->append(htmlMeta ? ".html" : ".txt");
174 if (lastPage < 1 || lastPage > doc->getNumPages()) {
175 lastPage = doc->getNumPages();
180 if (!textFileName->cmp("-")) {
183 if (!(f = fopen(textFileName->getCString(), "wb"))) {
184 error(-1, "Couldn't open text file '%s'", textFileName->getCString());
188 fputs("<html>\n", f);
189 fputs("<head>\n", f);
190 doc->getDocInfo(&info);
192 printInfoString(f, info.getDict(), "Title", "<title>", "</title>\n",
194 printInfoString(f, info.getDict(), "Subject",
195 "<meta name=\"Subject\" content=\"", "\">\n", uMap);
196 printInfoString(f, info.getDict(), "Keywords",
197 "<meta name=\"Keywords\" content=\"", "\">\n", uMap);
198 printInfoString(f, info.getDict(), "Author",
199 "<meta name=\"Author\" content=\"", "\">\n", uMap);
200 printInfoString(f, info.getDict(), "Creator",
201 "<meta name=\"Creator\" content=\"", "\">\n", uMap);
202 printInfoString(f, info.getDict(), "Producer",
203 "<meta name=\"Producer\" content=\"", "\">\n", uMap);
204 printInfoDate(f, info.getDict(), "CreationDate",
205 "<meta name=\"CreationDate\" content=\"\">\n");
206 printInfoDate(f, info.getDict(), "LastModifiedDate",
207 "<meta name=\"ModDate\" content=\"\">\n");
210 fputs("</head>\n", f);
211 fputs("<body>\n", f);
219 textOut = new TextOutputDev(textFileName->getCString(), rawOrder, htmlMeta);
220 if (textOut->isOk()) {
221 doc->displayPages(textOut, firstPage, lastPage, 72, 0, gFalse);
225 // write end of HTML file
227 if (!textFileName->cmp("-")) {
230 if (!(f = fopen(textFileName->getCString(), "ab"))) {
231 error(-1, "Couldn't open text file '%s'", textFileName->getCString());
235 fputs("</pre>\n", f);
236 fputs("</body>\n", f);
237 fputs("</html>\n", f);
252 // check for memory leaks
253 Object::memCheck(stderr);
259 static void printInfoString(FILE *f, Dict *infoDict, char *key,
260 char *text1, char *text2, UnicodeMap *uMap) {
268 if (infoDict->lookup(key, &obj)->isString()) {
270 s1 = obj.getString();
271 if ((s1->getChar(0) & 0xff) == 0xfe &&
272 (s1->getChar(1) & 0xff) == 0xff) {
279 while (i < obj.getString()->getLength()) {
281 u = ((s1->getChar(i) & 0xff) << 8) |
282 (s1->getChar(i+1) & 0xff);
285 u = s1->getChar(i) & 0xff;
288 n = uMap->mapUnicode(u, buf, sizeof(buf));
289 fwrite(buf, 1, n, f);
296 static void printInfoDate(FILE *f, Dict *infoDict, char *key, char *fmt) {
300 if (infoDict->lookup(key, &obj)->isString()) {
301 s = obj.getString()->getCString();
302 if (s[0] == 'D' && s[1] == ':') {