added pdf2pdf tool
[swftools.git] / src / pdf2pdf.c
1 /* pdf2pdf.c
2    main routine for pdf2pdf(1)
3
4    Part of the swftools package.
5    
6    Copyright (c) 2009 Matthias Kramm <kramm@quiss.org> 
7  
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include "../config.h"
28 #include "../lib/args.h"
29 #include "../lib/os.h"
30 #include "../lib/gfxsource.h"
31 #include "../lib/gfxdevice.h"
32 #include "../lib/gfxpoly.h"
33 #include "../lib/devices/rescale.h"
34 #include "../lib/devices/polyops.h"
35 #include "../lib/devices/pdf.h"
36 #include "../lib/readers/image.h"
37 #include "../lib/readers/swf.h"
38 #include "../lib/pdf/pdf.h"
39 #include "../lib/log.h"
40
41 static gfxsource_t*driver = 0;
42
43 static char * outputname = 0;
44 static int loglevel = 3;
45 static char * pagerange = 0;
46 static char * filename = 0;
47 static const char * format = "ocr";
48
49 int args_callback_option(char*name,char*val) {
50     if (!strcmp(name, "o"))
51     {
52         outputname = val;
53         return 1;
54     }
55     else if (!strcmp(name, "v"))
56     {
57         loglevel ++;
58         setConsoleLogging(loglevel);
59         return 0;
60     }
61     else if (!strcmp(name, "f"))
62     {
63         format = val;
64         return 1;
65     }
66     else if (!strcmp(name, "q"))
67     {
68         loglevel --;
69         setConsoleLogging(loglevel);
70         return 0;
71     }
72     else if (name[0]=='p')
73     {
74         do {
75             name++;
76         } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
77
78         if(*name) {
79             pagerange = name;
80             return 0;
81         } 
82         pagerange = val;        
83         return 1;
84     }
85     else if (!strcmp(name, "s"))
86     {
87         if(!driver) {
88             fprintf(stderr, "Specify input file before -s\n");
89             exit(1);
90         }
91         char*s = strdup(val);
92         char*c = strchr(s, '=');
93         if(c && *c && c[1])  {
94             *c = 0;
95             c++;
96             driver->set_parameter(driver, s,c);
97         } else {
98             driver->set_parameter(driver, s,"1");
99         }
100         free(s);
101         return 1;
102     }
103     else if (!strcmp(name, "V"))
104     {   
105         printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
106         exit(0);
107     }
108     else 
109     {
110         fprintf(stderr, "Unknown option: -%s\n", name);
111         exit(1);
112     }
113     return 0;
114 }
115
116 struct options_t options[] =
117 {{"o","output"},
118  {"q","quiet"},
119  {"V","version"},
120  {"s","set"},
121  {"p","pages"},
122  {0,0}
123 };
124
125 int args_callback_longoption(char*name,char*val) {
126     return args_long2shortoption(options, name, val);
127 }
128
129 int args_callback_command(char*name, char*val) {
130     if (!filename) {
131
132         filename = name;
133
134         if(strstr(filename, ".pdf") || strstr(filename, ".PDF")) {
135             msg("<verbose> Treating file as PDF");
136             driver = gfxsource_pdf_create();
137         } else if(strstr(filename, ".swf") || strstr(filename, ".SWF")) {
138             msg("<verbose> Treating file as SWF");
139             driver = gfxsource_swf_create();
140         } else if(strstr(filename, ".jpg") || strstr(filename, ".JPG") ||
141                   strstr(filename, ".png") || strstr(filename, ".PNG")) {
142             msg("<verbose> Treating file as Image");
143             driver = gfxsource_image_create();
144         } else {
145             driver = gfxsource_pdf_create();
146         }
147     } else {
148         if(outputname)
149         {
150              fprintf(stderr, "Error: Do you want the output to go to %s or to %s?", 
151                      outputname, name);
152              exit(1);
153         }
154         outputname = name;
155     }
156     return 0;
157 }
158
159 void args_callback_usage(char*name)
160 {
161 }
162
163 int main(int argn, char *argv[])
164 {
165     processargs(argn, argv);
166     initLog(0,-1,0,0,-1,loglevel);
167     
168     if(!filename) {
169         fprintf(stderr, "Please specify an input file\n");
170         exit(1);
171     }
172     
173     if(!outputname)
174     {
175         if(filename) {
176             outputname = stripFilename(filename, ".print.pdf");
177             msg("<notice> Output filename not given. Writing to %s", outputname);
178         } 
179     }
180     if(!outputname)
181     {
182         fprintf(stderr, "Please use -o to specify an output file\n");
183         exit(1);
184     }
185
186     is_in_range(0x7fffffff, pagerange);
187     if(pagerange)
188         driver->set_parameter(driver, "pages", pagerange);
189
190     if(!filename) {
191         args_callback_usage(argv[0]);
192         exit(0);
193     }
194
195     gfxdocument_t* doc = driver->open(driver, filename);
196     doc->set_parameter(doc, "drawonlyshapes", "1");
197     doc->set_parameter(doc, "disable_polygon_conversion", "1");
198
199     if(!doc) {
200         msg("<error> Couldn't open %s", filename);
201         exit(1);
202     }
203
204     gfxdevice_t _out,*out=&_out;
205     gfxdevice_pdf_init(out);
206
207     /*gfxdevice_t wrap;
208     gfxdevice_removeclippings_init(&wrap, out);
209     out = &wrap;*/
210
211     int pagenr;
212     for(pagenr = 1; pagenr <= doc->num_pages; pagenr++) 
213     {
214         if(is_in_range(pagenr, pagerange)) {
215             gfxpage_t* page = doc->getpage(doc, pagenr);
216             out->startpage(out, page->width, page->height);
217             page->render(page, out);
218             out->endpage(out);
219             page->destroy(page);
220         }
221     }
222     gfxresult_t*result = out->finish(out);
223     if(result) {
224         if(result->save(result, outputname) < 0) {
225             exit(1);
226         }
227         result->destroy(result);
228     }
229     doc->destroy(doc);
230     driver->destroy(driver);
231     return 0;
232 }
233