1 //========================================================================
5 // Copyright 1997-2003 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 physLayout = gFalse;
39 static GBool rawOrder = gFalse;
40 static GBool htmlMeta = gFalse;
41 static char textEncName[128] = "";
42 static char textEOL[16] = "";
43 static char ownerPassword[33] = "";
44 static char userPassword[33] = "";
45 static GBool quiet = gFalse;
46 static char cfgFileName[256] = "";
47 static GBool printVersion = gFalse;
48 static GBool printHelp = gFalse;
50 static ArgDesc argDesc[] = {
51 {"-f", argInt, &firstPage, 0,
52 "first page to convert"},
53 {"-l", argInt, &lastPage, 0,
54 "last page to convert"},
55 {"-layout", argFlag, &physLayout, 0,
56 "maintain original physical layout"},
57 {"-raw", argFlag, &rawOrder, 0,
58 "keep strings in content stream order"},
59 {"-htmlmeta", argFlag, &htmlMeta, 0,
60 "generate a simple HTML file, including the meta information"},
61 {"-enc", argString, textEncName, sizeof(textEncName),
62 "output text encoding name"},
63 {"-eol", argString, textEOL, sizeof(textEOL),
64 "output end-of-line convention (unix, dos, or mac)"},
65 {"-opw", argString, ownerPassword, sizeof(ownerPassword),
66 "owner password (for encrypted files)"},
67 {"-upw", argString, userPassword, sizeof(userPassword),
68 "user password (for encrypted files)"},
69 {"-q", argFlag, &quiet, 0,
70 "don't print any messages or errors"},
71 {"-cfg", argString, cfgFileName, sizeof(cfgFileName),
72 "configuration file to use in place of .xpdfrc"},
73 {"-v", argFlag, &printVersion, 0,
74 "print copyright and version info"},
75 {"-h", argFlag, &printHelp, 0,
76 "print usage information"},
77 {"-help", argFlag, &printHelp, 0,
78 "print usage information"},
79 {"--help", argFlag, &printHelp, 0,
80 "print usage information"},
81 {"-?", argFlag, &printHelp, 0,
82 "print usage information"},
86 int main(int argc, char *argv[]) {
89 GString *textFileName;
90 GString *ownerPW, *userPW;
91 TextOutputDev *textOut;
102 ok = parseArgs(argDesc, &argc, argv);
103 if (!ok || argc < 2 || argc > 3 || printVersion || printHelp) {
104 fprintf(stderr, "pdftotext version %s\n", xpdfVersion);
105 fprintf(stderr, "%s\n", xpdfCopyright);
107 printUsage("pdftotext", "<PDF-file> [<text-file>]", argDesc);
111 fileName = new GString(argv[1]);
114 globalParams = new GlobalParams(cfgFileName);
115 if (textEncName[0]) {
116 globalParams->setTextEncoding(textEncName);
119 if (!globalParams->setTextEOL(textEOL)) {
120 fprintf(stderr, "Bad '-eol' value on command line\n");
124 globalParams->setErrQuiet(quiet);
127 // get mapping to output encoding
128 if (!(uMap = globalParams->getTextEncoding())) {
129 error(-1, "Couldn't get text encoding");
135 if (ownerPassword[0]) {
136 ownerPW = new GString(ownerPassword);
140 if (userPassword[0]) {
141 userPW = new GString(userPassword);
145 doc = new PDFDoc(fileName, ownerPW, userPW);
157 // check for copy permission
158 if (!doc->okToCopy()) {
159 error(-1, "Copying of text from this document is not allowed.");
164 // construct text file name
166 textFileName = new GString(argv[2]);
168 p = fileName->getCString() + fileName->getLength() - 4;
169 if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF")) {
170 textFileName = new GString(fileName->getCString(),
171 fileName->getLength() - 4);
173 textFileName = fileName->copy();
175 textFileName->append(htmlMeta ? ".html" : ".txt");
182 if (lastPage < 1 || lastPage > doc->getNumPages()) {
183 lastPage = doc->getNumPages();
188 if (!textFileName->cmp("-")) {
191 if (!(f = fopen(textFileName->getCString(), "wb"))) {
192 error(-1, "Couldn't open text file '%s'", textFileName->getCString());
197 fputs("<html>\n", f);
198 fputs("<head>\n", f);
199 doc->getDocInfo(&info);
201 printInfoString(f, info.getDict(), "Title", "<title>", "</title>\n",
203 printInfoString(f, info.getDict(), "Subject",
204 "<meta name=\"Subject\" content=\"", "\">\n", uMap);
205 printInfoString(f, info.getDict(), "Keywords",
206 "<meta name=\"Keywords\" content=\"", "\">\n", uMap);
207 printInfoString(f, info.getDict(), "Author",
208 "<meta name=\"Author\" content=\"", "\">\n", uMap);
209 printInfoString(f, info.getDict(), "Creator",
210 "<meta name=\"Creator\" content=\"", "\">\n", uMap);
211 printInfoString(f, info.getDict(), "Producer",
212 "<meta name=\"Producer\" content=\"", "\">\n", uMap);
213 printInfoDate(f, info.getDict(), "CreationDate",
214 "<meta name=\"CreationDate\" content=\"\">\n");
215 printInfoDate(f, info.getDict(), "LastModifiedDate",
216 "<meta name=\"ModDate\" content=\"\">\n");
219 fputs("</head>\n", f);
220 fputs("<body>\n", f);
228 textOut = new TextOutputDev(textFileName->getCString(),
229 physLayout, rawOrder, htmlMeta);
230 if (textOut->isOk()) {
231 doc->displayPages(textOut, firstPage, lastPage, 72, 0, gFalse);
239 // write end of HTML file
241 if (!textFileName->cmp("-")) {
244 if (!(f = fopen(textFileName->getCString(), "ab"))) {
245 error(-1, "Couldn't open text file '%s'", textFileName->getCString());
250 fputs("</pre>\n", f);
251 fputs("</body>\n", f);
252 fputs("</html>\n", f);
270 // check for memory leaks
271 Object::memCheck(stderr);
277 static void printInfoString(FILE *f, Dict *infoDict, char *key,
278 char *text1, char *text2, UnicodeMap *uMap) {
286 if (infoDict->lookup(key, &obj)->isString()) {
288 s1 = obj.getString();
289 if ((s1->getChar(0) & 0xff) == 0xfe &&
290 (s1->getChar(1) & 0xff) == 0xff) {
297 while (i < obj.getString()->getLength()) {
299 u = ((s1->getChar(i) & 0xff) << 8) |
300 (s1->getChar(i+1) & 0xff);
303 u = s1->getChar(i) & 0xff;
306 n = uMap->mapUnicode(u, buf, sizeof(buf));
307 fwrite(buf, 1, n, f);
314 static void printInfoDate(FILE *f, Dict *infoDict, char *key, char *fmt) {
318 if (infoDict->lookup(key, &obj)->isString()) {
319 s = obj.getString()->getCString();
320 if (s[0] == 'D' && s[1] == ':') {