]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/pdftotext.cc
change (disabled) font embedding code
[evince.git] / pdf / xpdf / pdftotext.cc
1 //========================================================================
2 //
3 // pdftotext.cc
4 //
5 // Copyright 1997-2002 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #include <aconf.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stddef.h>
13 #include <string.h>
14 #include "parseargs.h"
15 #include "GString.h"
16 #include "gmem.h"
17 #include "GlobalParams.h"
18 #include "Object.h"
19 #include "Stream.h"
20 #include "Array.h"
21 #include "Dict.h"
22 #include "XRef.h"
23 #include "Catalog.h"
24 #include "Page.h"
25 #include "PDFDoc.h"
26 #include "TextOutputDev.h"
27 #include "CharTypes.h"
28 #include "UnicodeMap.h"
29 #include "Error.h"
30 #include "config.h"
31
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);
35
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;
48
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"},
80   {NULL}
81 };
82
83 int main(int argc, char *argv[]) {
84   PDFDoc *doc;
85   GString *fileName;
86   GString *textFileName;
87   GString *ownerPW, *userPW;
88   TextOutputDev *textOut;
89   FILE *f;
90   UnicodeMap *uMap;
91   Object info;
92   GBool ok;
93   char *p;
94
95   // parse args
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);
100     if (!printVersion) {
101       printUsage("pdftotext", "<PDF-file> [<text-file>]", argDesc);
102     }
103     exit(1);
104   }
105   fileName = new GString(argv[1]);
106
107   // read config file
108   globalParams = new GlobalParams(cfgFileName);
109   if (textEncName[0]) {
110     globalParams->setTextEncoding(textEncName);
111   }
112   if (textEOL[0]) {
113     if (!globalParams->setTextEOL(textEOL)) {
114       fprintf(stderr, "Bad '-eol' value on command line\n");
115     }
116   }
117   if (quiet) {
118     globalParams->setErrQuiet(quiet);
119   }
120
121   // get mapping to output encoding
122   if (!(uMap = globalParams->getTextEncoding())) {
123     error(-1, "Couldn't get text encoding");
124     delete fileName;
125     goto err1;
126   }
127
128   // open PDF file
129   if (ownerPassword[0]) {
130     ownerPW = new GString(ownerPassword);
131   } else {
132     ownerPW = NULL;
133   }
134   if (userPassword[0]) {
135     userPW = new GString(userPassword);
136   } else {
137     userPW = NULL;
138   }
139   doc = new PDFDoc(fileName, ownerPW, userPW);
140   if (userPW) {
141     delete userPW;
142   }
143   if (ownerPW) {
144     delete ownerPW;
145   }
146   if (!doc->isOk()) {
147     goto err2;
148   }
149
150   // check for copy permission
151   if (!doc->okToCopy()) {
152     error(-1, "Copying of text from this document is not allowed.");
153     goto err2;
154   }
155
156   // construct text file name
157   if (argc == 3) {
158     textFileName = new GString(argv[2]);
159   } else {
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);
164     } else {
165       textFileName = fileName->copy();
166     }
167     textFileName->append(htmlMeta ? ".html" : ".txt");
168   }
169
170   // get page range
171   if (firstPage < 1) {
172     firstPage = 1;
173   }
174   if (lastPage < 1 || lastPage > doc->getNumPages()) {
175     lastPage = doc->getNumPages();
176   }
177
178   // write HTML header
179   if (htmlMeta) {
180     if (!textFileName->cmp("-")) {
181       f = stdout;
182     } else {
183       if (!(f = fopen(textFileName->getCString(), "wb"))) {
184         error(-1, "Couldn't open text file '%s'", textFileName->getCString());
185         goto err3;
186       }
187     }
188     fputs("<html>\n", f);
189     fputs("<head>\n", f);
190     doc->getDocInfo(&info);
191     if (info.isDict()) {
192       printInfoString(f, info.getDict(), "Title", "<title>", "</title>\n",
193                       uMap);
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");
208     }
209     info.free();
210     fputs("</head>\n", f);
211     fputs("<body>\n", f);
212     fputs("<pre>\n", f);
213     if (f != stdout) {
214       fclose(f);
215     }
216   }
217
218   // write text file
219   textOut = new TextOutputDev(textFileName->getCString(), rawOrder, htmlMeta);
220   if (textOut->isOk()) {
221     doc->displayPages(textOut, firstPage, lastPage, 72, 0, gFalse);
222   }
223   delete textOut;
224
225   // write end of HTML file
226   if (htmlMeta) {
227     if (!textFileName->cmp("-")) {
228       f = stdout;
229     } else {
230       if (!(f = fopen(textFileName->getCString(), "ab"))) {
231         error(-1, "Couldn't open text file '%s'", textFileName->getCString());
232         goto err3;
233       }
234     }
235     fputs("</pre>\n", f);
236     fputs("</body>\n", f);
237     fputs("</html>\n", f);
238     if (f != stdout) {
239       fclose(f);
240     }
241   }
242
243   // clean up
244  err3:
245   delete textFileName;
246  err2:
247   delete doc;
248   uMap->decRefCnt();
249  err1:
250   delete globalParams;
251
252   // check for memory leaks
253   Object::memCheck(stderr);
254   gMemReport(stderr);
255
256   return 0;
257 }
258
259 static void printInfoString(FILE *f, Dict *infoDict, char *key,
260                             char *text1, char *text2, UnicodeMap *uMap) {
261   Object obj;
262   GString *s1;
263   GBool isUnicode;
264   Unicode u;
265   char buf[8];
266   int i, n;
267
268   if (infoDict->lookup(key, &obj)->isString()) {
269     fputs(text1, f);
270     s1 = obj.getString();
271     if ((s1->getChar(0) & 0xff) == 0xfe &&
272         (s1->getChar(1) & 0xff) == 0xff) {
273       isUnicode = gTrue;
274       i = 2;
275     } else {
276       isUnicode = gFalse;
277       i = 0;
278     }
279     while (i < obj.getString()->getLength()) {
280       if (isUnicode) {
281         u = ((s1->getChar(i) & 0xff) << 8) |
282             (s1->getChar(i+1) & 0xff);
283         i += 2;
284       } else {
285         u = s1->getChar(i) & 0xff;
286         ++i;
287       }
288       n = uMap->mapUnicode(u, buf, sizeof(buf));
289       fwrite(buf, 1, n, f);
290     }
291     fputs(text2, f);
292   }
293   obj.free();
294 }
295
296 static void printInfoDate(FILE *f, Dict *infoDict, char *key, char *fmt) {
297   Object obj;
298   char *s;
299
300   if (infoDict->lookup(key, &obj)->isString()) {
301     s = obj.getString()->getCString();
302     if (s[0] == 'D' && s[1] == ':') {
303       s += 2;
304     }
305     fprintf(f, fmt, s);
306   }
307   obj.free();
308 }