summaryrefslogtreecommitdiff
path: root/pdftops/pdftops.cxx
blob: fcd5ce4bba4642a2436e96d193960ce3ec7773ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//
// "$Id: pdftops.cxx 6421 2007-03-30 20:02:19Z mike $"
//
//   PDF to PostScript filter front-end for the Common UNIX Printing
//   System (CUPS).
//
//   Copyright 1997-2006 by Easy Software Products.
//
//   These coded instructions, statements, and computer programs are the
//   property of Easy Software Products and are protected by Federal
//   copyright law.  Distribution and use rights are outlined in the file
//   "LICENSE.txt" which should have been included with this file.  If this
//   file is missing or damaged please contact Easy Software Products
//   at:
//
//       Attn: CUPS Licensing Information
//       Easy Software Products
//       44141 Airport View Drive, Suite 204
//       Hollywood, Maryland 20636 USA
//
//       Voice: (301) 373-9600
//       EMail: cups-info@cups.org
//         WWW: http://www.cups.org
//
// Contents:
//
//   main() - Main entry for filter...
//

//
// Include necessary headers...
//

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <cups/string.h>
#include <cups/i18n.h>
#include "parseargs.h"
#include "GString.h"
#include "gmem.h"
#include "Object.h"
#include "Stream.h"
#include "Array.h"
#include "Dict.h"
#include "XRef.h"
#include "Catalog.h"
#include "Page.h"
#include "PDFDoc.h"
#include "PSOutputDev.h"
#include "GlobalParams.h"
#include "Error.h"
#include "config.h"

#include <cups/cups.h>


//
// 'main()' - Main entry for filter...
//

