1 //========================================================================
5 // Copyright 1996-2003 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] = "\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;
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, 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"},
120 int main(int argc, char *argv[]) {
126 GString *ownerPW, *userPW;
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);
140 printUsage("pdftops", "<PDF-file> [<PS-file>]", argDesc);
144 if ((level1 ? 1 : 0) +
145 (level1Sep ? 1 : 0) +
147 (level2Sep ? 1 : 0) +
149 (level3Sep ? 1 : 0) > 1) {
150 fprintf(stderr, "Error: use only one of the 'level' options.\n");
153 if (doEPS && doForm) {
154 fprintf(stderr, "Error: use only one of -eps and -form\n");
159 } else if (level1Sep) {
161 } else if (level2Sep) {
165 } else if (level3Sep) {
170 if (doForm && level < psLevel2) {
171 fprintf(stderr, "Error: forms are only available with Level 2 output.\n");
174 mode = doEPS ? psModeEPS
175 : doForm ? psModeForm
177 fileName = new GString(argv[1]);
180 globalParams = new GlobalParams(cfgFileName);
182 if (!globalParams->setPSPaperSize(paperSize)) {
183 fprintf(stderr, "Invalid paper size\n");
189 globalParams->setPSPaperWidth(paperWidth);
192 globalParams->setPSPaperHeight(paperHeight);
196 globalParams->setPSDuplex(duplex);
198 if (level1 || level1Sep || level2 || level2Sep || level3 || level3Sep) {
199 globalParams->setPSLevel(level);
201 if (noEmbedT1Fonts) {
202 globalParams->setPSEmbedType1(!noEmbedT1Fonts);
204 if (noEmbedTTFonts) {
205 globalParams->setPSEmbedTrueType(!noEmbedTTFonts);
207 if (noEmbedCIDPSFonts) {
208 globalParams->setPSEmbedCIDPostScript(!noEmbedCIDPSFonts);
210 if (noEmbedCIDTTFonts) {
211 globalParams->setPSEmbedCIDTrueType(!noEmbedCIDTTFonts);
215 globalParams->setPSOPI(doOPI);
219 globalParams->setErrQuiet(quiet);
223 if (ownerPassword[0] != '\001') {
224 ownerPW = new GString(ownerPassword);
228 if (userPassword[0] != '\001') {
229 userPW = new GString(userPassword);
233 doc = new PDFDoc(fileName, ownerPW, userPW);
245 // check for print permission
246 if (!doc->okToPrint()) {
247 error(-1, "Printing this document is not allowed.");
252 // construct PostScript file name
254 psFileName = new GString(argv[2]);
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);
261 psFileName = fileName->copy();
263 psFileName->append(doEPS ? ".eps" : ".ps");
270 if (lastPage < 1 || lastPage > doc->getNumPages()) {
271 lastPage = doc->getNumPages();
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.");
280 // write PostScript file
281 psOut = new PSOutputDev(psFileName->getCString(), doc->getXRef(),
282 doc->getCatalog(), firstPage, lastPage, mode);
284 doc->displayPages(psOut, firstPage, lastPage, 72, 72, 0, gFalse);
302 // check for memory leaks
303 Object::memCheck(stderr);