]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/pdftops.cc
Synched with Xpdf 0.92
[evince.git] / pdf / xpdf / pdftops.cc
1 //========================================================================
2 //
3 // pdftops.cc
4 //
5 // Copyright 1996 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 "PSOutputDev.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 noEmbedFonts = gFalse;
32 static GBool doForm = gFalse;
33 static char userPassword[33] = "";
34 static GBool printVersion = gFalse;
35 static GBool printHelp = gFalse;
36
37 static ArgDesc argDesc[] = {
38   {"-f",      argInt,      &firstPage,      0,
39    "first page to print"},
40   {"-l",      argInt,      &lastPage,       0,
41    "last page to print"},
42   {"-paperw", argInt,      &paperWidth,     0,
43    "paper width, in points"},
44   {"-paperh", argInt,      &paperHeight,    0,
45    "paper height, in points"},
46   {"-level1", argFlag,     &psOutLevel1,    0,
47    "generate Level 1 PostScript"},
48   {"-level1sep", argFlag,  &psOutLevel1Sep, 0,
49    "generate Level 1 separable PostScript"},
50   {"-eps",    argFlag,     &psOutEPS,       0,
51    "generate Encapsulated PostScript (EPS)"},
52 #if OPI_SUPPORT
53   {"-opi",    argFlag,     &psOutOPI,       0,
54    "generate OPI comments"},
55 #endif
56   {"-noemb",  argFlag,     &noEmbedFonts,   0,
57    "don't embed Type 1 fonts"},
58   {"-form",   argFlag,     &doForm,         0,
59    "generate a PostScript form"},
60   {"-upw",    argString,   userPassword,    sizeof(userPassword),
61    "user password (for encrypted files)"},
62   {"-q",      argFlag,     &errQuiet,       0,
63    "don't print any messages or errors"},
64   {"-v",      argFlag,     &printVersion,   0,
65    "print copyright and version info"},
66   {"-h",      argFlag,     &printHelp,      0,
67    "print usage information"},
68   {"-help",   argFlag,     &printHelp,      0,
69    "print usage information"},
70   {NULL}
71 };
72
73 int main(int argc, char *argv[]) {
74   PDFDoc *doc;
75   GString *fileName;
76   GString *psFileName;
77   GString *userPW;
78   PSOutputDev *psOut;
79   GBool ok;
80   char *p;
81
82   // parse args
83   ok = parseArgs(argDesc, &argc, argv);
84   if (!ok || argc < 2 || argc > 3 || printVersion || printHelp) {
85     fprintf(stderr, "pdftops version %s\n", xpdfVersion);
86     fprintf(stderr, "%s\n", xpdfCopyright);
87     if (!printVersion) {
88       printUsage("pdftops", "<PDF-file> [<PS-file>]", argDesc);
89     }
90     exit(1);
91   }
92   if (psOutLevel1 && psOutLevel1Sep) {
93     fprintf(stderr, "Error: use -level1 or -level1sep, not both.\n");
94     exit(1);
95   }
96   if (doForm && (psOutLevel1 || psOutLevel1Sep)) {
97     fprintf(stderr, "Error: forms are only available with Level 2 output.\n");
98     exit(1);
99   }
100   fileName = new GString(argv[1]);
101
102   // init error file
103   errorInit();
104
105   // read config file
106   initParams(xpdfConfigFile);
107
108   // open PDF file
109   xref = NULL;
110   if (userPassword[0]) {
111     userPW = new GString(userPassword);
112   } else {
113     userPW = NULL;
114   }
115   doc = new PDFDoc(fileName, userPW);
116   if (userPW) {
117     delete userPW;
118   }
119   if (!doc->isOk()) {
120     goto err1;
121   }
122
123   // check for print permission
124   if (!doc->okToPrint()) {
125     error(-1, "Printing this document is not allowed.");
126     goto err1;
127   }
128
129   // construct PostScript file name
130   if (argc == 3) {
131     psFileName = new GString(argv[2]);
132   } else {
133     p = fileName->getCString() + fileName->getLength() - 4;
134     if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF"))
135       psFileName = new GString(fileName->getCString(),
136                                fileName->getLength() - 4);
137     else
138       psFileName = fileName->copy();
139     psFileName->append(psOutEPS ? ".eps" : ".ps");
140   }
141
142   // get page range
143   if (firstPage < 1)
144     firstPage = 1;
145   if (lastPage < 1 || lastPage > doc->getNumPages())
146     lastPage = doc->getNumPages();
147   if (doForm)
148     lastPage = firstPage;
149
150   // check for multi-page EPS
151   if (psOutEPS && firstPage != lastPage) {
152     error(-1, "EPS files can only contain one page.");
153     goto err2;
154   }
155
156   // write PostScript file
157   psOut = new PSOutputDev(psFileName->getCString(), doc->getCatalog(),
158                           firstPage, lastPage, !noEmbedFonts, doForm);
159   if (psOut->isOk())
160     doc->displayPages(psOut, firstPage, lastPage, 72, 0, gFalse);
161   delete psOut;
162
163   // clean up
164  err2:
165   delete psFileName;
166  err1:
167   delete doc;
168   freeParams();
169
170   // check for memory leaks
171   Object::memCheck(stderr);
172   gMemReport(stderr);
173
174   return 0;
175 }