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/devices/record.h"
40 #include "../lib/pdf/pdf.h"
41 #include "../lib/log.h"
43 #define SWFDIR concatPaths(getInstallationPath(), "swfs")
47 static char * outputname = 0;
48 static int loglevel = 3;
49 static char * pagerange = 0;
50 static char * filename = 0;
51 static char * password = 0;
54 static char * preloader = 0;
55 static char * viewer = 0;
59 static int info_only = 0;
61 static int flatten = 0;
69 int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
72 static int system_quiet=0;
74 int systemf(const char* format, ...)
79 va_start(arglist, format);
80 vsprintf(buf, format, arglist);
89 fprintf(stderr, "system() returned %d\n", ret);
95 int args_callback_option(char*name,char*val) {
96 if (!strcmp(name, "o"))
101 else if (!strcmp(name, "v"))
104 setConsoleLogging(loglevel);
107 else if (!strcmp(name, "2"))
113 else if (!strcmp(name, "4"))
119 else if (!strcmp(name, "9"))
125 else if (!strcmp(name, "q"))
128 setConsoleLogging(loglevel);
132 else if (name[0]=='p')
134 /* check whether the page range follows the p directly, like
138 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
147 else if (!strcmp(name, "P"))
152 else if (!strcmp(name, "c"))
154 char*s = strdup(val);
155 char*x1 = strtok(s, ":");
156 char*y1 = strtok(0, ":");
157 char*x2 = strtok(0, ":");
158 char*y2 = strtok(0, ":");
159 if(!(x1 && y1 && x2 && y2)) {
160 fprintf(stderr, "-m option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
171 else if (!strcmp(name, "m"))
173 char*s = strdup(val);
174 char*c = strchr(s, ':');
176 fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
186 else if (!strcmp(name, "s"))
188 char*s = strdup(val);
189 char*c = strchr(s, '=');
190 if(c && *c && c[1]) {
193 driver->set_parameter(s,c);
196 driver->set_parameter(s,"1");
199 else if (!strcmp(name, "S"))
201 driver->set_parameter("drawonlyshapes", "1");
204 else if (!strcmp(name, "i"))
206 driver->set_parameter("ignoredraworder", "1");
209 else if (!strcmp(name, "z"))
211 driver->set_parameter("enablezlib", "1");
215 else if (!strcmp(name, "n"))
217 driver->set_parameter("opennewwindow", "1");
220 else if (!strcmp(name, "I"))
225 else if (!strcmp(name, "t"))
227 driver->set_parameter("insertstop", "1");
230 else if (!strcmp(name, "T"))
232 if(!strcasecmp(val, "mx"))
233 driver->set_parameter("flashversion", "6");
235 driver->set_parameter("flashversion", val);
239 else if (!strcmp(name, "f"))
241 driver->set_parameter("storeallcharacters", "1");
244 else if (!strcmp(name, "w"))
246 driver->set_parameter("linksopennewwindow", "0");
249 else if (!strcmp(name, "G"))
254 else if (!strcmp(name, "F"))
256 char *s = strdup(val);
258 while(l && s[l-1]=='/') {
262 fontpaths[fontpathpos++] = s;
265 else if (!strcmp(name, "l"))
268 sprintf(buf, "%s/default_loader.swf", SWFDIR);
269 preloader = strdup(buf);
272 else if (!strcmp(name, "b"))
275 sprintf(buf, "%s/default_viewer.swf", SWFDIR);
276 viewer = strdup(buf);
279 else if (!strcmp(name, "L"))
287 systemf("ls %s/*_loader.swf", SWFDIR);
294 else if (!strcmp(name, "B"))
302 systemf("ls %s/*_viewer.swf", SWFDIR);
309 else if (!strcmp(name, "j"))
312 driver->set_parameter("jpegquality", &name[1]);
315 driver->set_parameter("jpegquality", val);
319 else if (!strcmp(name, "V"))
321 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
326 fprintf(stderr, "Unknown option: -%s\n", name);
332 /*struct docoptions_t options[] =
333 {{"o","output","filename::Specify output file"},
334 {"V","version","Print program version"},
335 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
336 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
337 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
338 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
339 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
340 {"w","samewindow","Don't open a new browser window for links in the SWF"},
341 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
342 {"F","fontpath","path::Add directory to font search path"},
343 {"B","viewer","name::Link viewer \"name\" to the pdf"},
344 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
345 {"b","defaultviewer","Link default viewer to the pdf"},
346 {"l","defaultpreloader","Link default preloader to the pdf"}
349 struct options_t options[] =
366 {"b","defaultviewer"},
367 {"l","defaultpreloader"},
369 {"T","flashversion"},
373 int args_callback_longoption(char*name,char*val) {
374 return args_long2shortoption(options, name, val);
377 int args_callback_command(char*name, char*val) {
383 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
392 void args_callback_usage(char*name)
394 printf("Usage: %s [Options] input.pdf [-o output.swf]\n", name);
395 printf("\nBasic options:\n");
396 printf("-p --pages=range Convert only pages in range\n");
397 printf("-P --password=password Use password for deciphering the pdf\n");
398 printf("-v --verbose Be verbose. Use more than one -v for greater effect\n");
399 printf("-q --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
401 printf("-F --fontdir directory Add directory to font search path\n");
403 printf("-V --version Print program version\n");
404 printf("\nEnhanced conversion options:\n");
405 printf("-S --shapes Don't use SWF Fonts, but store everything as shape\n");
406 printf("-z --zlib Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)\n");
407 printf("-w --samewindow Don't open a new Browser Window for Links in the SWF\n");
408 printf("-f --fonts Store full fonts in SWF. (Don't reduce to used characters)\n");
409 printf("-T --flashversion=num Set the flash version in the header to num (default: 4)\n");
410 printf("-s insertstop Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
411 printf("-s zoom=factor Scale result, default: 72\n");
412 printf("-s jpegquality=quality Set quality of embedded jpeg pictures (default:85)\n");
413 printf("-s caplinewidth=value Set the minimum line width to trigger cap style handling to value. (3)\n");
414 printf("-s splinequality=value Set the quality of spline convertion to value (0-100, default: 100).\n");
415 printf("-s fontquality=value Set the quality of font convertion to value (0-100, default: 100).\n");
416 printf("-s ignoredraworder Ignore draw order (makes the SWF file smaller and faster, but may produce\n"
417 " graphic errors)\n");
418 printf("-s filloverlap Make intersecting shapes overlap, instead of canceling each\n"
419 " other out. (Needed for some Powerpoint PDFs)\n");
420 //deliberately undocumented (for now)
421 //printf("-2 Put 2 pages into each frame.\n");
422 //printf("-4 Put 4 pages into each frame.\n");
423 printf("Postprocessing options:\n");
424 printf("-b --defaultviewer Link default viewer to the pdf (%s)\n", concatPaths(SWFDIR, "default_viewer.swf"));
425 printf("-l --defaultpreloader Link default preloader the pdf (%s)\n", concatPaths(SWFDIR, "default_loader.swf"));
426 printf("-B --viewer=filename Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
427 printf("-L --preloader=filename Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
430 float getRate(char*filename)
434 fi = open(filename,O_RDONLY|O_BINARY);
437 sprintf(buffer, "Couldn't open %s", filename);
441 if(swf_ReadSWF(fi,&swf) < 0)
443 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
448 return swf.frameRate / 256.0;
451 void show_info(gfxsource_t*driver, char*filename)
453 gfxdocument_t* pdf = driver->open(filename);
457 msg("<error> Couldn't open %s", filename);
461 fo = fopen(outputname, "wb");
463 perror(outputname);exit(1);;
469 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
471 gfxpage_t*page = pdf->getpage(pdf,pagenr);
472 if(is_in_range(pagenr, pagerange)) {
473 fprintf(fo, "page=%d width=%.2f height=%.2f\n", pagenr, page->width, page->height);
479 int main(int argn, char *argv[])
485 char t1searchpath[1024];
488 char* installPath = getInstallationPath();
491 initLog(0,-1,0,0,-1,loglevel);
493 #if defined(WIN32) && defined(HAVE_STAT) && defined(HAVE_SYS_STAT_H)
495 fontdir = concatPaths(installPath, "fonts");
496 FILE*test = fopen(concatPaths(fontdir,"\\d050000l.afm"), "rb");
498 fprintf(stderr, "Couldn't find file %s - pdf2swf not installed properly? OS says:\n", concatPaths(fontdir, "\\d050000l.afm"));
505 fontdir = concatPaths(installPath, "fonts");
515 driver = gfxsource_pdf_create();
517 processargs(argn, argv);
521 fprintf(stderr, "Please specify an input file\n");
526 show_info(driver, filename);
533 outputname = stripFilename(filename, ".swf");
534 msg("<notice> Output filename not given. Writing to %s", outputname);
540 fprintf(stderr, "Please use -o to specify an output file\n");
544 // test if the page range is o.k.
545 is_in_range(0x7fffffff, pagerange);
548 driver->set_parameter("pages", pagerange);
551 args_callback_usage(argv[0]);
558 driver->set_parameter("fontdir", fontdir);
560 for(t=0;t<fontpathpos;t++) {
561 driver->set_parameter("fontdir", fontpaths[t]);
565 if(password && *password) {
566 sprintf(fullname, "%s|%s", filename, password);
570 gfxdocument_t* pdf = driver->open(filename);
572 msg("<error> Couldn't open %s", filename);
576 gfxdevice_t swf,wrap;
577 gfxdevice_swf_init(&swf);
581 gfxdevice_removeclippings_init(&wrap, &swf);
597 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
599 if(is_in_range(pagenr, pagerange)) {
601 sprintf(mapping, "%d:%d", pagenr, frame);
602 pdf->set_parameter(pdf, "pagemap", mapping);
605 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
613 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
615 if(is_in_range(pagenr, pagerange)) {
616 gfxpage_t* page = pages[pagenum].page = pdf->getpage(pdf, pagenr);
617 pages[pagenum].x = 0;
618 pages[pagenum].y = 0;
619 pages[pagenum].page = page;
622 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
625 int xmax[xnup], ymax[xnup];
627 int width=0, height=0;
629 memset(xmax, 0, xnup*sizeof(int));
630 memset(ymax, 0, ynup*sizeof(int));
633 for(x=0;x<xnup;x++) {
636 if(pages[t].page->width > xmax[x])
637 xmax[x] = (int)pages[t].page->width;
638 if(pages[t].page->height > ymax[y])
639 ymax[y] = (int)pages[t].page->height;
641 for(x=0;x<xnup;x++) {
645 for(y=0;y<ynup;y++) {
650 out->startpage(out,clip_x2 - clip_x1, clip_y2 - clip_y1);
652 out->startpage(out,width,height);
654 for(t=0;t<pagenum;t++) {
657 int xpos = x>0?xmax[x-1]:0;
658 int ypos = y>0?ymax[y-1]:0;
659 msg("<verbose> Render (%d,%d) move:%d/%d\n",
660 (int)(pages[t].page->width + xpos),
661 (int)(pages[t].page->height + ypos), xpos, ypos);
662 pages[t].page->rendersection(pages[t].page, out, custom_move? move_x : xpos,
663 custom_move? move_y : ypos,
664 custom_clip? clip_x1 : 0 + xpos,
665 custom_clip? clip_y1 : 0 + ypos,
666 custom_clip? clip_x2 : pages[t].page->width + xpos,
667 custom_clip? clip_y2 : pages[t].page->height + ypos);
670 for(t=0;t<pagenum;t++) {
671 pages[t].page->destroy(pages[t].page);
677 gfxresult_t*result = out->finish(out);
679 if(result->save(result, outputname) < 0) {
683 int width = (int)result->get(result, "width");
684 int height = (int)result->get(result, "height");
685 msg("<notice> SWF written");
687 result->destroy(result);
695 if(!preloader && viewer) {
696 systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
697 viewer, outputname, outputname);
701 if(preloader && !viewer) {
702 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
703 ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
704 SWFDIR, preloader, outputname, outputname);
708 if(preloader && viewer) {
709 systemf("swfcombine \"%s\" viewport=%s -o __tmp__.swf",
711 systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\"",zip,width,height,
712 getRate(preloader), SWFDIR, preloader, outputname);
713 systemf("rm __tmp__.swf");