1 //========================================================================
5 // Copyright 1996-2003 Glyph & Cog, LLC
7 //========================================================================
14 #include "parseargs.h"
17 #include "GlobalParams.h"
26 #include "PSOutputDev.h"
28 #include "xpdfconfig.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 noCrop = gFalse;
51 static GBool expand = gFalse;
52 static GBool noShrink = gFalse;
53 static GBool noCenter = gFalse;
54 static GBool duplex = gFalse;
55 static char ownerPassword[33] = "\001";
56 static char userPassword[33] = "\001";
57 static GBool quiet = gFalse;
58 static char cfgFileName[256] = "";
59 static GBool printVersion = gFalse;
60 static GBool printHelp = gFalse;
62 static ArgDesc argDesc[] = {
63 {"-f", argInt, &firstPage, 0,
64 "first page to print"},
65 {"-l", argInt, &lastPage, 0,
66 "last page to print"},
67 {"-level1", argFlag, &level1, 0,
68 "generate Level 1 PostScript"},
69 {"-level1sep", argFlag, &level1Sep, 0,
70 "generate Level 1 separable PostScript"},
71 {"-level2", argFlag, &level2, 0,
72 "generate Level 2 PostScript"},
73 {"-level2sep", argFlag, &level2Sep, 0,
74 "generate Level 2 separable PostScript"},
75 {"-level3", argFlag, &level3, 0,
76 "generate Level 3 PostScript"},
77 {"-level3sep", argFlag, &level3Sep, 0,
78 "generate Level 3 separable PostScript"},
79 {"-eps", argFlag, &doEPS, 0,
80 "generate Encapsulated PostScript (EPS)"},
81 {"-form", argFlag, &doForm, 0,
82 "generate a PostScript form"},
84 {"-opi", argFlag, &doOPI, 0,
85 "generate OPI comments"},
87 {"-noembt1", argFlag, &noEmbedT1Fonts, 0,
88 "don't embed Type 1 fonts"},
89 {"-noembtt", argFlag, &noEmbedTTFonts, 0,
90 "don't embed TrueType fonts"},
91 {"-noembcidps", argFlag, &noEmbedCIDPSFonts, 0,
92 "don't embed CID PostScript fonts"},
93 {"-noembcidtt", argFlag, &noEmbedCIDTTFonts, 0,
94 "don't embed CID TrueType fonts"},
95 {"-paper", argString, paperSize, sizeof(paperSize),
96 "paper size (letter, legal, A4, A3, match)"},
97 {"-paperw", argInt, &paperWidth, 0,
98 "paper width, in points"},
99 {"-paperh", argInt, &paperHeight, 0,
100 "paper height, in points"},
101 {"-nocrop", argFlag, &noCrop, 0,
102 "don't crop pages to CropBox"},
103 {"-expand", argFlag, &expand, 0,
104 "expand pages smaller than the paper size"},
105 {"-noshrink", argFlag, &noShrink, 0,
106 "don't shrink pages larger than the paper size"},
107 {"-nocenter", argFlag, &noCenter, 0,
108 "don't center pages smaller than the paper size"},
109 {"-duplex", argFlag, &duplex, 0,
110 "enable duplex printing"},
111 {"-opw", argString, ownerPassword, sizeof(ownerPassword),
112 "owner password (for encrypted files)"},
113 {"-upw", argString, userPassword, sizeof(userPassword),
114 "user password (for encrypted files)"},
115 {"-q", argFlag, &quiet, 0,
116 "don't print any messages or errors"},
117 {"-cfg", argString, cfgFileName, sizeof(cfgFileName),
118 "configuration file to use in place of .xpdfrc"},
119 {"-v", argFlag, &printVersion, 0,
120 "print copyright and version info"},
121 {"-h", argFlag, &printHelp, 0,
122 "print usage information"},
123 {"-help", argFlag, &printHelp, 0,
124 "print usage information"},
125 {"--help", argFlag, &printHelp, 0,
126 "print usage information"},
127 {"-?", argFlag, &printHelp, 0,
128 "print usage information"},
132 int main(int argc, char *argv[]) {
138 GString *ownerPW, *userPW;
147 ok = parseArgs(argDesc, &argc, argv);
148 if (!ok || argc < 2 || argc > 3 || printVersion || printHelp) {
149 fprintf(stderr, "pdftops version %s\n", xpdfVersion);
150 fprintf(stderr, "%s\n", xpdfCopyright);
152 printUsage("pdftops", "<PDF-file> [<PS-file>]", argDesc);
156 if ((level1 ? 1 : 0) +
157 (level1Sep ? 1 : 0) +
159 (level2Sep ? 1 : 0) +
161 (level3Sep ? 1 : 0) > 1) {
162 fprintf(stderr, "Error: use only one of the 'level' options.\n");
165 if (doEPS && doForm) {
166 fprintf(stderr, "Error: use only one of -eps and -form\n");
171 } else if (level1Sep) {
173 } else if (level2Sep) {
177 } else if (level3Sep) {
182 if (doForm && level < psLevel2) {
183 fprintf(stderr, "Error: forms are only available with Level 2 output.\n");
186 mode = doEPS ? psModeEPS
187 : doForm ? psModeForm
189 fileName = new GString(argv[1]);
192 globalParams = new GlobalParams(cfgFileName);
194 if (!globalParams->setPSPaperSize(paperSize)) {
195 fprintf(stderr, "Invalid paper size\n");
201 globalParams->setPSPaperWidth(paperWidth);
204 globalParams->setPSPaperHeight(paperHeight);
208 globalParams->setPSCrop(gFalse);
211 globalParams->setPSExpandSmaller(gTrue);
214 globalParams->setPSShrinkLarger(gFalse);
217 globalParams->setPSCenter(gFalse);
220 globalParams->setPSDuplex(duplex);
222 if (level1 || level1Sep || level2 || level2Sep || level3 || level3Sep) {
223 globalParams->setPSLevel(level);
225 if (noEmbedT1Fonts) {
226 globalParams->setPSEmbedType1(!noEmbedT1Fonts);
228 if (noEmbedTTFonts) {
229 globalParams->setPSEmbedTrueType(!noEmbedTTFonts);
231 if (noEmbedCIDPSFonts) {
232 globalParams->setPSEmbedCIDPostScript(!noEmbedCIDPSFonts);
234 if (noEmbedCIDTTFonts) {
235 globalParams->setPSEmbedCIDTrueType(!noEmbedCIDTTFonts);
239 globalParams->setPSOPI(doOPI);
243 globalParams->setErrQuiet(quiet);
247 if (ownerPassword[0] != '\001') {
248 ownerPW = new GString(ownerPassword);
252 if (userPassword[0] != '\001') {
253 userPW = new GString(userPassword);
257 doc = new PDFDoc(fileName, ownerPW, userPW);
269 // check for print permission
270 if (!doc->okToPrint()) {
271 error(-1, "Printing this document is not allowed.");
276 // construct PostScript file name
278 psFileName = new GString(argv[2]);
280 p = fileName->getCString() + fileName->getLength() - 4;
281 if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF")) {
282 psFileName = new GString(fileName->getCString(),
283 fileName->getLength() - 4);
285 psFileName = fileName->copy();
287 psFileName->append(doEPS ? ".eps" : ".ps");
294 if (lastPage < 1 || lastPage > doc->getNumPages()) {
295 lastPage = doc->getNumPages();
298 // check for multi-page EPS or form
299 if ((doEPS || doForm) && firstPage != lastPage) {
300 error(-1, "EPS and form files can only contain one page.");
304 // write PostScript file
305 psOut = new PSOutputDev(psFileName->getCString(), doc->getXRef(),
306 doc->getCatalog(), firstPage, lastPage, mode);
308 doc->displayPages(psOut, firstPage, lastPage, 72, 72,
309 0, globalParams->getPSCrop(), gFalse);
327 // check for memory leaks
328 Object::memCheck(stderr);