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 GBool noPageBreaks = gFalse;
44 static char ownerPassword[33] = "\001";
45 static char userPassword[33] = "\001";
46 static GBool quiet = gFalse;
47 static char cfgFileName[256] = "";
48 static GBool printVersion = gFalse;
49 static GBool printHelp = gFalse;
51 static ArgDesc argDesc[] = {
52 {"-f", argInt, &firstPage, 0,
53 "first page to convert"},
54 {"-l", argInt, &lastPage, 0,
55 "last page to convert"},
56 {"-layout", argFlag, &physLayout, 0,
57 "maintain original physical layout"},
58 {"-raw", argFlag, &rawOrder, 0,
59 "keep strings in content stream order"},
60 {"-htmlmeta", argFlag, &htmlMeta, 0,
61 "generate a simple HTML file, including the meta information"},
62 {"-enc", argString, textEncName, sizeof(textEncName),
63 "output text encoding name"},
64 {"-eol", argString, textEOL, sizeof(textEOL),
65 "output end-of-line convention (unix, dos, or mac)"},
66 {"-nopgbrk", argFlag, &noPageBreaks, 0,
67 "don't insert page breaks between pages"},
68 {"-opw", argString, ownerPassword, sizeof(ownerPassword),
69 "owner password (for encrypted files)"},
70 {"-upw", argString, userPassword, sizeof(userPassword),
71 "user password (for encrypted files)"},
72 {"-q", argFlag, &quiet, 0,
73 "don't print any messages or errors"},
74 {"-cfg", argString, cfgFileName, sizeof(cfgFileName),
75 "configuration file to use in place of .xpdfrc"},
76 {"-v", argFlag, &printVersion, 0,
77 "print copyright and version info"},
78 {"-h", argFlag, &printHelp, 0,
79 "print usage information"},
80 {"-help", argFlag, &printHelp, 0,
81 "print usage information"},
82 {"--help", argFlag, &printHelp, 0,
83 "print usage information"},
84 {"-?", argFlag, &printHelp, 0,
85 "print usage information"},
89 int main(int argc, char *argv[]) {
92 GString *textFileName;
93 GString *ownerPW, *userPW;
94 TextOutputDev *textOut;
105 ok = parseArgs(argDesc, &argc, argv);
106 if (!ok || argc < 2 || argc > 3 || printVersion || printHelp) {
107 fprintf(stderr, "pdftotext version %s\n", xpdfVersion);
108 fprintf(stderr, "%s\n", xpdfCopyright);
110 printUsage("pdftotext", "<PDF-file> [<text-file>]", argDesc);
114 fileName = new GString(argv[1]);
117 globalParams = new GlobalParams(cfgFileName);
118 if (textEncName[0]) {
119 globalParams->setTextEncoding(textEncName);
122 if (!globalParams->setTextEOL(textEOL)) {
123 fprintf(stderr, "Bad '-eol' value on command line\n");
127 globalParams->setTextPageBreaks(gFalse);
130 globalParams->setErrQuiet(quiet);
133 // get mapping to output encoding
134 if (!(uMap = globalParams->getTextEncoding())) {
135 error(-1, "Couldn't get text encoding");
141 if (ownerPassword[0] != '\001') {
142 ownerPW = new GString(ownerPassword);
146 if (userPassword[0] != '\001') {
147 userPW = new GString(userPassword);
151 doc = new PDFDoc(fileName, ownerPW, userPW);
163 // check for copy permission
164 if (!doc->okToCopy()) {
165 error(-1, "Copying of text from this document is not allowed.");
170 // construct text file name
172 textFileName = new GString(argv[2]);
174 p = fileName->getCString() + fileName->getLength() - 4;
175 if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF")) {
176 textFileName = new GString(fileName->getCString(),
177 fileName->getLength() - 4);
179 textFileName = fileName->copy();
181 textFileName->append(htmlMeta ? ".html" : ".txt");
188 if (lastPage < 1 || lastPage > doc->getNumPages()) {
189 lastPage = doc->getNumPages();
194 if (!textFileName->cmp("-")) {
197 if (!(f = fopen(textFileName->getCString(), "wb"))) {
198 error(-1, "Couldn't open text file '%s'", textFileName->getCString());
203 fputs("<html>\n", f);
204 fputs("<head>\n", f);
205 doc->getDocInfo(&info);
207 printInfoString(f, info.getDict(), "Title", "<title>", "</title>\n",
209 printInfoString(f, info.getDict(), "Subject",
210 "<meta name=\"Subject\" content=\"", "\">\n", uMap);
211 printInfoString(f, info.getDict(), "Keywords",
212 "<meta name=\"Keywords\" content=\"", "\">\n", uMap);
213 printInfoString(f, info.getDict(), "Author",
214 "<meta name=\"Author\" content=\"", "\">\n", uMap);
215 printInfoString(f, info.getDict(), "Creator",
216 "<meta name=\"Creator\" content=\"", "\">\n", uMap);
217 printInfoString(f, info.getDict(), "Producer",
218 "<meta name=\"Producer\" content=\"", "\">\n", uMap);
219 printInfoDate(f, info.getDict(), "CreationDate",
220 "<meta name=\"CreationDate\" content=\"\">\n");
221 printInfoDate(f, info.getDict(), "LastModifiedDate",
222 "<meta name=\"ModDate\" content=\"\">\n");
225 fputs("</head>\n", f);
226 fputs("<body>\n", f);
234 textOut = new TextOutputDev(textFileName->getCString(),
235 physLayout, rawOrder, htmlMeta);
236 if (textOut->isOk()) {
237 doc->displayPages(textOut, firstPage, lastPage, 72, 72, 0, gTrue, gFalse);
245 // write end of HTML file
247 if (!textFileName->cmp("-")) {
250 if (!(f = fopen(textFileName->getCString(), "ab"))) {
251 error(-1, "Couldn't open text file '%s'", textFileName->getCString());
256 fputs("</pre>\n", f);
257 fputs("</body>\n", f);
258 fputs("</html>\n", f);
276 // check for memory leaks
277 Object::memCheck(stderr);
283 static void printInfoString(FILE *f, Dict *infoDict, char *key,
284 char *text1, char *text2, UnicodeMap *uMap) {
292 if (infoDict->lookup(key, &obj)->isString()) {
294 s1 = obj.getString();
295 if ((s1->getChar(0) & 0xff) == 0xfe &&
296 (s1->getChar(1) & 0xff) == 0xff) {
303 while (i < obj.getString()->getLength()) {
305 u = ((s1->getChar(i) & 0xff) << 8) |
306 (s1->getChar(i+1) & 0xff);
309 u = s1->getChar(i) & 0xff;
312 n = uMap->mapUnicode(u, buf, sizeof(buf));
313 fwrite(buf, 1, n, f);
320 static void printInfoDate(FILE *f, Dict *infoDict, char *key, char *fmt) {
324 if (infoDict->lookup(key, &obj)->isString()) {
325 s = obj.getString()->getCString();
326 if (s[0] == 'D' && s[1] == ':') {