]> www.fi.muni.cz Git - evince.git/blob - pdfimages.cc
5ab74fbd988db3cc14163767d2d5f1fad8f7d82b
[evince.git] / pdfimages.cc
1 //========================================================================
2 //
3 // pdfimages.cc
4 //
5 // Copyright 1998 Derek B. Noonburg
6 //
7 //========================================================================
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stddef.h>
12 #include <string.h>
13 #include "parseargs.h"
14 #include "GString.h"
15 #include "gmem.h"
16 #include "Object.h"
17 #include "Stream.h"
18 #include "Array.h"
19 #include "Dict.h"
20 #include "XRef.h"
21 #include "Catalog.h"
22 #include "Page.h"
23 #include "PDFDoc.h"
24 #include "ImageOutputDev.h"
25 #include "Params.h"
26 #include "Error.h"
27 #include "config.h"
28
29 static int firstPage = 1;
30 static int lastPage = 0;
31 static GBool dumpJPEG = gFalse;
32 GBool printCommands = gFalse;
33 static GBool printHelp = gFalse;
34
35 static ArgDesc argDesc[] = {
36   {"-f",      argInt,      &firstPage,     0,
37    "first page to convert"},
38   {"-l",      argInt,      &lastPage,      0,
39    "last page to convert"},
40   {"-j",      argFlag,     &dumpJPEG,      0,
41    "write JPEG images as JPEG files"},
42   {"-q",      argFlag,     &errQuiet,      0,
43    "don't print any messages or errors"},
44   {"-h",      argFlag,     &printHelp,     0,
45    "print usage information"},
46   {"-help",   argFlag,     &printHelp,     0,
47    "print usage information"},
48   {NULL}
49 };
50
51 int main(int argc, char *argv[]) {
52   PDFDoc *doc;
53   GString *fileName;
54   char *imgRoot;
55   ImageOutputDev *imgOut;
56   GBool ok;
57
58   // parse args
59   ok = parseArgs(argDesc, &argc, argv);
60   if (!ok || argc != 3 || printHelp) {
61     fprintf(stderr, "pdfimages version %s\n", xpdfVersion);
62     fprintf(stderr, "%s\n", xpdfCopyright);
63     printUsage("pdfimages", "<PDF-file> <image-root>", argDesc);
64     exit(1);
65   }
66   fileName = new GString(argv[1]);
67   imgRoot = argv[2];
68
69   // init error file
70   errorInit();
71
72   // read config file
73   initParams(xpdfConfigFile);
74
75   // open PDF file
76   xref = NULL;
77   doc = new PDFDoc(fileName);
78   if (!doc->isOk()) {
79     goto err1;
80   }
81
82   // check for copy permission
83   if (!doc->okToCopy()) {
84     error(-1, "Copying of images from this document is not allowed.");
85     goto err2;
86   }
87
88   // get page range
89   if (firstPage < 1)
90     firstPage = 1;
91   if (lastPage < 1 || lastPage > doc->getNumPages())
92     lastPage = doc->getNumPages();
93
94   // write image files
95   imgOut = new ImageOutputDev(imgRoot, dumpJPEG);
96   if (imgOut->isOk())
97     doc->displayPages(imgOut, firstPage, lastPage, 72, 0);
98   delete imgOut;
99
100   // clean up
101  err2:
102   delete doc;
103  err1:
104   freeParams();
105
106   // check for memory leaks
107   Object::memCheck(stderr);
108   gMemReport(stderr);
109
110   return 0;
111 }