]>
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>
38 static void cancel_job(int sig
);
45 static int job_canceled
= 0;
49 * 'main()' - Main entry for filter...
52 int /* O - Exit status */
53 main(int argc
, /* I - Number of command-line args */
54 char *argv
[]) /* I - Command-line arguments */
56 int fd
; /* Copy file descriptor */
57 char *filename
, /* PDF file to convert */
58 tempfile
[1024]; /* Temporary file */
59 char buffer
[8192]; /* Copy buffer */
60 int bytes
; /* Bytes copied */
61 int num_options
; /* Number of options */
62 cups_option_t
*options
; /* Options */
63 const char *val
; /* Option value */
64 int orientation
; /* Output orientation */
65 ppd_file_t
*ppd
; /* PPD file */
66 ppd_size_t
*size
; /* Current page size */
67 int pdfpid
, /* Process ID for pdftops */
68 pdfwaitpid
, /* Process ID from wait() */
69 pdfstatus
, /* Status from pdftops */
70 pdfargc
; /* Number of args for pdftops */
71 char *pdfargv
[100], /* Arguments for pdftops/gs */
73 pdfwidth
[255], /* Paper width */
74 pdfheight
[255]; /* Paper height */
76 pdfgeometry
[255]; /* Paper width and height */
77 #endif /* HAVE_PDFTOPS */
78 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
79 struct sigaction action
; /* Actions for POSIX signals */
80 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
84 * Make sure status messages are not buffered...
90 * Make sure we have the right number of arguments for CUPS!
93 if (argc
< 6 || argc
> 7)
95 _cupsLangPrintf(stderr
,
96 _("Usage: %s job user title copies options [filename]\n"),
102 * Register a signal handler to cleanly cancel a job.
105 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
106 sigset(SIGTERM
, cancel_job
);
107 #elif defined(HAVE_SIGACTION)
108 memset(&action
, 0, sizeof(action
));
110 sigemptyset(&action
.sa_mask
);
111 action
.sa_handler
= cancel_job
;
112 sigaction(SIGTERM
, &action
, NULL
);
114 signal(SIGTERM
, cancel_job
);
115 #endif /* HAVE_SIGSET */
118 * Copy stdin if needed...
124 * Copy stdin to a temp file...
127 if ((fd
= cupsTempFd(tempfile
, sizeof(tempfile
))) < 0)
129 _cupsLangPrintError(_("ERROR: Unable to copy PDF file"));
133 fprintf(stderr
, "DEBUG: pdftops - copying to temp print file \"%s\"\n",
136 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
137 write(fd
, buffer
, bytes
);
146 * Use the filename on the command-line...
154 * Load the PPD file and mark options...
157 ppd
= ppdOpenFile(getenv("PPD"));
158 num_options
= cupsParseOptions(argv
[5], 0, &options
);
160 ppdMarkDefaults(ppd
);
161 cupsMarkOptions(ppd
, num_options
, options
);
164 * Build the command-line for the pdftops or gs filter...
168 pdfargv
[0] = (char *)"pdftops";
171 pdfargv
[0] = (char *)"gs";
172 pdfargv
[1] = (char *)"-q";
173 pdfargv
[2] = (char *)"-dNOPAUSE";
174 pdfargv
[3] = (char *)"-dBATCH";
175 pdfargv
[4] = (char *)"-dSAFER";
176 pdfargv
[5] = (char *)"-sDEVICE=pswrite";
177 pdfargv
[6] = (char *)"-sOUTPUTFILE=%stdout";
179 #endif /* HAVE_PDFTOPS */
184 * Set language level and TrueType font handling...
187 if (ppd
->language_level
== 1)
190 pdfargv
[pdfargc
++] = (char *)"-level1";
191 pdfargv
[pdfargc
++] = (char *)"-noembtt";
193 pdfargv
[pdfargc
++] = (char *)"-dLanguageLevel=1";
194 #endif /* HAVE_PDFTOPS */
196 else if (ppd
->language_level
== 2)
199 pdfargv
[pdfargc
++] = (char *)"-level2";
200 if (!ppd
->ttrasterizer
)
201 pdfargv
[pdfargc
++] = (char *)"-noembtt";
203 pdfargv
[pdfargc
++] = (char *)"-dLanguageLevel=2";
204 #endif /* HAVE_PDFTOPS */
208 pdfargv
[pdfargc
++] = (char *)"-level3";
210 pdfargv
[pdfargc
++] = (char *)"-dLanguageLevel=3";
211 #endif /* HAVE_PDFTOPS */
214 * Set output page size...
217 size
= ppdPageSize(ppd
, NULL
);
221 * Got the size, now get the orientation...
226 if ((val
= cupsGetOption("landscape", num_options
, options
)) != NULL
)
228 if (strcasecmp(val
, "no") != 0 && strcasecmp(val
, "off") != 0 &&
229 strcasecmp(val
, "false") != 0)
232 else if ((val
= cupsGetOption("orientation-requested", num_options
, options
)) != NULL
)
235 * Map IPP orientation values to 0 to 3:
239 * 5 = -90 degrees = 3
240 * 6 = 180 degrees = 2
243 orientation
= atoi(val
) - 3;
244 if (orientation
>= 2)
251 snprintf(pdfwidth
, sizeof(pdfwidth
), "%.0f", size
->length
);
252 snprintf(pdfheight
, sizeof(pdfheight
), "%.0f", size
->width
);
256 snprintf(pdfwidth
, sizeof(pdfwidth
), "%.0f", size
->width
);
257 snprintf(pdfheight
, sizeof(pdfheight
), "%.0f", size
->length
);
260 pdfargv
[pdfargc
++] = (char *)"-paperw";
261 pdfargv
[pdfargc
++] = pdfwidth
;
262 pdfargv
[pdfargc
++] = (char *)"-paperh";
263 pdfargv
[pdfargc
++] = pdfheight
;
266 snprintf(pdfgeometry
, sizeof(pdfgeometry
), "-g%.0fx%.0f", size
->length
,
269 snprintf(pdfgeometry
, sizeof(pdfgeometry
), "-g%.0fx%.0f", size
->width
,
272 pdfargv
[pdfargc
++] = pdfgeometry
;
273 #endif /* HAVE_PDFTOPS */
278 if ((val
= cupsGetOption("fitplot", num_options
, options
)) != NULL
&&
279 strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
280 strcasecmp(val
, "false"))
281 pdfargv
[pdfargc
++] = (char *)"-expand";
283 pdfargv
[pdfargc
++] = filename
;
284 pdfargv
[pdfargc
++] = (char *)"-";
286 pdfargv
[pdfargc
++] = (char *)"-c";
287 pdfargv
[pdfargc
++] = (char *)"save pop";
288 pdfargv
[pdfargc
++] = (char *)"-f";
289 pdfargv
[pdfargc
++] = filename
;
290 #endif /* HAVE_PDFTOPS */
292 pdfargv
[pdfargc
] = NULL
;
294 if ((pdfpid
= fork()) == 0)
297 * Child comes here...
301 execv(CUPS_PDFTOPS
, pdfargv
);
302 _cupsLangPrintError(_("ERROR: Unable to execute pdftops program"));
304 execv(CUPS_GHOSTSCRIPT
, pdfargv
);
305 _cupsLangPrintError(_("ERROR: Unable to execute gs program"));
306 #endif /* HAVE_PDFTOPS */
317 _cupsLangPrintError(_("ERROR: Unable to execute pdftops program"));
319 _cupsLangPrintError(_("ERROR: Unable to execute gs program"));
320 #endif /* HAVE_PDFTOPS */
327 * Parent comes here...
330 while ((pdfwaitpid
= wait(&pdfstatus
)) != pdfpid
&& errno
== EINTR
)
333 * Wait until we get a valid process ID or the job is canceled...
338 kill(pdfpid
, SIGTERM
);
345 if (WIFEXITED(pdfstatus
))
347 pdfstatus
= WEXITSTATUS(pdfstatus
);
349 _cupsLangPrintf(stderr
,
350 _("ERROR: pdftops filter exited with status %d!\n"),
355 pdfstatus
= WTERMSIG(pdfstatus
);
357 _cupsLangPrintf(stderr
,
358 _("ERROR: pdftops filter crashed on signal %d!\n"),
365 * Cleanup and exit...
376 * 'cancel_job()' - Flag the job as canceled.
380 cancel_job(int sig
) /* I - Signal number (unused) */