]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/pdftops.cc
Imported Xpdf 2.03 and fixed build.
[evince.git] / pdf / xpdf / pdftops.cc
1 //========================================================================
2 //
3 // pdftops.cc
4 //
5 // Copyright 1996-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 "PSOutputDev.h"
27 #include "Error.h"
28 #include "config.h"
29
30 static int firstPage = 1;
31 static int lastPage = 0;
32 static GBool level1 = gFalse;
33 static GBool level1Sep = gFalse;
34 static GBool level2 = gFalse;
35 static GBool level2Sep = gFalse;
36 static GBool level3 = gFalse;
37 static GBool level3Sep = gFalse;
38 static GBool doEPS = gFalse;
39 static GBool doForm = gFalse;
40 #if OPI_SUPPORT
41 static GBool doOPI = gFalse;
42 #endif
43 static GBool noEmbedT1Fonts = gFalse;
44 static GBool noEmbedTTFonts = gFalse;
45 static GBool noEmbedCIDPSFonts = gFalse;
46 static GBool noEmbedCIDTTFonts = gFalse;
47 static char paperSize[15] = "";
48 static int paperWidth = 0;
49 static int paperHeight = 0;
50 static GBool duplex = gFalse;
51 static char ownerPassword[33] = "\001";
52 static char userPassword[33] = "\001";
53 static GBool quiet = gFalse;
54 static char cfgFileName[256] = "";
55 static GBool printVersion = gFalse;
56 static GBool printHelp = gFalse;
57
58 static ArgDesc argDesc[] = {
59   {"-f",      argInt,      &firstPage,      0,
60    "first page to print"},
61   {"-l",      argInt,      &lastPage,       0,
62    "last page to print"},
63   {"-level1", argFlag,     &level1,         0,
64    "generate Level 1 PostScript"},
65   {"-level1sep", argFlag,  &level1Sep,      0,
66    "generate Level 1 separable PostScript"},
67   {"-level2", argFlag,     &level2,         0,
68    "generate Level 2 PostScript"},
69   {"-level2sep", argFlag,  &level2Sep,      0,
70    "generate Level 2 separable PostScript"},
71   {"-level3", argFlag,     &level3,         0,
72    "generate Level 3 PostScript"},
73   {"-level3sep", argFlag,  &level3Sep,      0,
74    "generate Level 3 separable PostScript"},
75   {"-eps",    argFlag,     &doEPS,          0,
76    "generate Encapsulated PostScript (EPS)"},
77   {"-form",   argFlag,     &doForm,         0,
78    "generate a PostScript form"},
79 #if OPI_SUPPORT
80   {"-opi",    argFlag,     &doOPI,          0,
81    "generate OPI comments"},
82 #endif
83   {"-noembt1", argFlag,     &noEmbedT1Fonts, 0,
84    "don't embed Type 1 fonts"},
85   {"-noembtt", argFlag,    &noEmbedTTFonts, 0,
86    "don't embed TrueType fonts"},
87   {"-noembcidps", argFlag, &noEmbedCIDPSFonts, 0,
88    "don't embed CID PostScript fonts"},
89   {"-noembcidtt", argFlag, &noEmbedCIDTTFonts, 0,
90    "don't embed CID TrueType fonts"},
91   {"-paper",  argString,   paperSize,       sizeof(paperSize),
92    "paper size (letter, legal, A4, A3, match)"},
93   {"-paperw", argInt,      &paperWidth,     0,
94    "paper width, in points"},
95   {"-paperh", argInt,      &paperHeight,    0,
96    "paper height, in points"},
97   {"-duplex", argFlag,     &duplex,         0,
98    "enable duplex printing"},
99   {"-opw",    argString,   ownerPassword,   sizeof(ownerPassword),
100    "owner password (for encrypted files)"},
101   {"-upw",    argString,   userPassword,    sizeof(userPassword),
102    "user password (for encrypted files)"},
103   {"-q",      argFlag,     &quiet,          0,
104    "don't print any messages or errors"},
105   {"-cfg",        argString,      cfgFileName,    sizeof(cfgFileName),
106    "configuration file to use in place of .xpdfrc"},
107   {"-v",      argFlag,     &printVersion,   0,
108    "print copyright and version info"},
109   {"-h",      argFlag,     &printHelp,      0,
110    "print usage information"},
111   {"-help",   argFlag,     &printHelp,      0,
112    "print usage information"},
113   {"--help",  argFlag,     &printHelp,      0,
114    "print usage information"},
115   {"-?",      argFlag,     &printHelp,      0,
116    "print usage information"},
117   {NULL}
118 };
119
120 int main(int argc, char *argv[]) {
121   PDFDoc *doc;
122   GString *fileName;
123   GString *psFileName;
124   PSLevel level;
125   PSOutMode mode;
126   GString *ownerPW, *userPW;
127   PSOutputDev *psOut;
128   GBool ok;
129   char *p;
130   int exitCode;
131
132   exitCode = 99;
133
134   // parse args
135   ok = parseArgs(argDesc, &argc, argv);
136   if (!ok || argc < 2 || argc > 3 || printVersion || printHelp) {
137     fprintf(stderr, "pdftops version %s\n", xpdfVersion);
138     fprintf(stderr, "%s\n", xpdfCopyright);
139     if (!printVersion) {
140       printUsage("pdftops", "<PDF-file> [<PS-file>]", argDesc);
141     }
142     exit(1);
143   }
144   if ((level1 ? 1 : 0) +
145       (level1Sep ? 1 : 0) +
146       (level2 ? 1 : 0) +
147       (level2Sep ? 1 : 0) +
148       (level3 ? 1 : 0) +
149       (level3Sep ? 1 : 0) > 1) {
150     fprintf(stderr, "Error: use only one of the 'level' options.\n");
151     exit(1);
152   }
153   if (doEPS && doForm) {
154     fprintf(stderr, "Error: use only one of -eps and -form\n");
155     exit(1);
156   }
157   if (level1) {
158     level = psLevel1;
159   } else if (level1Sep) {
160     level = psLevel1Sep;
161   } else if (level2Sep) {
162     level = psLevel2Sep;
163   } else if (level3) {
164     level = psLevel3;
165   } else if (level3Sep) {
166     level = psLevel3Sep;
167   } else {
168     level = psLevel2;
169   }
170   if (doForm && level < psLevel2) {
171     fprintf(stderr, "Error: forms are only available with Level 2 output.\n");
172     exit(1);
173   }
174   mode = doEPS ? psModeEPS
175                : doForm ? psModeForm
176                         : psModePS;
177   fileName = new GString(argv[1]);
178
179   // read config file
180   globalParams = new GlobalParams(cfgFileName);
181   if (paperSize[0]) {
182     if (!globalParams->setPSPaperSize(paperSize)) {
183       fprintf(stderr, "Invalid paper size\n");
184       delete fileName;
185       goto err0;
186     }
187   } else {
188     if (paperWidth) {
189       globalParams->setPSPaperWidth(paperWidth);
190     }
191     if (paperHeight) {
192       globalParams->setPSPaperHeight(paperHeight);
193     }
194   }
195   if (duplex) {
196     globalParams->setPSDuplex(duplex);
197   }
198   if (level1 || level1Sep || level2 || level2Sep || level3 || level3Sep) {
199     globalParams->setPSLevel(level);
200   }
201   if (noEmbedT1Fonts) {
202     globalParams->setPSEmbedType1(!noEmbedT1Fonts);
203   }
204   if (noEmbedTTFonts) {
205     globalParams->setPSEmbedTrueType(!noEmbedTTFonts);
206   }
207   if (noEmbedCIDPSFonts) {
208     globalParams->setPSEmbedCIDPostScript(!noEmbedCIDPSFonts);
209   }
210   if (noEmbedCIDTTFonts) {
211     globalParams->setPSEmbedCIDTrueType(!noEmbedCIDTTFonts);
212   }
213 #if OPI_SUPPORT
214   if (doOPI) {
215     globalParams->setPSOPI(doOPI);
216   }
217 #endif
218   if (quiet) {
219     globalParams->setErrQuiet(quiet);
220   }
221
222   // open PDF file
223   if (ownerPassword[0] != '\001') {
224     ownerPW = new GString(ownerPassword);
225   } else {
226     ownerPW = NULL;
227   }
228   if (userPassword[0] != '\001') {
229     userPW = new GString(userPassword);
230   } else {
231     userPW = NULL;
232   }
233   doc = new PDFDoc(fileName, ownerPW, userPW);
234   if (userPW) {
235     delete userPW;
236   }
237   if (ownerPW) {
238     delete ownerPW;
239   }
240   if (!doc->isOk()) {
241     exitCode = 1;
242     goto err1;
243   }
244
245   // check for print permission
246   if (!doc->okToPrint()) {
247     error(-1, "Printing this document is not allowed.");
248     exitCode = 3;
249     goto err1;
250   }
251
252   // construct PostScript file name
253   if (argc == 3) {
254     psFileName = new GString(argv[2]);
255   } else {
256     p = fileName->getCString() + fileName->getLength() - 4;
257     if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF")) {
258       psFileName = new GString(fileName->getCString(),
259                                fileName->getLength() - 4);
260     } else {
261       psFileName = fileName->copy();
262     }
263     psFileName->append(doEPS ? ".eps" : ".ps");
264   }
265
266   // get page range
267   if (firstPage < 1) {
268     firstPage = 1;
269   }
270   if (lastPage < 1 || lastPage > doc->getNumPages()) {
271     lastPage = doc->getNumPages();
272   }
273
274   // check for multi-page EPS or form
275   if ((doEPS || doForm) && firstPage != lastPage) {
276     error(-1, "EPS and form files can only contain one page.");
277     goto err2;
278   }
279
280   // write PostScript file
281   psOut = new PSOutputDev(psFileName->getCString(), doc->getXRef(),
282                           doc->getCatalog(), firstPage, lastPage, mode);
283   if (psOut->isOk()) {
284     doc->displayPages(psOut, firstPage, lastPage, 72, 72, 0, gFalse);
285   } else {
286     delete psOut;
287     exitCode = 2;
288     goto err2;
289   }
290   delete psOut;
291
292   exitCode = 0;
293
294   // clean up
295  err2:
296   delete psFileName;
297  err1:
298   delete doc;
299  err0:
300   delete globalParams;
301
302   // check for memory leaks
303   Object::memCheck(stderr);
304   gMemReport(stderr);
305
306   return exitCode;
307 }