pdftopbm.cpp
1//========================================================================
2//
3// pdftopbm.cpp
4//
5// Copyright 1998-2002 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 "PBMOutputDev.h"
27#include "Error.h"
28#include "config.h"
29
30static int firstPage = 1;
31static int lastPage = 0;
32static int resolution = 150;
33static char ownerPassword[33] = "";
34static char userPassword[33] = "";
35static GBool quiet = gFalse;
36static char cfgFileName[256] = "";
37static GBool printVersion = gFalse;
38static GBool printHelp = gFalse;
39
40static ArgDesc argDesc[] = {
41 {"-f", argInt, &firstPage, 0,
42 "first page to print"},
43 {"-l", argInt, &lastPage, 0,
44 "last page to print"},
45 {"-r", argInt, &resolution, 0,
46 "resolution, in DPI (default is 150)"},
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
68int main(int argc, char *argv[]) {
69 PDFDoc *doc;
70 GString *fileName;
71 char *pbmRoot;
72 GString *ownerPW, *userPW;
73 PBMOutputDev *pbmOut;
74 GBool ok;
75 int exitCode;
76
77 exitCode = 99;
78
79 // parse args
80 ok = parseArgs(argDesc, &argc, argv);
81 if (!ok || argc != 3 || printVersion || printHelp) {
82 fprintf(stderr, "pdftopbm version %s\n", xpdfVersion);
83 fprintf(stderr, "%s\n", xpdfCopyright);
84 if (!printVersion) {
85 printUsage("pdftopbm", "<PDF-file> <PBM-root>", argDesc);
86 }
87 goto err0;
88 }
89 fileName = new GString(argv[1]);
90 pbmRoot = argv[2];
91
92 // read config file
93 globalParams = new GlobalParams(cfgFileName);
94 if (quiet) {
95 globalParams->setErrQuiet(quiet);
96 }
97
98 // open PDF file
99 if (ownerPassword[0]) {
100 ownerPW = new GString(ownerPassword);
101 } else {
102 ownerPW = NULL;
103 }
104 if (userPassword[0]) {
105 userPW = new GString(userPassword);
106 } else {
107 userPW = NULL;
108 }
109 doc = new PDFDoc(fileName, ownerPW, userPW);
110 if (userPW) {
111 delete userPW;
112 }
113 if (ownerPW) {
114 delete ownerPW;
115 }
116 if (!doc->isOk()) {
117 exitCode = 1;
118 goto err1;
119 }
120
121 // get page range
122 if (firstPage < 1)
123 firstPage = 1;
124 if (lastPage < 1 || lastPage > doc->getNumPages())
125 lastPage = doc->getNumPages();
126
127 // write PBM files
128 pbmOut = PBMOutputDev::makePBMOutputDev(NULL, pbmRoot);
129 pbmOut->startDoc(doc->getXRef());
130 doc->displayPages(pbmOut, firstPage, lastPage, resolution, 0, gFalse);
131 PBMOutputDev::killPBMOutputDev(pbmOut);
132
133 exitCode = 0;
134
135 // clean up
136 err1:
137 delete doc;
138 delete globalParams;
139 err0:
140
141 // check for memory leaks
142 Object::memCheck(stderr);
143 gMemReport(stderr);
144
145 return exitCode;
146}