new parameter addspacechars
[swftools.git] / lib / pdf / pdf2jpeg.c
1 /* pdf2jpeg.c
2    Converts a pdf page to a jpeg.
3
4    Copyright (c) 2010 Matthias Kramm
5    Copyright (c) 1998-2009 Derek Noonburg
6
7    Swftools is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    Swftools is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with swftools; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
20
21 #include <aconf.h>
22 #include <stdio.h>
23 #include "parseargs.h"
24 #include "gmem.h"
25 #include "GString.h"
26 #include "GlobalParams.h"
27 #include "Object.h"
28 #include "PDFDoc.h"
29 #include "SplashBitmap.h"
30 #include "Splash.h"
31 #include "SplashOutputDev.h"
32 #include "config.h"
33 #include "../jpeg.h"
34
35 static int page = 1;
36 static int width = 0;
37 static int resolution = 150;
38 static int quality = 95;
39 static char ownerPassword[33] = "";
40 static char userPassword[33] = "";
41 static char cfgFileName[256] = "";
42 static GBool printVersion = gFalse;
43 static GBool printHelp = gFalse;
44 static char output[256] = "output.jpg";
45
46 static ArgDesc argDesc[] = {
47   {"-p",      argInt,      &page,     0,
48    "first page to print"},
49   {"-r",      argInt,      &resolution,    0,
50    "resolution, in DPI (default is 150)"},
51   {"-q",      argInt,      &quality,    0,
52    "jpeg conversion quality"},
53   {"-w",      argInt,      &width,    0,
54    "zoom to this width"},
55   {"-o",      argString,   &output,    sizeof(output),
56    "resolution, in DPI (default is 150)"},
57   {"-opw",    argString,   ownerPassword,  sizeof(ownerPassword),
58    "owner password (for encrypted files)"},
59   {"-upw",    argString,   userPassword,   sizeof(userPassword),
60    "user password (for encrypted files)"},
61   {"-cfg",        argString,      cfgFileName,    sizeof(cfgFileName),
62    "configuration file to use in place of .xpdfrc"},
63   {"-v",      argFlag,     &printVersion,  0,
64    "print copyright and version info"},
65   {"-h",      argFlag,     &printHelp,     0,
66    "print usage information"},
67   {"-help",   argFlag,     &printHelp,     0,
68    "print usage information"},
69   {"--help",  argFlag,     &printHelp,     0,
70    "print usage information"},
71   {"-?",      argFlag,     &printHelp,     0,
72    "print usage information"},
73   {NULL}
74 };
75
76 int main(int argc, char *argv[]) {
77   PDFDoc *doc;
78   GString *fileName;
79   GString *ownerPW, *userPW;
80   SplashColor paperColor;
81   SplashOutputDev *splashOut;
82   GBool ok;
83   int exitCode;
84   int pg;
85
86   exitCode = 99;
87
88   // parse args
89   ok = parseArgs(argDesc, &argc, argv);
90   
91   if (!ok || argc != 2 || printVersion || printHelp) {
92     fprintf(stderr, "pdf2jpeg version %s\n", xpdfVersion);
93     fprintf(stderr, "%s\n", xpdfCopyright);
94     if (!printVersion) {
95       printUsage("pdf2jpeg", "<PDF-file> -o <jpegfile>", argDesc);
96     }
97     goto err0;
98   }
99   fileName = new GString(argv[1]);
100
101   // read config file
102   globalParams = new GlobalParams(cfgFileName);
103   globalParams->setupBaseFonts(NULL);
104   
105   // open PDF file
106   if (ownerPassword[0]) {
107     ownerPW = new GString(ownerPassword);
108   } else {
109     ownerPW = NULL;
110   }
111   if (userPassword[0]) {
112     userPW = new GString(userPassword);
113   } else {
114     userPW = NULL;
115   }
116   doc = new PDFDoc(fileName, ownerPW, userPW);
117   if (userPW) {
118     delete userPW;
119   }
120   if (ownerPW) {
121     delete ownerPW;
122   }
123   if (!doc->isOk()) {
124     exitCode = 1;
125     goto err1;
126   }
127
128   paperColor[0] = paperColor[1] = paperColor[2] = 0xff;
129   splashOut = new SplashOutputDev(splashModeRGB8, 1, gFalse, paperColor);
130   
131   splashOut->startDoc(doc->getXRef());
132
133   if(page>=1 && page<=doc->getNumPages()) {
134       double r = resolution;
135       if(width) {
136         int old_width = doc->getPageCropWidth(page);
137         r = 72.0*width/old_width;
138       }
139       doc->displayPage(splashOut, page, r, r, 0, gFalse, gTrue, gFalse);
140       SplashBitmap*bitmap = splashOut->getBitmap();
141       if(bitmap) {
142         Guchar*rgb = bitmap->getDataPtr();
143         int width = bitmap->getWidth();
144         int height = bitmap->getHeight();
145         jpeg_save(rgb, width, height, quality, output);
146       }
147   }
148   delete splashOut;
149
150   exitCode = 0;
151
152   // clean up
153  err1:
154   delete doc;
155   delete globalParams;
156  err0:
157
158   // check for memory leaks
159   Object::memCheck(stderr);
160   gMemReport(stderr);
161
162   return exitCode;
163 }