1 //========================================================================
2 //
3 // pdfimages.cc
4 //
5 // Copyright 1998-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) 2007-2008, 2010 Albert Astals Cid <aacid@kde.org>
19 // Copyright (C) 2010 Hib Eris <hib@hiberis.nl>
20 // Copyright (C) 2010 Jakob Voss <jakob.voss@gbv.de>
21 //
22 // To see a description of the changes please see the Changelog file that
23 // came with your tarball or type make ChangeLog if you are building from git
24 //
25 //========================================================================
26
27 #include "config.h"
28 #include <poppler-config.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stddef.h>
32 #include <string.h>
33 #include "parseargs.h"
34 #include "goo/GooString.h"
35 #include "goo/gmem.h"
36 #include "GlobalParams.h"
37 #include "Object.h"
38 #include "Stream.h"
39 #include "Array.h"
40 #include "Dict.h"
41 #include "XRef.h"
42 #include "Catalog.h"
43 #include "Page.h"
44 #include "PDFDoc.h"
45 #include "PDFDocFactory.h"
46 #include "ImageOutputDev.h"
47 #include "Error.h"
48
49 static int firstPage = 1;
50 static int lastPage = 0;
51 static GBool dumpJPEG = gFalse;
52 static GBool pageNames = gFalse;
53 static char ownerPassword[33] = "\001";
54 static char userPassword[33] = "\001";
55 static GBool quiet = gFalse;
56 static GBool printVersion = gFalse;
57 static GBool printHelp = gFalse;
58
59 static const ArgDesc argDesc[] = {
60 {"-f", argInt, &firstPage, 0,
61 "first page to convert"},
62 {"-l", argInt, &lastPage, 0,
63 "last page to convert"},
64 {"-j", argFlag, &dumpJPEG, 0,
65 "write JPEG images as JPEG files"},
66 {"-opw", argString, ownerPassword, sizeof(ownerPassword),
67 "owner password (for encrypted files)"},
68 {"-upw", argString, userPassword, sizeof(userPassword),
69 "user password (for encrypted files)"},
70 {"-p", argFlag, &pageNames, 0,
71 "include page numbers in output file names"},
72 {"-q", argFlag, &quiet, 0,
73 "don't print any messages or errors"},
74 {"-v", argFlag, &printVersion, 0,
75 "print copyright and version info"},
76 {"-h", argFlag, &printHelp, 0,
77 "print usage information"},
78 {"-help", argFlag, &printHelp, 0,
79 "print usage information"},
80 {"--help", argFlag, &printHelp, 0,
81 "print usage information"},
82 {"-?", argFlag, &printHelp, 0,
83 "print usage information"},
84 {NULL}
85 };
86
main(int argc,char * argv[])87 int main(int argc, char *argv[]) {
88 PDFDoc *doc;
89 GooString *fileName;
90 char *imgRoot;
91 GooString *ownerPW, *userPW;
92 ImageOutputDev *imgOut;
93 GBool ok;
94 int exitCode;
95
96 exitCode = 99;
97
98 // parse args
99 ok = parseArgs(argDesc, &argc, argv);
100 if (!ok || argc != 3 || printVersion || printHelp) {
101 fprintf(stderr, "pdfimages version %s\n", PACKAGE_VERSION);
102 fprintf(stderr, "%s\n", popplerCopyright);
103 fprintf(stderr, "%s\n", xpdfCopyright);
104 if (!printVersion) {
105 printUsage("pdfimages", "<PDF-file> <image-root>", argDesc);
106 }
107 if (printVersion || printHelp)
108 exitCode = 0;
109 goto err0;
110 }
111 fileName = new GooString(argv[1]);
112 imgRoot = argv[2];
113
114 // read config file
115 globalParams = new GlobalParams();
116 if (quiet) {
117 globalParams->setErrQuiet(quiet);
118 }
119
120 // open PDF file
121 if (ownerPassword[0] != '\001') {
122 ownerPW = new GooString(ownerPassword);
123 } else {
124 ownerPW = NULL;
125 }
126 if (userPassword[0] != '\001') {
127 userPW = new GooString(userPassword);
128 } else {
129 userPW = NULL;
130 }
131 if (fileName->cmp("-") == 0) {
132 delete fileName;
133 fileName = new GooString("fd://0");
134 }
135
136 doc = PDFDocFactory().createPDFDoc(*fileName, ownerPW, userPW);
137 delete fileName;
138
139 if (userPW) {
140 delete userPW;
141 }
142 if (ownerPW) {
143 delete ownerPW;
144 }
145 if (!doc->isOk()) {
146 exitCode = 1;
147 goto err1;
148 }
149
150 // check for copy permission
151 #ifdef ENFORCE_PERMISSIONS
152 if (!doc->okToCopy()) {
153 error(-1, "Copying of images from this document is not allowed.");
154 exitCode = 3;
155 goto err1;
156 }
157 #endif
158
159 // get page range
160 if (firstPage < 1)
161 firstPage = 1;
162 if (lastPage < 1 || lastPage > doc->getNumPages())
163 lastPage = doc->getNumPages();
164
165 // write image files
166 imgOut = new ImageOutputDev(imgRoot, pageNames, dumpJPEG);
167 if (imgOut->isOk()) {
168 doc->displayPages(imgOut, firstPage, lastPage, 72, 72, 0,
169 gTrue, gFalse, gFalse);
170 }
171 delete imgOut;
172
173 exitCode = 0;
174
175 // clean up
176 err1:
177 delete doc;
178 delete globalParams;
179 err0:
180
181 // check for memory leaks
182 Object::memCheck(stderr);
183 gMemReport(stderr);
184
185 return exitCode;
186 }
187