]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/pdftops.c
4 * PDF to PostScript filter front-end for the Common UNIX Printing
7 * Copyright 2007-2008 by Apple Inc.
8 * Copyright 1997-2006 by Easy Software Products.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
18 * main() - Main entry for filter...
19 * cancel_job() - Flag the job as canceled.
23 * Include necessary headers...
26 #include <cups/cups.h>
27 #include <cups/string.h>
28 #include <cups/i18n.h>
37 static void cancel_job(int sig
);
41 * 'main()' - Main entry for filter...
44 int /* O - Exit status */
45 main(int argc
, /* I - Number of command-line args */
46 char *argv
[]) /* I - Command-line arguments */
48 int fd
; /* Copy file descriptor */
49 char *filename
, /* PDF file to convert */
50 tempfile
[1024]; /* Temporary file */
51 char buffer
[8192]; /* Copy buffer */
52 int bytes
; /* Bytes copied */
53 int num_options
; /* Number of options */
54 cups_option_t
*options
; /* Options */
55 const char *val
; /* Option value */
56 int orientation
; /* Output orientation */
57 ppd_file_t
*ppd
; /* PPD file */
58 ppd_size_t
*size
; /* Current page size */
59 int pdfpid
, /* Process ID for pdftops */
60 pdfstatus
, /* Status from pdftops */
61 pdfargc
; /* Number of args for pdftops */
62 char *pdfargv
[100], /* Arguments for pdftops */
63 pdfwidth
[255], /* Paper width */
64 pdfheight
[255]; /* Paper height */
65 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
66 struct sigaction action
; /* Actions for POSIX signals */
67 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
71 * Make sure status messages are not buffered...
77 * Make sure we have the right number of arguments for CUPS!
80 if (argc
< 6 || argc
> 7)
82 _cupsLangPrintf(stderr
,
83 _("Usage: %s job user title copies options [filename]\n"),
89 * Register a signal handler to cleanly cancel a job.
92 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
93 sigset(SIGTERM
, cancel_job
);
94 #elif defined(HAVE_SIGACTION)
95 memset(&action
, 0, sizeof(action
));
97 sigemptyset(&action
.sa_mask
);
98 action
.sa_handler
= cancel_job
;
99 sigaction(SIGTERM
, &action
, NULL
);
101 signal(SIGTERM
, cancel_job
);
102 #endif /* HAVE_SIGSET */
105 * Copy stdin if needed...
111 * Copy stdin to a temp file...
114 if ((fd
= cupsTempFd(tempfile
, sizeof(tempfile
))) < 0)
116 _cupsLangPrintError(_("ERROR: Unable to copy PDF file"));
120 fprintf(stderr
, "DEBUG: pdftops - copying to temp print file \"%s\"\n",
123 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
124 write(fd
, buffer
, bytes
);
133 * Use the filename on the command-line...
141 * Load the PPD file and mark options...
144 ppd
= ppdOpenFile(getenv("PPD"));
145 num_options
= cupsParseOptions(argv
[5], 0, &options
);
147 ppdMarkDefaults(ppd
);
148 cupsMarkOptions(ppd
, num_options
, options
);
151 * Build the command-line for the pdftops filter...
154 pdfargv
[0] = (char *)"pdftops";
160 * Set language level and TrueType font handling...
163 if (ppd
->language_level
== 1)
165 pdfargv
[pdfargc
++] = (char *)"-level1";
166 pdfargv
[pdfargc
++] = (char *)"-noembtt";
168 else if (ppd
->language_level
== 2)
170 pdfargv
[pdfargc
++] = (char *)"-level2";
171 if (!ppd
->ttrasterizer
)
172 pdfargv
[pdfargc
++] = (char *)"-noembtt";
175 pdfargv
[pdfargc
++] = (char *)"-level3";
178 * Set output page size...
181 size
= ppdPageSize(ppd
, NULL
);
185 * Got the size, now get the orientation...
190 if ((val
= cupsGetOption("landscape", num_options
, options
)) != NULL
)
192 if (strcasecmp(val
, "no") != 0 && strcasecmp(val
, "off") != 0 &&
193 strcasecmp(val
, "false") != 0)
196 else if ((val
= cupsGetOption("orientation-requested", num_options
, options
)) != NULL
)
199 * Map IPP orientation values to 0 to 3:
203 * 5 = -90 degrees = 3
204 * 6 = 180 degrees = 2
207 orientation
= atoi(val
) - 3;
208 if (orientation
>= 2)
214 snprintf(pdfwidth
, sizeof(pdfwidth
), "%.1f", size
->length
);
215 snprintf(pdfheight
, sizeof(pdfheight
), "%.1f", size
->width
);
219 snprintf(pdfwidth
, sizeof(pdfwidth
), "%.1f", size
->width
);
220 snprintf(pdfheight
, sizeof(pdfheight
), "%.1f", size
->length
);
223 pdfargv
[pdfargc
++] = (char *)"-paperw";
224 pdfargv
[pdfargc
++] = pdfwidth
;
225 pdfargv
[pdfargc
++] = (char *)"-paperh";
226 pdfargv
[pdfargc
++] = pdfheight
;
230 if ((val
= cupsGetOption("fitplot", num_options
, options
)) != NULL
&&
231 strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
232 strcasecmp(val
, "false"))
233 pdfargv
[pdfargc
++] = (char *)"-expand";
235 pdfargv
[pdfargc
++] = filename
;
236 pdfargv
[pdfargc
++] = (char *)"-";
237 pdfargv
[pdfargc
] = NULL
;
239 if ((pdfpid
= fork()) == 0)
242 * Child comes here...
245 execv(CUPS_PDFTOPS
, argv
);
246 _cupsLangPrintError(_("ERROR: Unable to execute pdftops filter"));
255 _cupsLangPrintError(_("ERROR: Unable to execute pdftops filter"));
261 * Parent comes here...
264 if (wait(&pdfstatus
) != pdfpid
)
266 kill(pdfpid
, SIGTERM
);
271 if (WIFEXITED(pdfstatus
))
273 pdfstatus
= WEXITSTATUS(pdfstatus
);
275 _cupsLangPrintf(stderr
,
276 _("ERROR: pdftops filter exited with status %d!"),
281 pdfstatus
= WTERMSIG(pdfstatus
);
283 _cupsLangPrintf(stderr
,
284 _("ERROR: pdftops filter crashed on signal %d!"),
291 * Cleanup and exit...
302 * 'cancel_job()' - Flag the job as canceled.
306 cancel_job(int sig
) /* I - Signal number (unused) */