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 #ifdef HAVE_SYS_STAT_H
34 #include "../lib/args.h"
35 #include "../lib/os.h"
36 #include "../lib/rfxswf.h"
37 #include "../lib/devices/swf.h"
38 #include "../lib/devices/arts.h"
39 #include "../lib/xpdf/pdf.h"
40 #include "../lib/log.h"
42 #define SWFDIR concatPaths(getInstallationPath(), "swfs")
46 static char * outputname = 0;
47 static int loglevel = 3;
48 static char * pagerange = 0;
49 static char * filename = 0;
50 static char * password = 0;
53 static char * preloader = 0;
54 static char * viewer = 0;
58 static int info_only = 0;
60 static int flatten = 0;
68 int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
71 static int system_quiet=0;
73 int systemf(const char* format, ...)
78 va_start(arglist, format);
79 vsprintf(buf, format, arglist);
88 fprintf(stderr, "system() returned %d\n", ret);
94 int args_callback_option(char*name,char*val) {
95 if (!strcmp(name, "o"))
100 else if (!strcmp(name, "v"))
103 setConsoleLogging(loglevel);
106 else if (!strcmp(name, "2"))
112 else if (!strcmp(name, "4"))
118 else if (!strcmp(name, "9"))
124 else if (!strcmp(name, "q"))
127 setConsoleLogging(loglevel);
131 else if (name[0]=='p')
133 /* check whether the page range follows the p directly, like
137 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
146 else if (!strcmp(name, "P"))
151 else if (!strcmp(name, "c"))
153 char*s = strdup(val);
154 char*x1 = strtok(s, ":");
155 char*y1 = strtok(0, ":");
156 char*x2 = strtok(0, ":");
157 char*y2 = strtok(0, ":");
158 if(!(x1 && y1 && x2 && y2)) {
159 fprintf(stderr, "-m option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
170 else if (!strcmp(name, "m"))
172 char*s = strdup(val);
173 char*c = strchr(s, ':');
175 fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
185 else if (!strcmp(name, "s"))
187 char*s = strdup(val);
188 char*c = strchr(s, '=');
189 if(c && *c && c[1]) {
192 driver->set_parameter(s,c);
195 driver->set_parameter(s,"1");
198 else if (!strcmp(name, "S"))
200 driver->set_parameter("drawonlyshapes", "1");
203 else if (!strcmp(name, "i"))
205 driver->set_parameter("ignoredraworder", "1");
208 else if (!strcmp(name, "z"))
210 driver->set_parameter("enablezlib", "1");
214 else if (!strcmp(name, "n"))
216 driver->set_parameter("opennewwindow", "1");
219 else if (!strcmp(name, "I"))
224 else if (!strcmp(name, "t"))
226 driver->set_parameter("insertstop", "1");
229 else if (!strcmp(name, "T"))
231 if(!strcasecmp(val, "mx"))
232 driver->set_parameter("flashversion", "6");
234 driver->set_parameter("flashversion", val);
238 else if (!strcmp(name, "f"))
240 driver->set_parameter("storeallcharacters", "1");
243 else if (!strcmp(name, "w"))
245 driver->set_parameter("linksopennewwindow", "0");
248 else if (!strcmp(name, "G"))
253 else if (!strcmp(name, "F"))
255 char *s = strdup(val);
257 while(l && s[l-1]=='/') {
261 fontpaths[fontpathpos++] = s;
264 else if (!strcmp(name, "l"))
267 sprintf(buf, "%s/default_loader.swf", SWFDIR);
268 preloader = strdup(buf);
271 else if (!strcmp(name, "b"))
274 sprintf(buf, "%s/default_viewer.swf", SWFDIR);
275 viewer = strdup(buf);
278 else if (!strcmp(name, "L"))
286 systemf("ls %s/*_loader.swf", SWFDIR);
293 else if (!strcmp(name, "B"))
301 systemf("ls %s/*_viewer.swf", SWFDIR);
308 else if (!strcmp(name, "j"))
311 driver->set_parameter("jpegquality", &name[1]);
314 driver->set_parameter("jpegquality", val);
318 else if (!strcmp(name, "V"))
320 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
325 fprintf(stderr, "Unknown option: -%s\n", name);
331 /*struct docoptions_t options[] =
332 {{"o","output","filename::Specify output file"},
333 {"V","version","Print program version"},
334 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
335 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
336 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
337 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
338 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
339 {"w","samewindow","Don't open a new browser window for links in the SWF"},
340 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
341 {"F","fontpath","path::Add directory to font search path"},
342 {"B","viewer","name::Link viewer \"name\" to the pdf"},
343 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
344 {"b","defaultviewer","Link default viewer to the pdf"},
345 {"l","defaultpreloader","Link default preloader to the pdf"}
348 struct options_t options[] =
365 {"b","defaultviewer"},
366 {"l","defaultpreloader"},
368 {"T","flashversion"},
372 int args_callback_longoption(char*name,char*val) {
373 return args_long2shortoption(options, name, val);
376 int args_callback_command(char*name, char*val) {
382 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
391 void args_callback_usage(char*name)
393 printf("Usage: %s [Options] input.pdf [-o output.swf]\n", name);
394 printf("\nBasic options:\n");
395 printf("-p --pages=range Convert only pages in range\n");
396 printf("-P --password=password Use password for deciphering the pdf\n");
397 printf("-v --verbose Be verbose. Use more than one -v for greater effect\n");
398 printf("-q --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
400 printf("-F --fontdir directory Add directory to font search path\n");
402 printf("-V --version Print program version\n");
403 printf("\nEnhanced conversion options:\n");
404 printf("-S --shapes Don't use SWF Fonts, but store everything as shape\n");
405 printf("-z --zlib Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)\n");
406 printf("-w --samewindow Don't open a new Browser Window for Links in the SWF\n");
407 printf("-f --fonts Store full fonts in SWF. (Don't reduce to used characters)\n");
408 printf("-T --flashversion=num Set the flash version in the header to num (default: 4)\n");
409 printf("-s insertstop Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
410 printf("-s zoom=factor Scale result, default: 72\n");
411 printf("-s jpegquality=quality Set quality of embedded jpeg pictures (default:85)\n");
412 printf("-s caplinewidth=value Set the minimum line width to trigger cap style handling to value. (3)\n");
413 printf("-s splinequality=value Set the quality of spline convertion to value (0-100, default: 100).\n");
414 printf("-s fontquality=value Set the quality of font convertion to value (0-100, default: 100).\n");
415 printf("-s ignoredraworder Ignore draw order (makes the SWF file smaller and faster, but may produce\n"
416 " graphic errors)\n");
417 printf("-s filloverlap Make intersecting shapes overlap, instead of canceling each\n"
418 " other out. (Needed for some Powerpoint PDFs)\n");
419 //deliberately undocumented (for now)
420 //printf("-2 Put 2 pages into each frame.\n");
421 //printf("-4 Put 4 pages into each frame.\n");
422 printf("Postprocessing options:\n");
423 printf("-b --defaultviewer Link default viewer to the pdf (%s)\n", concatPaths(SWFDIR, "default_viewer.swf"));
424 printf("-l --defaultpreloader Link default preloader the pdf (%s)\n", concatPaths(SWFDIR, "default_loader.swf"));
425 printf("-B --viewer=filename Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
426 printf("-L --preloader=filename Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
429 float getRate(char*filename)
433 fi = open(filename,O_RDONLY|O_BINARY);
436 sprintf(buffer, "Couldn't open %s", filename);
440 if(swf_ReadSWF(fi,&swf) < 0)
442 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
447 return swf.frameRate / 256.0;
450 void show_info(gfxsource_t*driver, char*filename)
452 gfxdocument_t* pdf = driver->open(filename);
456 msg("<error> Couldn't open %s", filename);
460 fo = fopen(outputname, "wb");
462 perror(outputname);exit(1);;
468 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
470 gfxpage_t*page = pdf->getpage(pdf,pagenr);
471 if(is_in_range(pagenr, pagerange)) {
472 fprintf(fo, "page=%d width=%.2f height=%.2f\n", pagenr, page->width, page->height);
478 int main(int argn, char *argv[])
484 char t1searchpath[1024];
487 char* installPath = getInstallationPath();
490 initLog(0,-1,0,0,-1,loglevel);
492 driver = gfxsource_pdf_create();
494 #if defined(WIN32) && defined(HAVE_STAT) && defined(HAVE_SYS_STAT_H)
496 fontdir = concatPaths(installPath, "fonts");
497 FILE*test = fopen(concatPaths(fontdir,"\\d050000l.afm"), "rb");
499 fprintf(stderr, "Couldn't find file %s - pdf2swf not installed properly? OS says:\n", concatPaths(fontdir, "\\d050000l.afm"));
506 fontdir = concatPaths(installPath, "fonts");
516 processargs(argn, argv);
520 fprintf(stderr, "Please specify an input file\n");
525 show_info(driver, filename);
532 outputname = stripFilename(filename, ".swf");
533 msg("<notice> Output filename not given. Writing to %s", outputname);
539 fprintf(stderr, "Please use -o to specify an output file\n");
543 // test if the page range is o.k.
544 is_in_range(0x7fffffff, pagerange);
547 args_callback_usage(argv[0]);
553 driver->set_parameter("fontdir", fontdir);
555 for(t=0;t<fontpathpos;t++) {
556 driver->set_parameter("fontdir", fontpaths[t]);
560 if(password && *password) {
561 sprintf(fullname, "%s|%s", filename, password);
565 gfxdocument_t* pdf = driver->open(filename);
567 msg("<error> Couldn't open %s", filename);
571 gfxdevice_t swf,wrap;
572 gfxdevice_swf_init(&swf);
576 gfxdevice_arts_init(&wrap, &swf);
592 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
594 if(is_in_range(pagenr, pagerange)) {
596 sprintf(mapping, "%d:%d", pagenr, frame);
597 pdf->set_parameter(pdf, "pagemap", mapping);
600 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
608 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
610 if(is_in_range(pagenr, pagerange)) {
611 gfxpage_t* page = pages[pagenum].page = pdf->getpage(pdf, pagenr);
612 pages[pagenum].x = 0;
613 pages[pagenum].y = 0;
614 pages[pagenum].page = page;
617 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
620 int xmax[xnup], ymax[xnup];
622 int width=0, height=0;
624 memset(xmax, 0, xnup*sizeof(int));
625 memset(ymax, 0, ynup*sizeof(int));
628 for(x=0;x<xnup;x++) {
631 if(pages[t].page->width > xmax[x])
632 xmax[x] = pages[t].page->width;
633 if(pages[t].page->height > ymax[y])
634 ymax[y] = pages[t].page->height;
636 for(x=0;x<xnup;x++) {
640 for(y=0;y<ynup;y++) {
645 out->startpage(out,clip_x2 - clip_x1, clip_y2 - clip_y1);
647 out->startpage(out,width,height);
649 for(t=0;t<pagenum;t++) {
652 int xpos = x>0?xmax[x-1]:0;
653 int ypos = y>0?ymax[y-1]:0;
654 msg("<verbose> Render (%d,%d) move:%d/%d\n",
655 (int)(pages[t].page->width + xpos),
656 (int)(pages[t].page->height + ypos), xpos, ypos);
657 pages[t].page->rendersection(pages[t].page, out, custom_move? move_x : xpos,
658 custom_move? move_y : ypos,
659 custom_clip? clip_x1 : 0 + xpos,
660 custom_clip? clip_y1 : 0 + ypos,
661 custom_clip? clip_x2 : pages[t].page->width + xpos,
662 custom_clip? clip_y2 : pages[t].page->height + ypos);
665 for(t=0;t<pagenum;t++) {
666 pages[t].page->destroy(pages[t].page);
672 gfxresult_t*result = out->finish(out);
673 if(result->save(result, outputname) < 0) {
677 int width = (int)result->get(result, "width");
678 int height = (int)result->get(result, "height");
679 msg("<notice> SWF written");
681 result->destroy(result);
688 #undef SYSTEM_BACKTICKS
689 if(viewer && !preloader) {
690 #ifdef SYSTEM_BACKTICKS
691 systemf("swfcombine %s `swfdump -XY \"%s\"` \"%s\" viewport=\"%s\" -o \"%s\"",zip,
692 viewer, outputname, outputname);
694 systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
695 viewer, outputname, outputname);
700 if(preloader && !viewer) {
701 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
702 #ifdef SYSTEM_BACKTICKS
703 ret = systemf("swfcombine %s `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,
704 preloader, SWFDIR, preloader, outputname, outputname);
706 ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
707 SWFDIR, preloader, outputname, outputname);
712 if(preloader && viewer) {
713 systemf("swfcombine \"%s\" viewport=%s -o __tmp__.swf",
714 viewer, outputname, outputname);
715 #ifdef SYSTEM_BACKTICKS
716 systemf("swfcombine %s `swfdump -XY \"%s\"` `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\"",zip,
717 outputname, preloader, SWFDIR, preloader, outputname);
719 /* TODO: read out rate */
720 systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\"",zip,width,height,
721 getRate(preloader), preloader, SWFDIR, preloader, outputname);
723 systemf("rm __tmp__.swf");