]>
git.ipfire.org Git - thirdparty/cups.git/blob - pdftops/pdftops.cxx
4 // PDF to PostScript filter front-end for the Common UNIX Printing
7 // Copyright 1997-2005 by Easy Software Products.
9 // These coded instructions, statements, and computer programs are the
10 // property of Easy Software Products and are protected by Federal
11 // copyright law. Distribution and use rights are outlined in the file
12 // "LICENSE.txt" which should have been included with this file. If this
13 // file is missing or damaged please contact Easy Software Products
16 // Attn: CUPS Licensing Information
17 // Easy Software Products
18 // 44141 Airport View Drive, Suite 204
19 // Hollywood, Maryland 20636 USA
21 // Voice: (301) 373-9600
22 // EMail: cups-info@cups.org
23 // WWW: http://www.cups.org
27 // main() - Main entry for filter...
31 // Include necessary headers...
37 #include <cups/string.h>
38 #include "parseargs.h"
49 #include "PSOutputDev.h"
50 #include "GlobalParams.h"
54 #include <cups/cups.h>
58 // 'main()' - Main entry for filter...
61 int // O - Exit status
62 main(int argc
, // I - Number of command-line args
63 char *argv
[]) // I - Command-line arguments
65 PDFDoc
*doc
; // Input file
66 GString
*fileName
; // Input filename
67 GString
*psFileName
; // Output filename
68 PSLevel level
; // PostScript level
69 PSOutputDev
*psOut
; // Output device
70 int num_options
; // Number of options
71 cups_option_t
*options
; // Options
72 const char *val
; // Option value
73 ppd_file_t
*ppd
; // Current PPD
74 ppd_size_t
*size
; // Current media size
75 FILE *fp
; // Copy file
76 const char *server_root
; // Location of config files
77 char tempfile
[1024]; // Temporary file
78 char buffer
[8192]; // Copy buffer
79 int bytes
; // Bytes copied
80 int width
, length
; // Size in points
81 int left
, bottom
, right
, top
;
82 // Imageable area in points
83 int orientation
; // Orientation
84 int temp
; // Temporary var
85 int duplex
; // Duplex the output?
86 int fit
; // Fit the pages to the output
87 int printCommands
; // Output debug info for commands?
90 // Make sure status messages are not buffered...
93 // Make sure we have the right number of arguments for CUPS!
94 if (argc
< 6 || argc
> 7) {
95 fputs("Usage: pdftops job user title copies options [filename]\n", stderr
);
99 // Copy stdin if needed...
101 if ((fp
= fopen(cupsTempFile(tempfile
, sizeof(tempfile
)), "w")) == NULL
) {
102 perror("ERROR: Unable to copy PDF file");
106 fprintf(stderr
, "DEBUG: pdftops - copying to temp print file \"%s\"\n",
109 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
110 fwrite(buffer
, 1, bytes
, fp
);
113 fileName
= new GString(tempfile
);
115 fileName
= new GString(argv
[6]);
119 // Default to "Universal" size - min of A4 and Letter...
130 // Get PPD and initialize options as needed...
131 num_options
= cupsParseOptions(argv
[5], 0, &options
);
133 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
)
135 fprintf(stderr
, "DEBUG: pdftops - opened PPD file \"%s\"...\n", getenv("PPD"));
137 ppdMarkDefaults(ppd
);
138 cupsMarkOptions(ppd
, num_options
, options
);
140 if ((size
= ppdPageSize(ppd
, NULL
)) != NULL
)
142 left
= (int)size
->left
;
143 bottom
= (int)size
->bottom
;
144 right
= (int)size
->right
;
145 top
= (int)size
->top
;
146 width
= (int)size
->width
;
147 length
= (int)size
->length
;
150 level
= ppd
->language_level
== 1 ? psLevel1
: psLevel2
;
153 // Track the orientation of the print job and update the page
154 // dimensions and margins as needed...
157 if ((val
= cupsGetOption("landscape", num_options
, options
)) != NULL
)
159 if (strcasecmp(val
, "no") != 0 && strcasecmp(val
, "off") != 0 &&
160 strcasecmp(val
, "false") != 0)
163 else if ((val
= cupsGetOption("orientation-requested", num_options
, options
)) != NULL
)
166 * Map IPP orientation values to 0 to 3:
170 * 5 = -90 degrees = 3
171 * 6 = 180 degrees = 2
174 orientation
= atoi(val
) - 3;
175 if (orientation
>= 2)
179 switch (orientation
& 3)
181 case 0 : /* Portait */
184 case 1 : /* Landscape */
198 case 2 : /* Reverse Portrait */
200 left
= width
- right
;
203 temp
= length
- bottom
;
204 bottom
= length
- top
;
208 case 3 : /* Reverse Landscape */
210 left
= width
- right
;
213 temp
= length
- bottom
;
214 bottom
= length
- top
;
231 if ((val
= cupsGetOption("debug", num_options
, options
)) != NULL
&&
232 strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
233 strcasecmp(val
, "false"))
238 if ((val
= cupsGetOption("fitplot", num_options
, options
)) != NULL
&&
239 strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
240 strcasecmp(val
, "false"))
243 if ((val
= cupsGetOption("sides", num_options
, options
)) != NULL
&&
244 strncasecmp(val
, "two-", 4) == 0)
246 else if ((val
= cupsGetOption("Duplex", num_options
, options
)) != NULL
&&
247 strncasecmp(val
, "Duplex", 6) == 0)
249 else if ((val
= cupsGetOption("JCLDuplex", num_options
, options
)) != NULL
&&
250 strncasecmp(val
, "Duplex", 6) == 0)
252 else if ((val
= cupsGetOption("EFDuplex", num_options
, options
)) != NULL
&&
253 strncasecmp(val
, "Duplex", 6) == 0)
255 else if ((val
= cupsGetOption("KD03Duplex", num_options
, options
)) != NULL
&&
256 strncasecmp(val
, "Duplex", 6) == 0)
258 else if (ppdIsMarked(ppd
, "Duplex", "DuplexNoTumble") ||
259 ppdIsMarked(ppd
, "Duplex", "DuplexTumble") ||
260 ppdIsMarked(ppd
, "JCLDuplex", "DuplexNoTumble") ||
261 ppdIsMarked(ppd
, "JCLDuplex", "DuplexTumble") ||
262 ppdIsMarked(ppd
, "EFDuplex", "DuplexNoTumble") ||
263 ppdIsMarked(ppd
, "EFDuplex", "DuplexTumble") ||
264 ppdIsMarked(ppd
, "KD03Duplex", "DuplexNoTumble") ||
265 ppdIsMarked(ppd
, "KD03Duplex", "DuplexTumble"))
271 fprintf(stderr
, "DEBUG: pdftops - level = %d, width = %d, length = %d\n",
272 level
, width
, length
);
275 if ((server_root
= getenv("CUPS_SERVERROOT")) == NULL
)
276 server_root
= CUPS_SERVERROOT
;
278 snprintf(buffer
, sizeof(buffer
), "%s/pdftops.conf", server_root
);
280 globalParams
= new GlobalParams(buffer
);
282 globalParams
->setPSPaperWidth(width
);
283 globalParams
->setPSPaperHeight(length
);
284 globalParams
->setPSImageableArea(left
, bottom
, right
, top
);
285 globalParams
->setPSDuplex(duplex
);
286 globalParams
->setPSExpandSmaller(fit
);
287 globalParams
->setPSShrinkLarger(fit
);
288 globalParams
->setPSLevel(level
);
289 globalParams
->setPSASCIIHex(level
== psLevel1
);
290 globalParams
->setPSEmbedType1(1);
291 globalParams
->setPSEmbedTrueType(1);
292 globalParams
->setPSEmbedCIDPostScript(1);
293 globalParams
->setErrQuiet(0);
294 globalParams
->setPrintCommands(printCommands
);
297 setbuf(stdout
, NULL
);
300 doc
= new PDFDoc(fileName
, NULL
, NULL
);
302 // check for print permission
303 if (doc
->isOk() && doc
->okToPrint())
305 // CUPS always writes to stdout...
306 psFileName
= new GString("-");
308 // write PostScript file
309 psOut
= new PSOutputDev(psFileName
->getCString(), doc
->getXRef(),
310 doc
->getCatalog(), 1, doc
->getNumPages(),
311 psModePS
, 0, 0, 0, 0, gFalse
,
312 cupsGetOption("page-ranges", num_options
, options
));
314 doc
->displayPages(psOut
, 1, doc
->getNumPages(), 72.0, 72.0, 0,
315 gTrue
, gFalse
, gFalse
);
323 error(-1, "Unable to print this document.");
326 cupsFreeOptions(num_options
, options
);
331 // check for memory leaks
332 Object::memCheck(stderr
);
335 // Remove temp file if needed...