#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>
int main(int argc, char *argv[]) {
PDFDoc *doc; GString *fileName; GString *psFileName; PSLevel level; PSOutputDev *psOut; int num_options; cups_option_t *options; const char *val; ppd_file_t *ppd; ppd_size_t *size; int fd; const char *server_root; char tempfile[1024]; char buffer[8192]; int bytes; int width, length; int orientation; int temp; int duplex; int fit; int printCommands;
setbuf(stderr, NULL);
if (argc < 6 || argc > 7) {
fprintf(stderr, _("Usage: %s job user title copies options [filename]\n"),
argv[0]);
return (1);
}
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';
}
width = 595;
length = 792;
level = psLevel2;
duplex = 0;
fit = 0;
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)
{
width = (int)size->width;
length = (int)size->length;
}
level = ppd->language_level == 1 ? psLevel1 : psLevel2;
}
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)
{
orientation = atoi(val) - 3;
if (orientation >= 2)
orientation ^= 1;
}
switch (orientation & 3)
{
case 0 :
case 2 :
break;
case 1 :
case 3 :
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);
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)
{
fprintf(stderr, "DEBUG: Setting paper dimensions to %dx%d!\n", width,
length);
globalParams->setPSPaperWidth(width);
globalParams->setPSPaperHeight(length);
globalParams->setPSImageableArea(0, 0, width, length);
}
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);
doc = new PDFDoc(fileName, NULL, NULL);
if (doc->isOk() && doc->okToPrint())
{
psFileName = new GString("-");
psOut = new PSOutputDev(psFileName->getCString(), doc->getXRef(),
doc->getCatalog(), 1, doc->getNumPages(),
psModePS, 0, 0, 0, 0, gFalse, NULL);
if (psOut->isOk())
doc->displayPages(psOut, 1, doc->getNumPages(), 72.0, 72.0, 0,
gTrue, gFalse, gFalse);
delete psOut;
delete psFileName;
}
else
{
error(-1, "Unable to print this document.");
}
cupsFreeOptions(num_options, options);
delete doc;
delete globalParams;
Object::memCheck(stderr);
gMemReport(stderr);
if (tempfile[0])
unlink(tempfile);
return 0;
}