1 //========================================================================
5 // Copyright 1996-2002 Glyph & Cog, LLC
7 //========================================================================
14 #include "parseargs.h"
17 #include "GlobalParams.h"
26 #include "PSOutputDev.h"
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;
41 static GBool doOPI = gFalse;
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;
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"},
80 {"-opi", argFlag, &doOPI, 0,
81 "generate OPI comments"},
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"},
120 int main(int argc, char *argv[]) {
126 GString *ownerPW, *userPW;
132 ok = parseArgs(argDesc, &argc, argv);
133 if (!ok || argc < 2 || argc > 3 || printVersion || printHelp) {
134 fprintf(stderr, "pdftops version %s\n", xpdfVersion);
135 fprintf(stderr, "%s\n", xpdfCopyright);
137 printUsage("pdftops", "<PDF-file> [<PS-file>]", argDesc);
141 if ((level1 ? 1 : 0) +
142 (level1Sep ? 1 : 0) +
144 (level2Sep ? 1 : 0) +
146 (level3Sep ? 1 : 0) > 1) {
147 fprintf(stderr, "Error: use only one of the 'level' options.\n");
150 if (doEPS && doForm) {
151 fprintf(stderr, "Error: use only one of -eps and -form\n");
156 } else if (level1Sep) {
158 } else if (level2Sep) {
162 } else if (level3Sep) {
167 if (doForm && level < psLevel2) {
168 fprintf(stderr, "Error: forms are only available with Level 2 output.\n");
171 mode = doEPS ? psModeEPS
172 : doForm ? psModeForm
174 fileName = new GString(argv[1]);
177 globalParams = new GlobalParams(cfgFileName);
179 if (!globalParams->setPSPaperSize(paperSize)) {
180 fprintf(stderr, "Invalid paper size\n");
185 globalParams->setPSPaperWidth(paperWidth);
188 globalParams->setPSPaperHeight(paperHeight);
192 globalParams->setPSDuplex(duplex);
194 if (level1 || level1Sep || level2 || level2Sep || level3 || level3Sep) {
195 globalParams->setPSLevel(level);
197 if (noEmbedT1Fonts) {
198 globalParams->setPSEmbedType1(!noEmbedT1Fonts);
200 if (noEmbedTTFonts) {
201 globalParams->setPSEmbedTrueType(!noEmbedTTFonts);
203 if (noEmbedCIDPSFonts) {
204 globalParams->setPSEmbedCIDPostScript(!noEmbedCIDPSFonts);
206 if (noEmbedCIDTTFonts) {
207 globalParams->setPSEmbedCIDTrueType(!noEmbedCIDTTFonts);
211 globalParams->setPSOPI(doOPI);
215 globalParams->setErrQuiet(quiet);
219 if (ownerPassword[0]) {
220 ownerPW = new GString(ownerPassword);
224 if (userPassword[0]) {
225 userPW = new GString(userPassword);
229 doc = new PDFDoc(fileName, ownerPW, userPW);
240 // check for print permission
241 if (!doc->okToPrint()) {
242 error(-1, "Printing this document is not allowed.");
246 // construct PostScript file name
248 psFileName = new GString(argv[2]);
250 p = fileName->getCString() + fileName->getLength() - 4;
251 if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF")) {
252 psFileName = new GString(fileName->getCString(),
253 fileName->getLength() - 4);
255 psFileName = fileName->copy();
257 psFileName->append(doEPS ? ".eps" : ".ps");
264 if (lastPage < 1 || lastPage > doc->getNumPages()) {
265 lastPage = doc->getNumPages();
268 // check for multi-page EPS or form
269 if ((doEPS || doForm) && firstPage != lastPage) {
270 error(-1, "EPS and form files can only contain one page.");
274 // write PostScript file
275 psOut = new PSOutputDev(psFileName->getCString(), doc->getXRef(),
276 doc->getCatalog(), firstPage, lastPage, mode);
278 doc->displayPages(psOut, firstPage, lastPage, 72, 0, gFalse);
289 // check for memory leaks
290 Object::memCheck(stderr);