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, 2018 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 // Copyright (C) 2012, 2013, 2017 Adrian Johnson <ajohnson@redneon.com>
22 // Copyright (C) 2013 Suzuki Toshiya <mpsuzuki@hiroshima-u.ac.jp>
23 // Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
24 // Copyright (C) 2019, 2021 Oliver Sander <oliver.sander@tu-dresden.de>
25 // Copyright (C) 2019 Hartmut Goebel <h.goebel@crazy-compilers.com>
26 //
27 // To see a description of the changes please see the Changelog file that
28 // came with your tarball or type make ChangeLog if you are building from git
29 //
30 //========================================================================
31 
32 #include "config.h"
33 #include <poppler-config.h>
34 #include <cstdio>
35 #include <cstdlib>
36 #include <cstddef>
37 #include <cstring>
38 #include "parseargs.h"
39 #include "goo/GooString.h"
40 #include "goo/gmem.h"
41 #include "GlobalParams.h"
42 #include "Object.h"
43 #include "Stream.h"
44 #include "Array.h"
45 #include "Dict.h"
46 #include "XRef.h"
47 #include "Catalog.h"
48 #include "Page.h"
49 #include "PDFDoc.h"
50 #include "PDFDocFactory.h"
51 #include "ImageOutputDev.h"
52 #include "Error.h"
53 #include "Win32Console.h"
54 
55 static int firstPage = 1;
56 static int lastPage = 0;
57 static bool listImages = false;
58 static bool enablePNG = false;
59 static bool enableTiff = false;
60 static bool dumpJPEG = false;
61 static bool dumpJP2 = false;
62 static bool dumpJBIG2 = false;
63 static bool dumpCCITT = false;
64 static bool allFormats = false;
65 static bool pageNames = false;
66 static char ownerPassword[33] = "\001";
67 static char userPassword[33] = "\001";
68 static bool quiet = false;
69 static bool printVersion = false;
70 static bool printHelp = false;
71 
72 static const ArgDesc argDesc[] = { { "-f", argInt, &firstPage, 0, "first page to convert" },
73                                    { "-l", argInt, &lastPage, 0, "last page to convert" },
74 #ifdef ENABLE_LIBPNG
75                                    { "-png", argFlag, &enablePNG, 0, "change the default output format to PNG" },
76 #endif
77 #ifdef ENABLE_LIBTIFF
78                                    { "-tiff", argFlag, &enableTiff, 0, "change the default output format to TIFF" },
79 #endif
80                                    { "-j", argFlag, &dumpJPEG, 0, "write JPEG images as JPEG files" },
81                                    { "-jp2", argFlag, &dumpJP2, 0, "write JPEG2000 images as JP2 files" },
82                                    { "-jbig2", argFlag, &dumpJBIG2, 0, "write JBIG2 images as JBIG2 files" },
83                                    { "-ccitt", argFlag, &dumpCCITT, 0, "write CCITT images as CCITT files" },
84                                    { "-all", argFlag, &allFormats, 0, "equivalent to -png -tiff -j -jp2 -jbig2 -ccitt" },
85                                    { "-list", argFlag, &listImages, 0, "print list of images instead of saving" },
86                                    { "-opw", argString, ownerPassword, sizeof(ownerPassword), "owner password (for encrypted files)" },
87                                    { "-upw", argString, userPassword, sizeof(userPassword), "user password (for encrypted files)" },
88                                    { "-p", argFlag, &pageNames, 0, "include page numbers in output file names" },
89                                    { "-q", argFlag, &quiet, 0, "don't print any messages or errors" },
90                                    { "-v", argFlag, &printVersion, 0, "print copyright and version info" },
91                                    { "-h", argFlag, &printHelp, 0, "print usage information" },
92                                    { "-help", argFlag, &printHelp, 0, "print usage information" },
93                                    { "--help", argFlag, &printHelp, 0, "print usage information" },
94                                    { "-?", argFlag, &printHelp, 0, "print usage information" },
95                                    {} };
96 
main(int argc,char * argv[])97 int main(int argc, char *argv[])
98 {
99     GooString *fileName;
100     char *imgRoot = nullptr;
101     GooString *ownerPW, *userPW;
102     ImageOutputDev *imgOut;
103     bool ok;
104 
105     Win32Console win32Console(&argc, &argv);
106 
107     // parse args
108     ok = parseArgs(argDesc, &argc, argv);
109     if (!ok || (listImages && argc != 2) || (!listImages && argc != 3) || printVersion || printHelp) {
110         fprintf(stderr, "pdfimages version %s\n", PACKAGE_VERSION);
111         fprintf(stderr, "%s\n", popplerCopyright);
112         fprintf(stderr, "%s\n", xpdfCopyright);
113         if (!printVersion) {
114             printUsage("pdfimages", "<PDF-file> <image-root>", argDesc);
115         }
116         if (printVersion || printHelp)
117             return 0;
118         return 99;
119     }
120     fileName = new GooString(argv[1]);
121     if (!listImages)
122         imgRoot = argv[2];
123 
124     // read config file
125     globalParams = std::make_unique<GlobalParams>();
126     if (quiet) {
127         globalParams->setErrQuiet(quiet);
128     }
129 
130     // open PDF file
131     if (ownerPassword[0] != '\001') {
132         ownerPW = new GooString(ownerPassword);
133     } else {
134         ownerPW = nullptr;
135     }
136     if (userPassword[0] != '\001') {
137         userPW = new GooString(userPassword);
138     } else {
139         userPW = nullptr;
140     }
141     if (fileName->cmp("-") == 0) {
142         delete fileName;
143         fileName = new GooString("fd://0");
144     }
145 
146     std::unique_ptr<PDFDoc> doc = PDFDocFactory().createPDFDoc(*fileName, ownerPW, userPW);
147     delete fileName;
148 
149     if (userPW) {
150         delete userPW;
151     }
152     if (ownerPW) {
153         delete ownerPW;
154     }
155     if (!doc->isOk()) {
156         return 1;
157     }
158 
159     // check for copy permission
160 #ifdef ENFORCE_PERMISSIONS
161     if (!doc->okToCopy()) {
162         error(errNotAllowed, -1, "Copying of images from this document is not allowed.");
163         return 3;
164     }
165 #endif
166 
167     // get page range
168     if (firstPage < 1)
169         firstPage = 1;
170     if (firstPage > doc->getNumPages()) {
171         error(errCommandLine, -1, "Wrong page range given: the first page ({0:d}) can not be larger then the number of pages in the document ({1:d}).", firstPage, doc->getNumPages());
172         return 99;
173     }
174     if (lastPage < 1 || lastPage > doc->getNumPages())
175         lastPage = doc->getNumPages();
176     if (lastPage < firstPage) {
177         error(errCommandLine, -1, "Wrong page range given: the first page ({0:d}) can not be after the last page ({1:d}).", firstPage, lastPage);
178         return 99;
179     }
180 
181     // write image files
182     imgOut = new ImageOutputDev(imgRoot, pageNames, listImages);
183     if (imgOut->isOk()) {
184         if (allFormats) {
185             imgOut->enablePNG(true);
186             imgOut->enableTiff(true);
187             imgOut->enableJpeg(true);
188             imgOut->enableJpeg2000(true);
189             imgOut->enableJBig2(true);
190             imgOut->enableCCITT(true);
191         } else {
192             imgOut->enablePNG(enablePNG);
193             imgOut->enableTiff(enableTiff);
194             imgOut->enableJpeg(dumpJPEG);
195             imgOut->enableJpeg2000(dumpJP2);
196             imgOut->enableJBig2(dumpJBIG2);
197             imgOut->enableCCITT(dumpCCITT);
198         }
199         doc->displayPages(imgOut, firstPage, lastPage, 72, 72, 0, true, false, false);
200     }
201     delete imgOut;
202 
203     return 0;
204 }
205