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/swf.h"
34 #include "../../swftools/lib/devices/text.h"
35 #include "../../swftools/lib/devices/render.h"
36 #include "../../swftools/lib/devices/bbox.h"
37 #include "../../swftools/lib/devices/lrf.h"
38 #include "../../swftools/lib/devices/ocr.h"
39 #include "../../swftools/lib/devices/rescale.h"
40 #include "../../swftools/lib/devices/record.h"
41 #include "../../swftools/lib/readers/image.h"
42 #include "../../swftools/lib/readers/swf.h"
43 #include "../../swftools/lib/pdf/pdf.h"
44 #include "../../swftools/lib/log.h"
46 static gfxsource_t*driver = 0;
48 static char * outputname = 0;
49 static int loglevel = 3;
50 static char * pagerange = 0;
51 static char * filename = 0;
52 static const char * format = "ocr";
54 int args_callback_option(char*name,char*val) {
55 if (!strcmp(name, "o"))
60 else if (!strcmp(name, "v"))
63 setConsoleLogging(loglevel);
66 else if (!strcmp(name, "f"))
71 else if (!strcmp(name, "q"))
74 setConsoleLogging(loglevel);
77 else if (name[0]=='p')
81 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
90 else if (!strcmp(name, "s"))
93 fprintf(stderr, "Specify input file before -s\n");
97 char*c = strchr(s, '=');
101 driver->set_parameter(driver, s,c);
103 driver->set_parameter(driver, s,"1");
108 else if (!strcmp(name, "V"))
110 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
115 fprintf(stderr, "Unknown option: -%s\n", name);
121 struct options_t options[] =
130 int args_callback_longoption(char*name,char*val) {
131 return args_long2shortoption(options, name, val);
134 int args_callback_command(char*name, char*val) {
139 if(strstr(filename, ".pdf") || strstr(filename, ".PDF")) {
140 msg("<notice> Treating file as PDF");
141 driver = gfxsource_pdf_create();
142 } else if(strstr(filename, ".swf") || strstr(filename, ".SWF")) {
143 msg("<notice> Treating file as SWF");
144 driver = gfxsource_swf_create();
145 } else if(strstr(filename, ".jpg") || strstr(filename, ".JPG") ||
146 strstr(filename, ".png") || strstr(filename, ".PNG")) {
147 msg("<notice> Treating file as Image");
148 driver = gfxsource_image_create();
153 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
162 void args_callback_usage(char*name)
166 int main(int argn, char *argv[])
168 processargs(argn, argv);
169 initLog(0,-1,0,0,-1,loglevel);
172 fprintf(stderr, "Please specify an input file\n");
179 outputname = stripFilename(filename, ".out");
180 msg("<notice> Output filename not given. Writing to %s", outputname);
185 fprintf(stderr, "Please use -o to specify an output file\n");
188 is_in_range(0x7fffffff, pagerange);
190 driver->set_parameter(driver, "pages", pagerange);
193 args_callback_usage(argv[0]);
197 gfxdocument_t* doc = driver->open(driver, filename);
199 msg("<error> Couldn't open %s", filename);
203 gfxresult_t*result = 0;
204 if(!strcmp(format, "lrf")) {
206 gfxdevice_lrf_init(&lrf);
209 gfxdevice_rescale_init(&rescale, &lrf, 592, 732, 0);
211 gfxdevice_t*out = &rescale;
212 out->setparameter(out, "keepratio", "1");
213 out->setparameter(out, "pagepattern", outputname);
215 gfxdevice_t bbox2,*bbox=&bbox2;
216 gfxdevice_bbox_init(bbox);
217 bbox->setparameter(bbox, "graphics", "0");
221 for(pagenr = 1; pagenr <= doc->num_pages; pagenr++)
223 if(is_in_range(pagenr, pagerange)) {
224 gfxpage_t* page = doc->getpage(doc, pagenr);
225 bbox->startpage(bbox,-1,-1);
226 page->render(page, bbox);
227 gfxbbox_t b = gfxdevice_bbox_getbbox(bbox);
229 out->startpage(out, b.xmax-b.xmin, b.ymax-b.ymin);
230 page->rendersection(page, out, -b.xmin, -b.ymin, 0,0,b.xmax-b.xmin,b.ymax-b.ymin);
236 result = out->finish(out);
238 gfxdevice_t _out,*out=&_out;
239 if(!strcmp(format, "ocr")) {
240 gfxdevice_ocr_init(out);
241 } if(!strcmp(format, "swf")) {
242 gfxdevice_swf_init(out);
243 } if(!strcmp(format, "img")) {
244 gfxdevice_render_init(out);
245 } if(!strcmp(format, "txt")) {
246 gfxdevice_text_init(out);
250 for(pagenr = 1; pagenr <= doc->num_pages; pagenr++)
252 if(is_in_range(pagenr, pagerange)) {
253 gfxpage_t* page = doc->getpage(doc, pagenr);
254 out->startpage(out, page->width, page->height);
255 page->render(page, out);
260 result = out->finish(out);
264 if(result->save(result, outputname) < 0) {
267 result->destroy(result);
272 driver->destroy(driver);