]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/pdftops.cc
drag and drop, removed redundant verb popup code.
[evince.git] / pdf / xpdf / pdftops.cc
1 //========================================================================
2 //
3 // pdftops.cc
4 //
5 // Copyright 1996 Derek B. Noonburg
6 //
7 //========================================================================
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stddef.h>
12 #include <string.h>
13 #include "parseargs.h"
14 #include "GString.h"
15 #include "gmem.h"
16 #include "Object.h"
17 #include "Stream.h"
18 #include "Array.h"
19 #include "Dict.h"
20 #include "XRef.h"
21 #include "Catalog.h"
22 #include "Page.h"
23 #include "PDFDoc.h"
24 #include "PSOutputDev.h"
25 #include "Params.h"
26 #include "Error.h"
27 #include "config.h"
28
29 static int firstPage = 1;
30 static int lastPage = 0;
31 static GBool noEmbedFonts = gFalse;
32 static GBool doForm = gFalse;
33 GBool printCommands = gFalse;
34 static GBool printHelp = gFalse;
35
36 static ArgDesc argDesc[] = {
37   {"-f",      argInt,      &firstPage,     0,
38    "first page to print"},
39   {"-l",      argInt,      &lastPage,      0,
40    "last page to print"},
41   {"-paperw", argInt,      &paperWidth,    0,
42    "paper width, in points"},
43   {"-paperh", argInt,      &paperHeight,   0,
44    "paper height, in points"},
45   {"-level1", argFlag,     &psOutLevel1,   0,
46    "generate Level 1 PostScript"},
47   {"-noemb",  argFlag,     &noEmbedFonts,  0,
48    "don't embed Type 1 fonts"},
49   {"-form",   argFlag,     &doForm,        0,
50    "generate a PostScript form"},
51   {"-q",      argFlag,     &errQuiet,      0,
52    "don't print any messages or errors"},
53   {"-h",      argFlag,     &printHelp,     0,
54    "print usage information"},
55   {"-help",   argFlag,     &printHelp,     0,
56    "print usage information"},
57   {NULL}
58 };
59
60 int main(int argc, char *argv[]) {
61   PDFDoc *doc;
62   GString *fileName;
63   GString *psFileName;
64   PSOutputDev *psOut;
65   GBool ok;
66   char *p;
67
68   // parse args
69   ok = parseArgs(argDesc, &argc, argv);
70   if (!ok || argc < 2 || argc > 3 || printHelp) {
71     fprintf(stderr, "pdftops version %s\n", xpdfVersion);
72     fprintf(stderr, "%s\n", xpdfCopyright);
73     printUsage("pdftops", "<PDF-file> [<PS-file>]", argDesc);
74     exit(1);
75   }
76   if (doForm && psOutLevel1) {
77     fprintf(stderr, "Error: forms are only available with Level 2 output.\n");
78     exit(1);
79   }
80   fileName = new GString(argv[1]);
81
82   // init error file
83   errorInit();
84
85   // read config file
86   initParams(xpdfConfigFile);
87
88   // open PDF file
89   xref = NULL;
90   doc = new PDFDoc(fileName);
91   if (!doc->isOk()) {
92     goto err1;
93   }
94
95   // check for print permission
96   if (!doc->okToPrint()) {
97     error(-1, "Printing this document is not allowed.");
98     goto err2;
99   }
100
101   // construct PostScript file name
102   if (argc == 3) {
103     psFileName = new GString(argv[2]);
104   } else {
105     p = fileName->getCString() + fileName->getLength() - 4;
106     if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF"))
107       psFileName = new GString(fileName->getCString(),
108                                fileName->getLength() - 4);
109     else
110       psFileName = fileName->copy();
111     psFileName->append(".ps");
112   }
113
114   // get page range
115   if (firstPage < 1)
116     firstPage = 1;
117   if (lastPage < 1 || lastPage > doc->getNumPages())
118     lastPage = doc->getNumPages();
119   if (doForm)
120     lastPage = firstPage;
121
122   // write PostScript file
123   psOut = new PSOutputDev(psFileName->getCString(), doc->getCatalog(),
124                           firstPage, lastPage, !noEmbedFonts, doForm);
125   if (psOut->isOk())
126     doc->displayPages(psOut, firstPage, lastPage, 72, 0);
127   delete psOut;
128
129   // clean up
130   delete psFileName;
131  err2:
132   delete doc;
133  err1:
134   freeParams();
135
136   // check for memory leaks
137   Object::memCheck(stderr);
138   gMemReport(stderr);
139
140   return 0;
141 }