2 main routine for pdf2swf(1)
4 Part of the swftools package.
6 Copyright (c) 2001,2002,2003 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
27 #include "../../swftools/config.h"
28 #include "../../swftools/lib/args.h"
29 #include "../../swftools/lib/os.h"
30 #include "../../swftools/lib/gfxsource.h"
31 #include "../../swftools/lib/gfxdevice.h"
32 #include "../../swftools/lib/gfxpoly.h"
33 #include "../../swftools/lib/devices/pdf.h"
34 #include "../../swftools/lib/devices/swf.h"
35 #include "../../swftools/lib/devices/text.h"
36 #include "../../swftools/lib/devices/render.h"
37 #include "../../swftools/lib/devices/file.h"
38 #include "../../swftools/lib/devices/bbox.h"
40 #include "../../swftools/lib/devices/lrf.h"
42 #include "../../swftools/lib/devices/ocr.h"
43 #include "../../swftools/lib/devices/rescale.h"
44 #include "../../swftools/lib/devices/record.h"
45 #include "../../swftools/lib/readers/image.h"
46 #include "../../swftools/lib/readers/swf.h"
47 #include "../../swftools/lib/pdf/pdf.h"
48 #include "../../swftools/lib/log.h"
50 static gfxsource_t*driver = 0;
52 static char * outputname = 0;
53 static int loglevel = 3;
54 static char * pagerange = 0;
55 static char * filename = 0;
56 static const char * format = 0;
58 int args_callback_option(char*name,char*val) {
59 if (!strcmp(name, "o"))
64 else if (!strcmp(name, "v"))
67 setConsoleLogging(loglevel);
70 else if (!strcmp(name, "f"))
75 else if (!strcmp(name, "q"))
78 setConsoleLogging(loglevel);
81 else if (name[0]=='p')
85 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
94 else if (!strcmp(name, "s"))
97 fprintf(stderr, "Specify input file before -s\n");
100 char*s = strdup(val);
101 char*c = strchr(s, '=');
102 if(c && *c && c[1]) {
105 driver->setparameter(driver, s,c);
107 driver->setparameter(driver, s,"1");
112 else if (!strcmp(name, "V"))
114 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
119 fprintf(stderr, "Unknown option: -%s\n", name);
125 struct options_t options[] =
134 int args_callback_longoption(char*name,char*val) {
135 return args_long2shortoption(options, name, val);
138 int args_callback_command(char*name, char*val) {
143 if(strstr(filename, ".pdf") || strstr(filename, ".PDF")) {
144 msg("<notice> Treating file as PDF");
145 driver = gfxsource_pdf_create();
146 } else if(strstr(filename, ".swf") || strstr(filename, ".SWF")) {
147 msg("<notice> Treating file as SWF");
148 driver = gfxsource_swf_create();
149 } else if(strstr(filename, ".jpg") || strstr(filename, ".JPG") ||
150 strstr(filename, ".png") || strstr(filename, ".PNG")) {
151 msg("<notice> Treating file as Image");
152 driver = gfxsource_image_create();
157 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
166 void args_callback_usage(char*name)
170 int main(int argn, char *argv[])
172 processargs(argn, argv);
173 initLog(0,-1,0,0,-1,loglevel);
176 fprintf(stderr, "Please specify an input file\n");
183 outputname = stripFilename(filename, ".out");
184 msg("<notice> Output filename not given. Writing to %s", outputname);
189 fprintf(stderr, "Please use -o to specify an output file\n");
192 is_in_range(0x7fffffff, pagerange);
194 driver->setparameter(driver, "pages", pagerange);
197 args_callback_usage(argv[0]);
201 gfxdocument_t* doc = driver->open(driver, filename);
203 msg("<error> Couldn't open %s", filename);
208 char*x = strrchr(outputname, '.');
214 gfxresult_t*result = 0;
216 if(!strcasecmp(format, "lrf")) {
218 gfxdevice_lrf_init(&lrf);
221 gfxdevice_rescale_init(&rescale, &lrf, 592, 732, 0);
223 gfxdevice_t*out = &rescale;
224 out->setparameter(out, "keepratio", "1");
225 out->setparameter(out, "pagepattern", outputname);
227 gfxdevice_t bbox2,*bbox=&bbox2;
228 gfxdevice_bbox_init(bbox);
229 bbox->setparameter(bbox, "graphics", "0");
233 for(pagenr = 1; pagenr <= doc->num_pages; pagenr++)
235 if(is_in_range(pagenr, pagerange)) {
236 gfxpage_t* page = doc->getpage(doc, pagenr);
237 bbox->startpage(bbox,-1,-1);
238 page->render(page, bbox);
239 gfxbbox_t b = gfxdevice_bbox_getbbox(bbox);
241 out->startpage(out, b.xmax-b.xmin, b.ymax-b.ymin);
242 page->rendersection(page, out, -b.xmin, -b.ymin, 0,0,b.xmax-b.xmin,b.ymax-b.ymin);
248 result = out->finish(out);
252 gfxdevice_t _out,*out=&_out;
253 if(!strcasecmp(format, "ocr")) {
254 gfxdevice_ocr_init(out);
255 } else if(!strcasecmp(format, "swf")) {
256 gfxdevice_swf_init(out);
257 } else if(!strcasecmp(format, "img") || !strcasecmp(format, "png")) {
258 gfxdevice_render_init(out);
259 out->setparameter(out, "antialize", "4");
260 } else if(!strcasecmp(format, "txt")) {
261 gfxdevice_text_init(out);
262 } else if(!strcasecmp(format, "log")) {
263 gfxdevice_file_init(out, "/tmp/device.log");
264 } else if(!strcasecmp(format, "pdf")) {
265 gfxdevice_pdf_init(out);
267 msg("<error> Invalid output format: %s", format);
272 for(pagenr = 1; pagenr <= doc->num_pages; pagenr++)
274 if(is_in_range(pagenr, pagerange)) {
275 gfxpage_t* page = doc->getpage(doc, pagenr);
276 out->startpage(out, page->width, page->height);
277 page->render(page, out);
282 result = out->finish(out);
286 if(result->save(result, outputname) < 0) {
289 result->destroy(result);
294 driver->destroy(driver);