]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/pdftops.cc
Import of Xpdf 2.01 for merge
[evince.git] / pdf / xpdf / pdftops.cc
1 //========================================================================
2 //
3 // pdftops.cc
4 //
5 // Copyright 1996-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 "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] = "";
52 static char userPassword[33] = "";
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)"},
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       goto err0;
185     }
186   } else {
187     if (paperWidth) {
188       globalParams->setPSPaperWidth(paperWidth);
189     }
190     if (paperHeight) {
191       globalParams->setPSPaperHeight(paperHeight);
192     }
193   }
194   if (duplex) {
195     globalParams->setPSDuplex(duplex);
196   }
197   if (level1 || level1Sep || level2 || level2Sep || level3 || level3Sep) {
198     globalParams->setPSLevel(level);
199   }
200   if (noEmbedT1Fonts) {
201     globalParams->setPSEmbedType1(!noEmbedT1Fonts);
202   }
203   if (noEmbedTTFonts) {
204     globalParams->setPSEmbedTrueType(!noEmbedTTFonts);
205   }
206   if (noEmbedCIDPSFonts) {
207     globalParams->setPSEmbedCIDPostScript(!noEmbedCIDPSFonts);
208   }
209   if (noEmbedCIDTTFonts) {
210     globalParams->setPSEmbedCIDTrueType(!noEmbedCIDTTFonts);
211   }
212 #if OPI_SUPPORT
213   if (doOPI) {
214     globalParams->setPSOPI(doOPI);
215   }
216 #endif
217   if (quiet) {
218     globalParams->setErrQuiet(quiet);
219   }
220
221   // open PDF file
222   if (ownerPassword[0]) {
223     ownerPW = new GString(ownerPassword);
224   } else {
225     ownerPW = NULL;
226   }
227   if (userPassword[0]) {
228     userPW = new GString(userPassword);
229   } else {
230     userPW = NULL;
231   }
232   doc = new PDFDoc(fileName, ownerPW, userPW);
233   if (userPW) {
234     delete userPW;
235   }
236   if (ownerPW) {
237     delete ownerPW;
238   }
239   if (!doc->isOk()) {
240     exitCode = 1;
241     goto err1;
242   }
243
244   // check for print permission
245   if (!doc->okToPrint()) {
246     error(-1, "Printing this document is not allowed.");
247     exitCode = 3;
248     goto err1;
249   }
250
251   // construct PostScript file name
252   if (argc == 3) {
253     psFileName = new GString(argv[2]);
254   } else {
255     p = fileName->getCString() + fileName->getLength() - 4;
256     if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF")) {
257       psFileName = new GString(fileName->getCString(),
258                                fileName->getLength() - 4);
259     } else {
260       psFileName = fileName->copy();
261     }
262     psFileName->append(doEPS ? ".eps" : ".ps");
263   }
264
265   // get page range
266   if (firstPage < 1) {
267     firstPage = 1;
268   }
269   if (lastPage < 1 || lastPage > doc->getNumPages()) {
270     lastPage = doc->getNumPages();
271   }
272
273   // check for multi-page EPS or form
274   if ((doEPS || doForm) && firstPage != lastPage) {
275     error(-1, "EPS and form files can only contain one page.");
276     goto err2;
277   }
278
279   // write PostScript file
280   psOut = new PSOutputDev(psFileName->getCString(), doc->getXRef(),
281                           doc->getCatalog(), firstPage, lastPage, mode);
282   if (psOut->isOk()) {
283     doc->displayPages(psOut, firstPage, lastPage, 72, 0, gFalse);
284   } else {
285     delete psOut;
286     exitCode = 2;
287     goto err2;
288   }
289   delete psOut;
290
291   exitCode = 0;
292
293   // clean up
294  err2:
295   delete psFileName;
296  err1:
297   delete doc;
298   delete globalParams;
299  err0:
300
301   // check for memory leaks
302   Object::memCheck(stderr);
303   gMemReport(stderr);
304
305   return exitCode;
306 }