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 "../config.h"
31 #include "../lib/args.h"
32 #include "../lib/os.h"
33 #include "../lib/rfxswf.h"
34 #include "../lib/devices/swf.h"
35 #include "../lib/devices/polyops.h"
36 #include "../lib/devices/record.h"
37 #include "../lib/pdf/pdf.h"
38 #include "../lib/log.h"
40 #define SWFDIR concatPaths(getInstallationPath(), "swfs")
44 static char * outputname = 0;
45 static int loglevel = 3;
46 static char * pagerange = 0;
47 static char * filename = 0;
48 static char * password = 0;
51 static char * preloader = 0;
52 static char * viewer = 0;
56 static int info_only = 0;
58 static int flatten = 0;
66 int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
69 static int system_quiet=0;
71 int systemf(const char* format, ...)
76 va_start(arglist, format);
77 vsprintf(buf, format, arglist);
86 fprintf(stderr, "system() returned %d\n", ret);
92 int args_callback_option(char*name,char*val) {
93 if (!strcmp(name, "o"))
98 else if (!strcmp(name, "v"))
101 setConsoleLogging(loglevel);
104 else if (!strcmp(name, "2"))
110 else if (!strcmp(name, "4"))
116 else if (!strcmp(name, "9"))
122 else if (!strcmp(name, "q"))
125 setConsoleLogging(loglevel);
129 else if (name[0]=='p')
131 /* check whether the page range follows the p directly, like
135 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
144 else if (!strcmp(name, "P"))
149 else if (!strcmp(name, "c"))
151 char*s = strdup(val);
152 char*x1 = strtok(s, ":");
153 char*y1 = strtok(0, ":");
154 char*x2 = strtok(0, ":");
155 char*y2 = strtok(0, ":");
156 if(!(x1 && y1 && x2 && y2)) {
157 fprintf(stderr, "-c option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
168 else if (!strcmp(name, "m"))
170 char*s = strdup(val);
171 char*c = strchr(s, ':');
173 fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
183 else if (!strcmp(name, "s"))
185 char*s = strdup(val);
186 char*c = strchr(s, '=');
187 if(c && *c && c[1]) {
190 driver->set_parameter(driver, s,c);
193 driver->set_parameter(driver, s,"1");
196 else if (!strcmp(name, "S"))
198 driver->set_parameter(driver, "drawonlyshapes", "1");
201 else if (!strcmp(name, "i"))
203 driver->set_parameter(driver, "ignoredraworder", "1");
207 else if (!strcmp(name, "Q"))
209 int seconds = atoi(val);
214 else if (!strcmp(name, "z"))
216 driver->set_parameter(driver, "enablezlib", "1");
220 else if (!strcmp(name, "n"))
222 driver->set_parameter(driver, "opennewwindow", "1");
225 else if (!strcmp(name, "I"))
230 else if (!strcmp(name, "t"))
232 driver->set_parameter(driver, "insertstop", "1");
235 else if (!strcmp(name, "T"))
237 if(!strcasecmp(val, "mx"))
238 driver->set_parameter(driver, "flashversion", "6");
240 driver->set_parameter(driver, "flashversion", val);
244 else if (!strcmp(name, "f"))
246 driver->set_parameter(driver, "storeallcharacters", "1");
247 driver->set_parameter(driver, "extrafontdata", "1");
250 else if (!strcmp(name, "w"))
252 driver->set_parameter(driver, "linksopennewwindow", "0");
255 else if (!strcmp(name, "O"))
259 if(val&& val[0] && val[1]==0 && isdigit(val[0])) {
264 driver->set_parameter(driver, "poly2bitmap", "1");
266 driver->set_parameter(driver, "bitmapfonts", "1");
268 driver->set_parameter(driver, "ignoredraworder", "1");
271 else if (!strcmp(name, "G"))
273 driver->set_parameter(driver, "optimize_polygons", "1");
277 else if (!strcmp(name, "F"))
279 char *s = strdup(val);
281 while(l && s[l-1]=='/') {
285 fontpaths[fontpathpos++] = s;
288 else if (!strcmp(name, "l"))
291 sprintf(buf, "%s/default_loader.swf", SWFDIR);
292 preloader = strdup(buf);
295 else if (!strcmp(name, "b"))
298 sprintf(buf, "%s/default_viewer.swf", SWFDIR);
299 viewer = strdup(buf);
302 else if (!strcmp(name, "L"))
310 systemf("ls %s/*_loader.swf", SWFDIR);
317 else if (!strcmp(name, "B"))
325 systemf("ls %s/*_viewer.swf", SWFDIR);
332 else if (!strcmp(name, "j"))
335 driver->set_parameter(driver, "jpegquality", &name[1]);
338 driver->set_parameter(driver, "jpegquality", val);
342 else if (!strcmp(name, "V"))
344 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
349 fprintf(stderr, "Unknown option: -%s\n", name);
355 /*struct docoptions_t options[] =
356 {{"o","output","filename::Specify output file"},
357 {"V","version","Print program version"},
358 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
359 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
360 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
361 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
362 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
363 {"w","samewindow","Don't open a new browser window for links in the SWF"},
364 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
365 {"F","fontpath","path::Add directory to font search path"},
366 {"B","viewer","name::Link viewer \"name\" to the pdf"},
367 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
368 {"b","defaultviewer","Link default viewer to the pdf"},
369 {"l","defaultpreloader","Link default preloader to the pdf"}
372 struct options_t options[] =
390 {"b","defaultviewer"},
391 {"l","defaultpreloader"},
393 {"T","flashversion"},
397 int args_callback_longoption(char*name,char*val) {
398 return args_long2shortoption(options, name, val);
401 int args_callback_command(char*name, char*val) {
407 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
416 void args_callback_usage(char*name)
418 printf("Usage: %s [Options] input.pdf [-o output.swf]\n", name);
419 printf("\nBasic options:\n");
420 printf("-p --pages=range Convert only pages in range\n");
421 printf("-P --password=password Use password for deciphering the pdf\n");
422 printf("-v --verbose Be verbose. Use more than one -v for greater effect\n");
423 printf("-q --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
425 printf("-F --fontdir directory Add directory to font search path\n");
427 printf("-V --version Print program version\n");
428 printf("\nEnhanced conversion options:\n");
429 printf("-S --shapes Don't use SWF Fonts, but store everything as shape\n");
430 printf("-z --zlib Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)\n");
431 printf("-w --samewindow Don't open a new Browser Window for Links in the SWF\n");
432 printf("-f --fonts Store full fonts in SWF. (Don't reduce to used characters)\n");
433 printf("-T --flashversion=num Set the flash version in the header to num (default: 4)\n");
434 printf("-s insertstop Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
435 printf("-s zoom=factor Scale result, default: 72\n");
436 printf("-s jpegquality=quality Set quality of embedded jpeg pictures (default:85)\n");
437 printf("-s caplinewidth=value Set the minimum line width to trigger cap style handling to value. (3)\n");
438 printf("-s splinequality=value Set the quality of spline convertion to value (0-100, default: 100).\n");
439 printf("-s fontquality=value Set the quality of font convertion to value (0-100, default: 100).\n");
440 printf("-s ignoredraworder Ignore draw order (makes the SWF file smaller and faster, but may produce\n"
441 " graphic errors)\n");
442 printf("-s filloverlap Make intersecting shapes overlap, instead of canceling each\n"
443 " other out. (Needed for some Powerpoint PDFs)\n");
444 printf("-s transparent Make the SWF transparent\n");
445 //deliberately undocumented (for now)
446 //printf("-2 Put 2 pages into each frame.\n");
447 //printf("-4 Put 4 pages into each frame.\n");
448 printf("Postprocessing options:\n");
449 printf("-b --defaultviewer Link default viewer to the pdf (%s)\n", concatPaths(SWFDIR, "default_viewer.swf"));
450 printf("-l --defaultpreloader Link default preloader the pdf (%s)\n", concatPaths(SWFDIR, "default_loader.swf"));
451 printf("-B --viewer=filename Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
452 printf("-L --preloader=filename Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
455 float getRate(char*filename)
459 fi = open(filename,O_RDONLY|O_BINARY);
462 sprintf(buffer, "Couldn't open %s", filename);
466 if(swf_ReadSWF(fi,&swf) < 0)
468 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
473 return swf.frameRate / 256.0;
476 void show_info(gfxsource_t*driver, char*filename)
478 gfxdocument_t* pdf = driver->open(driver, filename);
482 msg("<error> Couldn't open %s", filename);
486 fo = fopen(outputname, "wb");
488 perror(outputname);exit(1);;
494 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
496 gfxpage_t*page = pdf->getpage(pdf,pagenr);
497 if(is_in_range(pagenr, pagerange)) {
498 fprintf(fo, "page=%d width=%.2f height=%.2f\n", pagenr, page->width, page->height);
504 int main(int argn, char *argv[])
510 char t1searchpath[1024];
513 char* installPath = getInstallationPath();
515 initLog(0,-1,0,0,-1,loglevel);
517 /* not needed anymore since fonts are embedded
519 fontpaths[fontpathpos++] = concatPaths(installPath, "fonts");
529 driver = gfxsource_pdf_create();
531 processargs(argn, argv);
535 fprintf(stderr, "Please specify an input file\n");
540 show_info(driver, filename);
547 outputname = stripFilename(filename, ".swf");
548 msg("<notice> Output filename not given. Writing to %s", outputname);
554 fprintf(stderr, "Please use -o to specify an output file\n");
558 // test if the page range is o.k.
559 is_in_range(0x7fffffff, pagerange);
562 driver->set_parameter(driver, "pages", pagerange);
565 args_callback_usage(argv[0]);
570 for(t=0;t<fontpathpos;t++) {
571 driver->set_parameter(driver, "fontdir", fontpaths[t]);
575 if(password && *password) {
576 sprintf(fullname, "%s|%s", filename, password);
580 gfxdocument_t* pdf = driver->open(driver, filename);
582 msg("<error> Couldn't open %s", filename);
586 gfxdevice_t swf,wrap;
587 gfxdevice_swf_init(&swf);
591 gfxdevice_removeclippings_init(&wrap, &swf);
607 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
609 if(is_in_range(pagenr, pagerange)) {
611 sprintf(mapping, "%d:%d", pagenr, frame);
612 pdf->set_parameter(pdf, "pagemap", mapping);
615 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
623 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
625 if(is_in_range(pagenr, pagerange)) {
626 gfxpage_t* page = pages[pagenum].page = pdf->getpage(pdf, pagenr);
627 pages[pagenum].x = 0;
628 pages[pagenum].y = 0;
629 pages[pagenum].page = page;
632 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
635 int xmax[xnup], ymax[xnup];
637 int width=0, height=0;
639 memset(xmax, 0, xnup*sizeof(int));
640 memset(ymax, 0, ynup*sizeof(int));
643 for(x=0;x<xnup;x++) {
646 if(pages[t].page->width > xmax[x])
647 xmax[x] = (int)pages[t].page->width;
648 if(pages[t].page->height > ymax[y])
649 ymax[y] = (int)pages[t].page->height;
651 for(x=0;x<xnup;x++) {
655 for(y=0;y<ynup;y++) {
660 out->startpage(out,clip_x2 - clip_x1, clip_y2 - clip_y1);
662 out->startpage(out,width,height);
664 for(t=0;t<pagenum;t++) {
667 int xpos = x>0?xmax[x-1]:0;
668 int ypos = y>0?ymax[y-1]:0;
669 msg("<verbose> Render (%d,%d) move:%d/%d\n",
670 (int)(pages[t].page->width + xpos),
671 (int)(pages[t].page->height + ypos), xpos, ypos);
672 pages[t].page->rendersection(pages[t].page, out, custom_move? move_x : xpos,
673 custom_move? move_y : ypos,
674 custom_clip? clip_x1 : 0 + xpos,
675 custom_clip? clip_y1 : 0 + ypos,
676 custom_clip? clip_x2 : pages[t].page->width + xpos,
677 custom_clip? clip_y2 : pages[t].page->height + ypos);
680 for(t=0;t<pagenum;t++) {
681 pages[t].page->destroy(pages[t].page);
687 gfxresult_t*result = out->finish(out);
689 if(result->save(result, outputname) < 0) {
693 int width = (int)result->get(result, "width");
694 int height = (int)result->get(result, "height");
695 msg("<notice> SWF written");
697 result->destroy(result);
700 driver->destroy(driver);
706 if(!preloader && viewer) {
707 systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
708 viewer, outputname, outputname);
712 if(preloader && !viewer) {
713 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
714 ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
715 SWFDIR, preloader, outputname, outputname);
719 if(preloader && viewer) {
721 char tmpname[] = "__swf__XXXXXX";
724 char*tmpname = "__tmp__.swf";
726 systemf("swfcombine \"%s\" viewport=%s -o %s",
727 viewer, outputname, tmpname);
728 systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=%s -o \"%s\"",zip,width,height,
729 getRate(preloader), SWFDIR, preloader, tmpname, outputname);
730 systemf("rm %s", tmpname);