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 "SWFOutputDev.h"
39 #define FONTDIR concatPaths(getInstallationPath(), "fonts")
40 #define SWFDIR concatPaths(getInstallationPath(), "swfs")
42 static char * outputname = 0;
43 static int loglevel = 3;
44 static char * pagerange = 0;
45 static char * filename = 0;
46 static char * password = 0;
49 static char * preloader = 0;
50 static char * viewer = 0;
56 static int system_quiet=0;
58 int systemf(const char* format, ...)
63 va_start(arglist, format);
64 vsprintf(buf, format, arglist);
73 fprintf(stderr, "system() returned %d\n", ret);
79 int args_callback_option(char*name,char*val) {
80 if (!strcmp(name, "o"))
85 else if (!strcmp(name, "v"))
88 setConsoleLogging(loglevel);
91 else if (!strcmp(name, "2"))
96 else if (!strcmp(name, "4"))
101 else if (!strcmp(name, "q"))
104 setConsoleLogging(loglevel);
108 else if (name[0]=='p')
110 /* check whether the page range follows the p directly, like
114 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
123 else if (!strcmp(name, "P"))
128 else if (!strcmp(name, "s"))
130 char*s = strdup(val);
131 char*c = strchr(s, '=');
132 if(c && *c && c[1]) {
135 pdfswf_setparameter(s,c);
138 pdfswf_setparameter(s,"1");
141 else if (!strcmp(name, "S"))
143 pdfswf_setparameter("drawonlyshapes", "1");
146 else if (!strcmp(name, "i"))
148 pdfswf_setparameter("ignoredraworder", "1");
151 else if (!strcmp(name, "z"))
153 pdfswf_setparameter("enablezlib", "1");
157 else if (!strcmp(name, "n"))
159 pdfswf_setparameter("opennewwindow", "1");
162 else if (!strcmp(name, "t"))
164 pdfswf_setparameter("insertstop", "1");
167 else if (!strcmp(name, "T"))
169 if(!strcasecmp(val, "mx"))
170 pdfswf_setparameter("flashversion", "6");
172 pdfswf_setparameter("flashversion", val);
176 else if (!strcmp(name, "f"))
178 pdfswf_setparameter("storeallcharacters", "1");
181 else if (!strcmp(name, "w"))
183 pdfswf_setparameter("linksopennewwindow", "0");
186 else if (!strcmp(name, "F"))
188 char *s = strdup(val);
190 while(l && s[l-1]=='/') {
194 fontpaths[fontpathpos++] = s;
197 else if (!strcmp(name, "l"))
200 sprintf(buf, "%s/default_loader.swf", SWFDIR);
201 preloader = strdup(buf);
204 else if (!strcmp(name, "b"))
207 sprintf(buf, "%s/default_viewer.swf", SWFDIR);
208 viewer = strdup(buf);
211 else if (!strcmp(name, "L"))
219 systemf("ls %s/*_loader.swf", SWFDIR);
226 else if (!strcmp(name, "B"))
234 systemf("ls %s/*_viewer.swf", SWFDIR);
241 else if (!strcmp(name, "j"))
244 pdfswf_setparameter("jpegquality", &name[1]);
247 pdfswf_setparameter("jpegquality", val);
251 else if (!strcmp(name, "V"))
253 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
258 fprintf(stderr, "Unknown option: -%s\n", name);
264 /*struct docoptions_t options[] =
265 {{"o","output","filename::Specify output file"},
266 {"V","version","Print program version"},
267 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
268 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
269 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
270 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
271 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
272 {"w","samewindow","Don't open a new browser window for links in the SWF"},
273 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
274 {"F","fontpath","path::Add directory to font search path"},
275 {"B","viewer","name::Link viewer \"name\" to the pdf"},
276 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
277 {"b","defaultviewer","Link default viewer to the pdf"},
278 {"l","defaultpreloader","Link default preloader to the pdf"}
281 struct options_t options[] =
296 {"b","defaultviewer"},
297 {"l","defaultpreloader"},
299 {"T","flashversion"},
303 int args_callback_longoption(char*name,char*val) {
304 return args_long2shortoption(options, name, val);
307 int args_callback_command(char*name, char*val) {
313 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
322 void args_callback_usage(char*name)
324 printf("Usage: %s [Options] input.pdf [-o output.swf]\n", name);
325 printf("\nBasic options:\n");
326 printf("-p --pages=range Convert only pages in range\n");
327 printf("-P --password=password Use password for deciphering the pdf\n");
328 printf("-v --verbose Be verbose. Use more than one -v for greater effect\n");
329 printf("-q --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
331 printf("-F --fontdir directory Add directory to font search path\n");
333 printf("-V --version Print program version\n");
334 printf("\nEnhanced conversion options:\n");
335 printf("-S --shapes Don't use SWF Fonts, but store everything as shape\n");
336 printf("-z --zlib Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)\n");
337 printf("-w --samewindow Don't open a new Browser Window for Links in the SWF\n");
338 printf("-f --fonts Store full fonts in SWF. (Don't reduce to used characters)\n");
339 printf("-T --flashversion=num Set the flash version in the header to num (default: 4)\n");
340 printf("-s insertstop Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
341 printf("-s zoom=factor Scale result, default: 72\n");
342 printf("-s jpegquality=quality Set quality of embedded jpeg pictures (default:85)\n");
343 printf("-s caplinewidth=value Set the minimum line width to trigger cap style handling to value. (3)\n");
344 printf("-s splinequality=value Set the quality of spline convertion to value (0-100, default: 100).\n");
345 printf("-s fontquality=value Set the quality of font convertion to value (0-100, default: 100).\n");
346 printf("-s ignoredraworder Ignore draw order (makes the SWF file smaller and faster, but may produce\n"
347 " graphic errors)\n");
348 printf("-s filloverlap Make intersecting shapes overlap, instead of canceling each\n"
349 " other out. (Needed for some Powerpoint PDFs)\n");
350 //deliberately undocumented (for now)
351 //printf("-2 Put 2 pages into each frame.\n");
352 //printf("-4 Put 4 pages into each frame.\n");
353 printf("Postprocessing options:\n");
354 #ifndef SYSTEM_BACKTICKS
355 printf("(They might not work because your system call doesn't support command substitution)\n");
357 printf("-b --defaultviewer Link default viewer to the pdf (%s)\n", concatPaths(SWFDIR, "default_viewer.swf"));
358 printf("-l --defaultpreloader Link default preloader the pdf (%s)\n", concatPaths(SWFDIR, "default_loader.swf"));
359 printf("-B --viewer=filename Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
360 printf("-L --preloader=filename Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
363 int main(int argn, char *argv[])
369 char t1searchpath[1024];
373 initLog(0,-1,0,0,-1,loglevel);
375 #if defined(WIN32) && defined(HAVE_STAT) && defined(HAVE_SYS_STAT_H)
376 FILE*test = fopen(concatPaths(FONTDIR,"\\d050000l.afm"), "rb");
378 fprintf(stderr, "Couldn't find file %s - pdf2swf not installed properly? OS says:\n", concatPaths(FONTDIR, "\\d050000l.afm"));
392 processargs(argn, argv);
396 fprintf(stderr, "Please specify an input file\n");
403 outputname = stripFilename(filename, ".swf");
404 msg("<notice> Output filename not given. Writing to %s", outputname);
410 fprintf(stderr, "Please use -o to specify an output file\n");
414 // test if the page range is o.k.
415 is_in_range(0x7fffffff, pagerange);
418 args_callback_usage(argv[0]);
423 pdfswf_addfontdir(FONTDIR);
424 for(t=0;t<fontpathpos;t++) {
425 pdfswf_addfontdir(fontpaths[t]);
428 pdf_doc_t* pdf = pdf_init(filename, password);
430 msg("<error> Couldn't open %s", filename);
433 swf_output_t* swf = swf_output_init();
435 for(t = 1; t <= pdf->num_pages; t++)
437 if(is_in_range(t, pagerange)) {
438 /* for links: FIXME */
439 pdfswf_preparepage(t);
441 if(is_in_range(t, pagerange)) {
442 pdf_page_t*page = pdf_getpage(pdf, t);
444 pdf_page_info_t* info = pdf_page_getinfo(page);
445 if(nup_pos%nup == 0) {
446 pdf_page_render(page, swf);
449 } else if(nup_pos%nup == 1) {
450 pdf_page_rendersection(page, swf, x, 0, info->xMin+x, info->yMin, info->xMax+x, info->yMax);
451 } else if(nup_pos%nup == 2) {
452 pdf_page_rendersection(page, swf, 0, y, info->xMin, info->yMin+y, info->xMax, info->yMax+y);
453 } else if(nup_pos%nup == 3) {
454 pdf_page_rendersection(page, swf, x, y, info->xMin+x, info->yMin+y, info->xMax+x, info->yMax+y);
456 if(nup_pos % nup == nup-1)
457 swf_output_pagefeed(swf);
459 pdf_page_info_destroy(info);
461 pdf_page_render(page, swf);
462 swf_output_pagefeed(swf);
465 pdf_page_destroy(page);
469 if(!swf_output_save(swf, outputname))
471 msg("<notice> SWF written");
472 swf_output_destroy(swf);
476 if(viewer || preloader) {
477 #ifndef SYSTEM_BACKTICKS
478 msg("<warning> Not sure whether system() can handle command substitution");
479 msg("<warning> (According to config.h, it can't)");
489 if(viewer && !preloader) {
490 systemf("swfcombine %s `swfdump -XY \"%s\"` \"%s\" viewport=\"%s\" -o \"%s\"",zip,
491 outputname, viewer, outputname, outputname);
495 if(preloader && !viewer) {
496 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
497 ret = systemf("swfcombine %s `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,
498 preloader, SWFDIR, preloader, outputname, outputname);
502 if(preloader && viewer) {
503 systemf("swfcombine \"%s\" viewport=%s -o __tmp__.swf",
504 viewer, outputname, outputname);
505 systemf("swfcombine %s `swfdump -XY \"%s\"` `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\"",zip,
506 outputname, preloader, SWFDIR, preloader, outputname);
507 systemf("rm __tmp__.swf");