int					// O - Exit status
main(int  argc,				// I - Number of command-line args
     char *argv[])			// I - Command-line arguments
{
  PDFDoc	*doc;			// Input file
  GString	*fileName;		// Input filename
  GString	*psFileName;		// Output filename
  PSLevel	level;			// PostScript level
  PSOutputDev	*psOut;			// Output device
  int		num_options;		// Number of options
  cups_option_t	*options;		// Options
  const char	*val;			// Option value
  ppd_file_t	*ppd;			// Current PPD
  ppd_size_t	*size;			// Current media size
  int		fd;			// Copy file descriptor
  const char	*server_root;		// Location of config files
  char		tempfile[1024];		// Temporary file
  char		buffer[8192];		// Copy buffer
  int		bytes;			// Bytes copied
  int		width, length;		// Size in points
  int		left, bottom, right, top;
					// Imageable area in points
  int		orientation;		// Orientation
  int		temp;			// Temporary var
  int		duplex;			// Duplex the output?
  int		fit;			// Fit the pages to the output
  int		printCommands;		// Output debug info for commands?


  // Make sure status messages are not buffered...
  setbuf(stderr, NULL);

  // Make sure we have the right number of arguments for CUPS!
  if (argc < 6 || argc > 7) {
    fprintf(stderr, _("Usage: %s job user title copies options [filename]\n"),
            argv[0]);
    return (1);
  }

  // Copy stdin if needed...
  if (argc == 6) {
    if ((fd = cupsTempFd(tempfile, sizeof(tempfile))) < 0) {
      perror(_("ERROR: Unable to copy PDF file"));
      return (1);
    }

    fprintf(stderr, "DEBUG: pdftops - copying to temp print file \"%s\"\n",
            tempfile);

    while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0)
      write(fd, buffer, bytes);
    close(fd);

    fileName = new GString(tempfile);
  } else {
    fileName = new GString(argv[6]);
    tempfile[0] = '\0';
  }

  // Default to "Universal" size - min of A4 and Letter...
  left   = 0;
  bottom = 0;
  right  = 595;
  top    = 792;
  width  = 595;
  length = 792;
  level  = psLevel2;
  duplex = 0;
  fit    = 0;

  // Get PPD and initialize options as needed...
  num_options = cupsParseOptions(argv[5], 0, &options);

  if ((ppd = ppdOpenFile(getenv("PPD"))) != NULL)
  {
    fprintf(stderr, "DEBUG: pdftops - opened PPD file \"%s\"...\n", getenv("PPD"));

    ppdMarkDefaults(ppd);
    cupsMarkOptions(ppd, num_options, options);

    if ((size = ppdPageSize(ppd, NULL)) != NULL)
    {
      left   = (int)size->left;
      bottom = (int)size->bottom;
      right  = (int)size->right;
      top    = (int)size->top;
      width  = (int)size->width;
      length = (int)size->length;
    }

    level = ppd->language_level == 1 ? psLevel1 : psLevel2;
  }

  // Track the orientation of the print job and update the page
  // dimensions and margins as needed...
  orientation = 0;

  if ((val = cupsGetOption("landscape", num_options, options)) != NULL)
  {
    if (strcasecmp(val, "no") != 0 && strcasecmp(val, "off") != 0 &&
        strcasecmp(val, "false") != 0)
      orientation = 1;
  }
  else if ((val = cupsGetOption("orientation-requested", num_options, options)) != NULL)
  {
   /*
    * Map IPP orientation values to 0 to 3:
    *
    *   3 = 0 degrees   = 0
    *   4 = 90 degrees  = 1
    *   5 = -90 degrees = 3
    *   6 = 180 degrees = 2
    */

    orientation = atoi(val) - 3;
    if (orientation >= 2)
      orientation ^= 1;
  }

  switch (orientation & 3)
  {
    case 0 : /* Portait */
        break;

    case 1 : /* Landscape */
	temp   = left;
	left   = bottom;
	bottom = temp;

	temp   = right;
	right  = top;
	top    = temp;

	temp   = width;
	width  = length;
	length = temp;
	break;

    case 2 : /* Reverse Portrait */
	temp   = width - left;
	left   = width - right;
	right  = temp;

	temp   = length - bottom;
	bottom = length - top;
	top    = temp;
        break;

    case 3 : /* Reverse Landscape */
	temp   = width - left;
	left   = width - right;
	right  = temp;

	temp   = length - bottom;
	bottom = length - top;
	top    = temp;

	temp   = left;
	left   = bottom;
	bottom = temp;

	temp   = right;
	right  = top;
	top    = temp;

	temp   = width;
	width  = length;
	length = temp;
	break;
  }

  if ((val = cupsGetOption("debug", num_options, options)) != NULL &&
      strcasecmp(val, "no") && strcasecmp(val, "off") &&
      strcasecmp(val, "false"))
    printCommands = 1;
  else
    printCommands = 0;

  if ((val = cupsGetOption("fitplot", num_options, options)) != NULL &&
      strcasecmp(val, "no") && strcasecmp(val, "off") &&
      strcasecmp(val, "false"))
    fit = 1;

  if ((val = cupsGetOption("sides", num_options, options)) != NULL &&
      strncasecmp(val, "two-", 4) == 0)
    duplex = 1;
  else if ((val = cupsGetOption("Duplex", num_options, options)) != NULL &&
           strncasecmp(val, "Duplex", 6) == 0)
    duplex = 1;
  else if ((val = cupsGetOption("JCLDuplex", num_options, options)) != NULL &&
           strncasecmp(val, "Duplex", 6) == 0)
    duplex = 1;
  else if ((val = cupsGetOption("EFDuplex", num_options, options)) != NULL &&
           strncasecmp(val, "Duplex", 6) == 0)
    duplex = 1;
  else if ((val = cupsGetOption("KD03Duplex", num_options, options)) != NULL &&
           strncasecmp(val, "Duplex", 6) == 0)
    duplex = 1;
  else if (ppdIsMarked(ppd, "Duplex", "DuplexNoTumble") ||
           ppdIsMarked(ppd, "Duplex", "DuplexTumble") ||
	   ppdIsMarked(ppd, "JCLDuplex", "DuplexNoTumble") ||
           ppdIsMarked(ppd, "JCLDuplex", "DuplexTumble") ||
	   ppdIsMarked(ppd, "EFDuplex", "DuplexNoTumble") ||
           ppdIsMarked(ppd, "EFDuplex", "DuplexTumble") ||
	   ppdIsMarked(ppd, "KD03Duplex", "DuplexNoTumble") ||
           ppdIsMarked(ppd, "KD03Duplex", "DuplexTumble"))
    duplex = 1;

  if (ppd != NULL)
    ppdClose(ppd);

  fprintf(stderr, "DEBUG: pdftops - level = %d, width = %d, length = %d\n",
          level, width, length);

  // read config file
  if ((server_root = getenv("CUPS_SERVERROOT")) == NULL)
    server_root = CUPS_SERVERROOT;

  snprintf(buffer, sizeof(buffer), "%s/pdftops.conf", server_root);

  globalParams = new GlobalParams(buffer);

  if (fit || globalParams->getPSPaperWidth() > 0)
  {
    // Only set paper size and area if we are fitting to the job's
    // page size or the pdftops.conf file does not contain
    // "psPaperSize match"...
    globalParams->setPSPaperWidth(width);
    globalParams->setPSPaperHeight(length);
    globalParams->setPSImageableArea(left, bottom, right, top);
  }

  globalParams->setPSDuplex(duplex);
  globalParams->setPSExpandSmaller(fit);
  globalParams->setPSShrinkLarger(fit);
  globalParams->setPSLevel(level);
  globalParams->setPSASCIIHex(level == psLevel1);
  globalParams->setPSEmbedType1(1);
  globalParams->setPSEmbedTrueType(1);
  globalParams->setPSEmbedCIDPostScript(1);
  globalParams->setErrQuiet(0);
  globalParams->setPrintCommands(printCommands);

  if (printCommands)
    setbuf(stdout, NULL);

  // open PDF file
  doc = new PDFDoc(fileName, NULL, NULL);

  // check for print permission
  if (doc->isOk() && doc->okToPrint())
  {
    // CUPS always writes to stdout...
    psFileName = new GString("-");

    // write PostScript file
    psOut = new PSOutputDev(psFileName->getCString(), doc->getXRef(),
                            doc->getCatalog(), 1, doc->getNumPages(),
			    psModePS, 0, 0, 0, 0, gFalse,
			    cupsGetOption("page-ranges", num_options, options));
    if (psOut->isOk())
      doc->displayPages(psOut, 1, doc->getNumPages(), 72.0, 72.0, 0,
                        gTrue, gFalse, gFalse);
    delete psOut;

    // clean up
    delete psFileName;
  }
  else
  {
    error(-1, "Unable to print this document.");
  }

  cupsFreeOptions(num_options, options);

  delete doc;
  delete globalParams;

  // check for memory leaks
  Object::memCheck(stderr);
  gMemReport(stderr);

  // Remove temp file if needed...
  if (tempfile[0])
    unlink(tempfile);

  return 0;
}


//
// End of "$Id: pdftops.cxx 6421 2007-03-30 20:02:19Z mike $".
//