]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/pdftops.cc
Totaly re-hash stream architecture ... again :-)
[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   FILE *file;
65   PSOutputDev *psOut;
66   GBool ok;
67   char *p;
68
69   // parse args
70   ok = parseArgs(argDesc, &argc, argv);
71   if (!ok || argc < 2 || argc > 3 || printHelp) {
72     fprintf(stderr, "pdftops version %s\n", xpdfVersion);
73     fprintf(stderr, "%s\n", xpdfCopyright);
74     printUsage("pdftops", "<PDF-file> [<PS-file>]", argDesc);
75     exit(1);
76   }
77   if (doForm && psOutLevel1) {
78     fprintf(stderr, "Error: forms are only available with Level 2 output.\n");
79     exit(1);
80   }
81   fileName = new GString(argv[1]);
82
83   // init error file
84   errorInit();
85
86   // read config file
87   initParams(xpdfConfigFile);
88
89   // open PDF file
90   xref = NULL;
91   doc = new PDFDoc(new FileStream (fileOpen (fileName)), fileName);
92   if (!doc->isOk()) {
93     goto err1;
94   }
95
96   // check for print permission
97   if (!doc->okToPrint()) {
98     error(-1, "Printing this document is not allowed.");
99     goto err2;
100   }
101
102   // construct PostScript file name
103   if (argc == 3) {
104     psFileName = new GString(argv[2]);
105   } else {
106     p = fileName->getCString() + fileName->getLength() - 4;
107     if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF"))
108       psFileName = new GString(fileName->getCString(),
109                                fileName->getLength() - 4);
110     else
111       psFileName = fileName->copy();
112     psFileName->append(".ps");
113   }
114
115   // get page range
116   if (firstPage < 1)
117     firstPage = 1;
118   if (lastPage < 1 || lastPage > doc->getNumPages())
119     lastPage = doc->getNumPages();
120   if (doForm)
121     lastPage = firstPage;
122
123   // write PostScript file
124   psOut = new PSOutputDev(psFileName->getCString(), doc->getCatalog(),
125                           firstPage, lastPage, !noEmbedFonts, doForm);
126   if (psOut->isOk())
127     doc->displayPages(psOut, firstPage, lastPage, 72, 0);
128   delete psOut;
129
130   // clean up
131   delete psFileName;
132  err2:
133   delete doc;
134  err1:
135   freeParams();
136
137   // check for memory leaks
138   Object::memCheck(stderr);
139   gMemReport(stderr);
140
141   return 0;
142 }