1 //========================================================================
2 //
3 // pdftotext.cc
4 //
5 // Copyright 1997-2003 Glyph & Cog, LLC
6 //
7 // Modified for Debian by Hamish Moffatt, 22 May 2002.
8 //
9 //========================================================================
10 
11 //========================================================================
12 //
13 // Modified under the Poppler project - http://poppler.freedesktop.org
14 //
15 // All changes made under the Poppler project to this file are licensed
16 // under GPL version 2 or later
17 //
18 // Copyright (C) 2006 Dominic Lachowicz <cinamod@hotmail.com>
19 // Copyright (C) 2007-2008, 2010, 2011, 2017-2021 Albert Astals Cid <aacid@kde.org>
20 // Copyright (C) 2009 Jan Jockusch <jan@jockusch.de>
21 // Copyright (C) 2010, 2013 Hib Eris <hib@hiberis.nl>
22 // Copyright (C) 2010 Kenneth Berland <ken@hero.com>
23 // Copyright (C) 2011 Tom Gleason <tom@buildadam.com>
24 // Copyright (C) 2011 Steven Murdoch <Steven.Murdoch@cl.cam.ac.uk>
25 // Copyright (C) 2013 Yury G. Kudryashov <urkud.urkud@gmail.com>
26 // Copyright (C) 2013 Suzuki Toshiya <mpsuzuki@hiroshima-u.ac.jp>
27 // Copyright (C) 2015 Jeremy Echols <jechols@uoregon.edu>
28 // Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com>
29 // Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>. Work sponsored by the LiMux project of the city of Munich
30 // Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
31 // Copyright (C) 2018 Sanchit Anand <sanxchit@gmail.com>
32 // Copyright (C) 2019 Dan Shea <dan.shea@logical-innovations.com>
33 // Copyright (C) 2019, 2021 Oliver Sander <oliver.sander@tu-dresden.de>
34 // Copyright (C) 2021 William Bader <williambader@hotmail.com>
35 //
36 // To see a description of the changes please see the Changelog file that
37 // came with your tarball or type make ChangeLog if you are building from git
38 //
39 //========================================================================
40 
41 #include "config.h"
42 #include <poppler-config.h>
43 #include <cstdio>
44 #include <cstdlib>
45 #include <cstddef>
46 #include <cstring>
47 #include "parseargs.h"
48 #include "printencodings.h"
49 #include "goo/GooString.h"
50 #include "goo/gmem.h"
51 #include "GlobalParams.h"
52 #include "Object.h"
53 #include "Stream.h"
54 #include "Array.h"
55 #include "Dict.h"
56 #include "XRef.h"
57 #include "Catalog.h"
58 #include "Page.h"
59 #include "PDFDoc.h"
60 #include "PDFDocFactory.h"
61 #include "TextOutputDev.h"
62 #include "CharTypes.h"
63 #include "UnicodeMap.h"
64 #include "PDFDocEncoding.h"
65 #include "Error.h"
66 #include <string>
67 #include <sstream>
68 #include <iomanip>
69 #include "Win32Console.h"
70 
71 static void printInfoString(FILE *f, Dict *infoDict, const char *key, const char *text1, const char *text2, const UnicodeMap *uMap);
72 static void printInfoDate(FILE *f, Dict *infoDict, const char *key, const char *fmt);
73 void printDocBBox(FILE *f, PDFDoc *doc, TextOutputDev *textOut, int first, int last);
74 void printWordBBox(FILE *f, PDFDoc *doc, TextOutputDev *textOut, int first, int last);
75 
76 static int firstPage = 1;
77 static int lastPage = 0;
78 static double resolution = 72.0;
79 static int x = 0;
80 static int y = 0;
81 static int w = 0;
82 static int h = 0;
83 static bool bbox = false;
84 static bool bboxLayout = false;
85 static bool physLayout = false;
86 static bool useCropBox = false;
87 static double fixedPitch = 0;
88 static bool rawOrder = false;
89 static bool discardDiag = false;
90 static bool htmlMeta = false;
91 static char textEncName[128] = "";
92 static char textEOLStr[16] = "";
93 static bool noPageBreaks = false;
94 static char ownerPassword[33] = "\001";
95 static char userPassword[33] = "\001";
96 static bool quiet = false;
97 static bool printVersion = false;
98 static bool printHelp = false;
99 static bool printEnc = false;
100 
101 static const ArgDesc argDesc[] = { { "-f", argInt, &firstPage, 0, "first page to convert" },
102                                    { "-l", argInt, &lastPage, 0, "last page to convert" },
103                                    { "-r", argFP, &resolution, 0, "resolution, in DPI (default is 72)" },
104                                    { "-x", argInt, &x, 0, "x-coordinate of the crop area top left corner" },
105                                    { "-y", argInt, &y, 0, "y-coordinate of the crop area top left corner" },
106                                    { "-W", argInt, &w, 0, "width of crop area in pixels (default is 0)" },
107                                    { "-H", argInt, &h, 0, "height of crop area in pixels (default is 0)" },
108                                    { "-layout", argFlag, &physLayout, 0, "maintain original physical layout" },
109                                    { "-fixed", argFP, &fixedPitch, 0, "assume fixed-pitch (or tabular) text" },
110                                    { "-raw", argFlag, &rawOrder, 0, "keep strings in content stream order" },
111                                    { "-nodiag", argFlag, &discardDiag, 0, "discard diagonal text" },
112                                    { "-htmlmeta", argFlag, &htmlMeta, 0, "generate a simple HTML file, including the meta information" },
113                                    { "-enc", argString, textEncName, sizeof(textEncName), "output text encoding name" },
114                                    { "-listenc", argFlag, &printEnc, 0, "list available encodings" },
115                                    { "-eol", argString, textEOLStr, sizeof(textEOLStr), "output end-of-line convention (unix, dos, or mac)" },
116                                    { "-nopgbrk", argFlag, &noPageBreaks, 0, "don't insert page breaks between pages" },
117                                    { "-bbox", argFlag, &bbox, 0, "output bounding box for each word and page size to html.  Sets -htmlmeta" },
118                                    { "-bbox-layout", argFlag, &bboxLayout, 0, "like -bbox but with extra layout bounding box data.  Sets -htmlmeta" },
119                                    { "-cropbox", argFlag, &useCropBox, 0, "use the crop box rather than media box" },
120                                    { "-opw", argString, ownerPassword, sizeof(ownerPassword), "owner password (for encrypted files)" },
121                                    { "-upw", argString, userPassword, sizeof(userPassword), "user password (for encrypted files)" },
122                                    { "-q", argFlag, &quiet, 0, "don't print any messages or errors" },
123                                    { "-v", argFlag, &printVersion, 0, "print copyright and version info" },
124                                    { "-h", argFlag, &printHelp, 0, "print usage information" },
125                                    { "-help", argFlag, &printHelp, 0, "print usage information" },
126                                    { "--help", argFlag, &printHelp, 0, "print usage information" },
127                                    { "-?", argFlag, &printHelp, 0, "print usage information" },
128                                    {} };
129 
myStringReplace(const std::string & inString,const std::string & oldToken,const std::string & newToken)130 static std::string myStringReplace(const std::string &inString, const std::string &oldToken, const std::string &newToken)
131 {
132     std::string result = inString;
133     size_t foundLoc;
134     int advance = 0;
135     do {
136         foundLoc = result.find(oldToken, advance);
137         if (foundLoc != std::string::npos) {
138             result.replace(foundLoc, oldToken.length(), newToken);
139             advance = foundLoc + newToken.length();
140         }
141     } while (foundLoc != std::string::npos);
142     return result;
143 }
144 
myXmlTokenReplace(const char * inString)145 static std::string myXmlTokenReplace(const char *inString)
146 {
147     std::string myString(inString);
148     myString = myStringReplace(myString, "&", "&amp;");
149     myString = myStringReplace(myString, "'", "&apos;");
150     myString = myStringReplace(myString, "\"", "&quot;");
151     myString = myStringReplace(myString, "<", "&lt;");
152     myString = myStringReplace(myString, ">", "&gt;");
153     return myString;
154 }
155 
main(int argc,char * argv[])156 int main(int argc, char *argv[])
157 {
158     std::unique_ptr<PDFDoc> doc;
159     GooString *fileName;
160     GooString *textFileName;
161     GooString *ownerPW, *userPW;
162     TextOutputDev *textOut;
163     FILE *f;
164     const UnicodeMap *uMap;
165     Object info;
166     bool ok;
167     int exitCode;
168     EndOfLineKind textEOL = TextOutputDev::defaultEndOfLine();
169 
170     Win32Console win32Console(&argc, &argv);
171     exitCode = 99;
172 
173     // parse args
174     ok = parseArgs(argDesc, &argc, argv);
175     if (bboxLayout) {
176         bbox = true;
177     }
178     if (bbox) {
179         htmlMeta = true;
180     }
181     if (!ok || (argc < 2 && !printEnc) || argc > 3 || printVersion || printHelp) {
182         fprintf(stderr, "pdftotext version %s\n", PACKAGE_VERSION);
183         fprintf(stderr, "%s\n", popplerCopyright);
184         fprintf(stderr, "%s\n", xpdfCopyright);
185         if (!printVersion) {
186             printUsage("pdftotext", "<PDF-file> [<text-file>]", argDesc);
187         }
188         if (printVersion || printHelp)
189             exitCode = 0;
190         goto err0;
191     }
192 
193     // read config file
194     globalParams = std::make_unique<GlobalParams>();
195 
196     if (printEnc) {
197         printEncodings();
198         exitCode = 0;
199         goto err0;
200     }
201 
202     fileName = new GooString(argv[1]);
203     if (fixedPitch) {
204         physLayout = true;
205     }
206 
207     if (textEncName[0]) {
208         globalParams->setTextEncoding(textEncName);
209     }
210     if (textEOLStr[0]) {
211         if (!strcmp(textEOLStr, "unix")) {
212             textEOL = eolUnix;
213         } else if (!strcmp(textEOLStr, "dos")) {
214             textEOL = eolDOS;
215         } else if (!strcmp(textEOLStr, "mac")) {
216             textEOL = eolMac;
217         } else {
218             fprintf(stderr, "Bad '-eol' value on command line\n");
219         }
220     }
221     if (quiet) {
222         globalParams->setErrQuiet(quiet);
223     }
224 
225     // get mapping to output encoding
226     if (!(uMap = globalParams->getTextEncoding())) {
227         error(errCommandLine, -1, "Couldn't get text encoding");
228         delete fileName;
229         goto err1;
230     }
231 
232     // open PDF file
233     if (ownerPassword[0] != '\001') {
234         ownerPW = new GooString(ownerPassword);
235     } else {
236         ownerPW = nullptr;
237     }
238     if (userPassword[0] != '\001') {
239         userPW = new GooString(userPassword);
240     } else {
241         userPW = nullptr;
242     }
243 
244     if (fileName->cmp("-") == 0) {
245         delete fileName;
246         fileName = new GooString("fd://0");
247     }
248 
249     doc = PDFDocFactory().createPDFDoc(*fileName, ownerPW, userPW);
250 
251     if (userPW) {
252         delete userPW;
253     }
254     if (ownerPW) {
255         delete ownerPW;
256     }
257     if (!doc->isOk()) {
258         exitCode = 1;
259         goto err2;
260     }
261 
262 #ifdef ENFORCE_PERMISSIONS
263     // check for copy permission
264     if (!doc->okToCopy()) {
265         error(errNotAllowed, -1, "Copying of text from this document is not allowed.");
266         exitCode = 3;
267         goto err2;
268     }
269 #endif
270 
271     // construct text file name
272     if (argc == 3) {
273         textFileName = new GooString(argv[2]);
274     } else if (fileName->cmp("fd://0") == 0) {
275         error(errCommandLine, -1, "You have to provide an output filename when reading from stdin.");
276         goto err2;
277     } else {
278         const char *p = fileName->c_str() + fileName->getLength() - 4;
279         if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF")) {
280             textFileName = new GooString(fileName->c_str(), fileName->getLength() - 4);
281         } else {
282             textFileName = fileName->copy();
283         }
284         textFileName->append(htmlMeta ? ".html" : ".txt");
285     }
286 
287     // get page range
288     if (firstPage < 1) {
289         firstPage = 1;
290     }
291     if (lastPage < 1 || lastPage > doc->getNumPages()) {
292         lastPage = doc->getNumPages();
293     }
294     if (lastPage < firstPage) {
295         error(errCommandLine, -1, "Wrong page range given: the first page ({0:d}) can not be after the last page ({1:d}).", firstPage, lastPage);
296         goto err3;
297     }
298 
299     // write HTML header
300     if (htmlMeta) {
301         if (!textFileName->cmp("-")) {
302             f = stdout;
303         } else {
304             if (!(f = fopen(textFileName->c_str(), "wb"))) {
305                 error(errIO, -1, "Couldn't open text file '{0:t}'", textFileName);
306                 exitCode = 2;
307                 goto err3;
308             }
309         }
310         fputs("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">", f);
311         fputs("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n", f);
312         fputs("<head>\n", f);
313         info = doc->getDocInfo();
314         if (info.isDict()) {
315             Object obj = info.getDict()->lookup("Title");
316             if (obj.isString()) {
317                 printInfoString(f, info.getDict(), "Title", "<title>", "</title>\n", uMap);
318             } else {
319                 fputs("<title></title>\n", f);
320             }
321             printInfoString(f, info.getDict(), "Subject", "<meta name=\"Subject\" content=\"", "\"/>\n", uMap);
322             printInfoString(f, info.getDict(), "Keywords", "<meta name=\"Keywords\" content=\"", "\"/>\n", uMap);
323             printInfoString(f, info.getDict(), "Author", "<meta name=\"Author\" content=\"", "\"/>\n", uMap);
324             printInfoString(f, info.getDict(), "Creator", "<meta name=\"Creator\" content=\"", "\"/>\n", uMap);
325             printInfoString(f, info.getDict(), "Producer", "<meta name=\"Producer\" content=\"", "\"/>\n", uMap);
326             printInfoDate(f, info.getDict(), "CreationDate", "<meta name=\"CreationDate\" content=\"\"/>\n");
327             printInfoDate(f, info.getDict(), "LastModifiedDate", "<meta name=\"ModDate\" content=\"\"/>\n");
328         }
329         fputs("</head>\n", f);
330         fputs("<body>\n", f);
331         if (!bbox) {
332             fputs("<pre>\n", f);
333             if (f != stdout) {
334                 fclose(f);
335             }
336         }
337     }
338 
339     // write text file
340     if (htmlMeta && bbox) { // htmlMeta && is superfluous but makes gcc happier
341         textOut = new TextOutputDev(nullptr, physLayout, fixedPitch, rawOrder, htmlMeta, discardDiag);
342 
343         if (textOut->isOk()) {
344             textOut->setTextEOL(textEOL);
345             if (noPageBreaks) {
346                 textOut->setTextPageBreaks(false);
347             }
348             if (bboxLayout) {
349                 printDocBBox(f, doc.get(), textOut, firstPage, lastPage);
350             } else {
351                 printWordBBox(f, doc.get(), textOut, firstPage, lastPage);
352             }
353         }
354         if (f != stdout) {
355             fclose(f);
356         }
357     } else {
358         textOut = new TextOutputDev(textFileName->c_str(), physLayout, fixedPitch, rawOrder, htmlMeta, discardDiag);
359         if (textOut->isOk()) {
360             textOut->setTextEOL(textEOL);
361             if (noPageBreaks) {
362                 textOut->setTextPageBreaks(false);
363             }
364             if ((w == 0) && (h == 0) && (x == 0) && (y == 0)) {
365                 doc->displayPages(textOut, firstPage, lastPage, resolution, resolution, 0, true, false, false);
366             } else {
367 
368                 for (int page = firstPage; page <= lastPage; ++page) {
369                     doc->displayPageSlice(textOut, page, resolution, resolution, 0, true, false, false, x, y, w, h);
370                 }
371             }
372 
373         } else {
374             delete textOut;
375             exitCode = 2;
376             goto err3;
377         }
378     }
379     delete textOut;
380 
381     // write end of HTML file
382     if (htmlMeta) {
383         if (!textFileName->cmp("-")) {
384             f = stdout;
385         } else {
386             if (!(f = fopen(textFileName->c_str(), "ab"))) {
387                 error(errIO, -1, "Couldn't open text file '{0:t}'", textFileName);
388                 exitCode = 2;
389                 goto err3;
390             }
391         }
392         if (!bbox)
393             fputs("</pre>\n", f);
394         fputs("</body>\n", f);
395         fputs("</html>\n", f);
396         if (f != stdout) {
397             fclose(f);
398         }
399     }
400 
401     exitCode = 0;
402 
403     // clean up
404 err3:
405     delete textFileName;
406 err2:
407     delete fileName;
408 err1:
409 err0:
410 
411     return exitCode;
412 }
413 
printInfoString(FILE * f,Dict * infoDict,const char * key,const char * text1,const char * text2,const UnicodeMap * uMap)414 static void printInfoString(FILE *f, Dict *infoDict, const char *key, const char *text1, const char *text2, const UnicodeMap *uMap)
415 {
416     const GooString *s1;
417     bool isUnicode;
418     Unicode u;
419     char buf[9];
420     int i, n;
421 
422     Object obj = infoDict->lookup(key);
423     if (obj.isString()) {
424         fputs(text1, f);
425         s1 = obj.getString();
426         if ((s1->getChar(0) & 0xff) == 0xfe && (s1->getChar(1) & 0xff) == 0xff) {
427             isUnicode = true;
428             i = 2;
429         } else {
430             isUnicode = false;
431             i = 0;
432         }
433         while (i < obj.getString()->getLength()) {
434             if (isUnicode) {
435                 u = ((s1->getChar(i) & 0xff) << 8) | (s1->getChar(i + 1) & 0xff);
436                 i += 2;
437             } else {
438                 u = pdfDocEncoding[s1->getChar(i) & 0xff];
439                 ++i;
440             }
441             n = uMap->mapUnicode(u, buf, sizeof(buf));
442             buf[n] = '\0';
443             const std::string myString = myXmlTokenReplace(buf);
444             fputs(myString.c_str(), f);
445         }
446         fputs(text2, f);
447     }
448 }
449 
printInfoDate(FILE * f,Dict * infoDict,const char * key,const char * fmt)450 static void printInfoDate(FILE *f, Dict *infoDict, const char *key, const char *fmt)
451 {
452     Object obj = infoDict->lookup(key);
453     if (obj.isString()) {
454         const char *s = obj.getString()->c_str();
455         if (s[0] == 'D' && s[1] == ':') {
456             s += 2;
457         }
458         fprintf(f, fmt, s);
459     }
460 }
461 
printLine(FILE * f,const TextLine * line)462 static void printLine(FILE *f, const TextLine *line)
463 {
464     double xMin, yMin, xMax, yMax;
465     double lineXMin = 0, lineYMin = 0, lineXMax = 0, lineYMax = 0;
466     const TextWord *word;
467     std::stringstream wordXML;
468     wordXML << std::fixed << std::setprecision(6);
469 
470     for (word = line->getWords(); word; word = word->getNext()) {
471         word->getBBox(&xMin, &yMin, &xMax, &yMax);
472 
473         if (lineXMin == 0 || lineXMin > xMin)
474             lineXMin = xMin;
475         if (lineYMin == 0 || lineYMin > yMin)
476             lineYMin = yMin;
477         if (lineXMax < xMax)
478             lineXMax = xMax;
479         if (lineYMax < yMax)
480             lineYMax = yMax;
481 
482         GooString *wordText = word->getText();
483         const std::string myString = myXmlTokenReplace(wordText->c_str());
484         wordXML << "          <word xMin=\"" << xMin << "\" yMin=\"" << yMin << "\" xMax=\"" << xMax << "\" yMax=\"" << yMax << "\">" << myString << "</word>\n";
485         delete wordText;
486     }
487     fprintf(f, "        <line xMin=\"%f\" yMin=\"%f\" xMax=\"%f\" yMax=\"%f\">\n", lineXMin, lineYMin, lineXMax, lineYMax);
488     fputs(wordXML.str().c_str(), f);
489     fputs("        </line>\n", f);
490 }
491 
printDocBBox(FILE * f,PDFDoc * doc,TextOutputDev * textOut,int first,int last)492 void printDocBBox(FILE *f, PDFDoc *doc, TextOutputDev *textOut, int first, int last)
493 {
494     double xMin, yMin, xMax, yMax;
495     const TextFlow *flow;
496     const TextBlock *blk;
497     const TextLine *line;
498 
499     fprintf(f, "<doc>\n");
500     for (int page = first; page <= last; ++page) {
501         const double wid = useCropBox ? doc->getPageCropWidth(page) : doc->getPageMediaWidth(page);
502         const double hgt = useCropBox ? doc->getPageCropHeight(page) : doc->getPageMediaHeight(page);
503         fprintf(f, "  <page width=\"%f\" height=\"%f\">\n", wid, hgt);
504         doc->displayPage(textOut, page, resolution, resolution, 0, !useCropBox, useCropBox, false);
505         for (flow = textOut->getFlows(); flow; flow = flow->getNext()) {
506             fprintf(f, "    <flow>\n");
507             for (blk = flow->getBlocks(); blk; blk = blk->getNext()) {
508                 blk->getBBox(&xMin, &yMin, &xMax, &yMax);
509                 fprintf(f, "      <block xMin=\"%f\" yMin=\"%f\" xMax=\"%f\" yMax=\"%f\">\n", xMin, yMin, xMax, yMax);
510                 for (line = blk->getLines(); line; line = line->getNext()) {
511                     printLine(f, line);
512                 }
513                 fprintf(f, "      </block>\n");
514             }
515             fprintf(f, "    </flow>\n");
516         }
517         fprintf(f, "  </page>\n");
518     }
519     fprintf(f, "</doc>\n");
520 }
521 
printWordBBox(FILE * f,PDFDoc * doc,TextOutputDev * textOut,int first,int last)522 void printWordBBox(FILE *f, PDFDoc *doc, TextOutputDev *textOut, int first, int last)
523 {
524     fprintf(f, "<doc>\n");
525     for (int page = first; page <= last; ++page) {
526         double wid = useCropBox ? doc->getPageCropWidth(page) : doc->getPageMediaWidth(page);
527         double hgt = useCropBox ? doc->getPageCropHeight(page) : doc->getPageMediaHeight(page);
528         fprintf(f, "  <page width=\"%f\" height=\"%f\">\n", wid, hgt);
529         doc->displayPage(textOut, page, resolution, resolution, 0, !useCropBox, useCropBox, false);
530         std::unique_ptr<TextWordList> wordlist = textOut->makeWordList();
531         const int word_length = wordlist != nullptr ? wordlist->getLength() : 0;
532         TextWord *word;
533         double xMinA, yMinA, xMaxA, yMaxA;
534         if (word_length == 0)
535             fprintf(stderr, "no word list\n");
536 
537         for (int i = 0; i < word_length; ++i) {
538             word = wordlist->get(i);
539             word->getBBox(&xMinA, &yMinA, &xMaxA, &yMaxA);
540             const std::string myString = myXmlTokenReplace(word->getText()->c_str());
541             fprintf(f, "    <word xMin=\"%f\" yMin=\"%f\" xMax=\"%f\" yMax=\"%f\">%s</word>\n", xMinA, yMinA, xMaxA, yMaxA, myString.c_str());
542         }
543         fprintf(f, "  </page>\n");
544     }
545     fprintf(f, "</doc>\n");
546 }
547