1 //========================================================================
2 //
3 // pdfimages.cc
4 //
5 // Copyright 1998-2003 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 "ImageOutputDev.h"
27 #include "Error.h"
28 #include "config.h"
29 
30 static int firstPage = 1;
31 static int lastPage = 0;
32 static GBool dumpJPEG = gFalse;
33 static char ownerPassword[33] = "\001";
34 static char userPassword[33] = "\001";
35 static GBool quiet = gFalse;
36 static char cfgFileName[256] = "";
37 static GBool printVersion = gFalse;
38 static GBool printHelp = gFalse;
39 
40 static ArgDesc argDesc[] = {
41   {"-f",      argInt,      &firstPage,     0,
42    "first page to convert"},
43   {"-l",      argInt,      &lastPage,      0,
44    "last page to convert"},
45   {"-j",      argFlag,     &dumpJPEG,      0,
46    "write JPEG images as JPEG files"},
47   {"-opw",    argString,   ownerPassword,  sizeof(ownerPassword),
48    "owner password (for encrypted files)"},
49   {"-upw",    argString,   userPassword,   sizeof(userPassword),
50    "user password (for encrypted files)"},
51   {"-q",      argFlag,     &quiet,         0,
52    "don't print any messages or errors"},
53   {"-cfg",        argString,      cfgFileName,    sizeof(cfgFileName),
54    "configuration file to use in place of .xpdfrc"},
55   {"-v",      argFlag,     &printVersion,  0,
56    "print copyright and version info"},
57   {"-h",      argFlag,     &printHelp,     0,
58    "print usage information"},
59   {"-help",   argFlag,     &printHelp,     0,
60    "print usage information"},
61   {"--help",  argFlag,     &printHelp,     0,
62    "print usage information"},
63   {"-?",      argFlag,     &printHelp,     0,
64    "print usage information"},
65   {NULL}
66 };
67 
main(int argc,char * argv[])68 int main(int argc, char *argv[]) {
69   PDFDoc *doc;
70   GString *fileName;
71   char *imgRoot;
72   GString *ownerPW, *userPW;
73   ImageOutputDev *imgOut;
74   GBool ok;
75   int exitCode;
76 
77   exitCode = 99;
78 
79 #ifdef _MSC_VER
80   (void)kpse_set_program_name(argv[0], NULL);
81 #endif
82   // parse args
83   ok = parseArgs(argDesc, &argc, argv);
84   if (!ok || argc != 3 || printVersion || printHelp) {
85     fprintf(stderr, "pdfimages version %s\n", xpdfVersion);
86     fprintf(stderr, "%s\n", xpdfCopyright);
87     if (!printVersion) {
88       printUsage("pdfimages", "<PDF-file> <image-root>", argDesc);
89     }
90     goto err0;
91   }
92   fileName = new GString(argv[1]);
93   imgRoot = argv[2];
94 
95   // read config file
96   globalParams = new GlobalParams(cfgFileName);
97   if (quiet) {
98     globalParams->setErrQuiet(quiet);
99   }
100 
101   // open PDF file
102   if (ownerPassword[0] != '\001') {
103     ownerPW = new GString(ownerPassword);
104   } else {
105     ownerPW = NULL;
106   }
107   if (userPassword[0] != '\001') {
108     userPW = new GString(userPassword);
109   } else {
110     userPW = NULL;
111   }
112   doc = new PDFDoc(fileName, ownerPW, userPW);
113   if (userPW) {
114     delete userPW;
115   }
116   if (ownerPW) {
117     delete ownerPW;
118   }
119   if (!doc->isOk()) {
120     exitCode = 1;
121     goto err1;
122   }
123 
124   // check for copy permission
125   if (!doc->okToCopy()) {
126     error(errNotAllowed, -1,
127 	  "Copying of images from this document is not allowed.");
128     exitCode = 3;
129     goto err1;
130   }
131 
132   // get page range
133   if (firstPage < 1)
134     firstPage = 1;
135   if (lastPage < 1 || lastPage > doc->getNumPages())
136     lastPage = doc->getNumPages();
137 
138   // write image files
139   imgOut = new ImageOutputDev(imgRoot, dumpJPEG);
140   if (imgOut->isOk()) {
141     doc->displayPages(imgOut, firstPage, lastPage, 72, 72, 0,
142 		      gFalse, gTrue, gFalse);
143   }
144   delete imgOut;
145 
146   exitCode = 0;
147 
148   // clean up
149  err1:
150   delete doc;
151   delete globalParams;
152  err0:
153 
154   // check for memory leaks
155   Object::memCheck(stderr);
156   gMemReport(stderr);
157 
158   return exitCode;
159 }
160