Skip to content

Instantly share code, notes, and snippets.

@mems
Last active February 23, 2022 13:12
Show Gist options
  • Save mems/5301297 to your computer and use it in GitHub Desktop.
Save mems/5301297 to your computer and use it in GitHub Desktop.
How to convert a (simple) SWF to PDF

Convert (simple) SWF to PDF

In SWFTools a programs called gfx2gfx is available to do this task, but it's not officially available precompiled.

It can convert SWF, PDF or image to SWF, PDF, ebook or image.

For discard downscaling for rasterized image (default: 72dpi) we use the provided by @m-p-y: add -r attribute to increase maxdpi

Requirements

To compile it, you need pdflib and patch, with compilable sources (./configure && make && make install) or install it directly via macport (OSX only) (sudo port install pdflib patch). For Linux you need to install SWFTools deps: sudo apt-get install libfontconfig1-dev libfftw3-dev libzzip-dev libpoppler-dev libjpeg62-dev libgif-dev libpng12-dev. (see also)

Create a file make.sh, copy following code, set as executable (chmod +x make.sh) and execute it (./make.sh) :

#!/bin/bash

# Git is required here to download sources
# Remove folder if exist
rm -rf ./swftools
git clone --depth=1 git://github.com/matthiaskramm/swftools ./swftools
cd swftools

# Apply patch by @m-p-y
patch -p1 -f -i - <<EOF
diff -ur a/Makefile.in b/Makefile.in
--- a/Makefile.in	2016-03-13 01:08:14.000000000 +0100
+++ b/Makefile.in	2016-03-13 01:09:13.000000000 +0100
@@ -1,3 +1,4 @@
+vpath = src
 top_builddir = ..
 srcdir = @srcdir@
 top_srcdir = @top_srcdir@
diff -ur a/src/gfx2gfx.c b/src/gfx2gfx.c
--- a/src/gfx2gfx.c	2016-03-13 01:08:14.000000000 +0100
+++ b/src/gfx2gfx.c	2016-03-13 00:58:31.000000000 +0100
@@ -50,6 +50,7 @@
 
 static char * outputname = 0;
 static int loglevel = 3;
+static char * maxdpi = "72";
 static char * pagerange = 0;
 static char * filename = 0;
 static const char * format = 0;
@@ -77,6 +78,11 @@
         setConsoleLogging(loglevel);
 	return 0;
     }
+	else if (!strcmp(name, "r"))
+    {
+           maxdpi = val;
+               return 1;
+    }
     else if (name[0]=='p')
     {
 	do {
@@ -110,7 +116,7 @@
     }
     else if (!strcmp(name, "V"))
     {	
-	printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
+	printf("gfx2gfx - part of %s %s\n", PACKAGE, VERSION);
 	exit(0);
     }
     else 
@@ -126,6 +132,7 @@
  {"q","quiet"},
  {"V","version"},
  {"s","set"},
+ {"r","resolution"},
  {"p","pages"},
  {0,0}
 };
@@ -264,6 +271,7 @@
 	    msg("<error> Invalid output format: %s", format);
 	    exit(1);
 	}
+	out->setparameter(out, "maxdpi", maxdpi);
 
         int pagenr;
         for(pagenr = 1; pagenr <= doc->num_pages; pagenr++) 
EOF

./configure
make
cd lib
make
cd ../src
make gfx2gfx
echo "gfx2gfx bin should be compiled in $(pwd)"

If you get the error Undefined symbols for architecture x86_64: "_gfxdevice_pdf_init", that means pdflib is not installed

Usage

gfx2gfx file.swf -o file.pdf

Example: Non downloadable Slideshare document to PDF

This example use gfx2gfx to convert slides of slideshare slide show to PDFs

It use this slide show

View source of HTML, and find:

  • ID: like "doc":"xxx-xxx-phpapp0x" (just search "doc":)
  • slides count: like "slide_count":xx (just search "slide_count":)

Create a file download_convert.sh (set it executable), copy following code and change the path of gfx2gfx tool and founded values

#!/bin/bash

# ID
doc="neumar2013final-130325115515-phpapp01"

# Number of slides
numSlides=51

# Path to gfx2gfx tool
gfx2gfx="/usr/local/bin/gfx2gfx"

echo "See also:"
echo "http://s3.amazonaws.com/slideshare/$doc.pdf"
echo "http://s3.amazonaws.com/ppt-download/$doc.pdf"
echo "http://s3.amazonaws.com/ppt-download/$doc.ppt"
echo "http://s3.amazonaws.com/slideshare/$doc.xml"

mkdir "$doc"
cd "$doc"

for (( slide=1; slide<=$numSlides; slide++ ))
do
	wget --no-check-certificate -O "slide-$slide.swf" "https://slideshare.s3.amazonaws.com/$doc-slide-$slide.swf"
	"$gfx2gfx" -p400 "slide-$slide.swf" -o "slide-$slide.pdf"
done
echo "Now you can merge all slides to one PDF!"

Example: Non downloadable Issuu document to PDF

View source of HTML, and find:

  • ID: like "documentId":"xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  • slides count: like "pageCount":xxx

Create a file download_convert.sh (set it executable), copy following code and change the path of gfx2gfx tool and founded values

#!/bin/bash

# ID
doc="090224121313-2f028222e017481ba99f3ba2800c8167"

# Number of slides
numSlides=11

# Path to gfx2gfx tool
gfx2gfx="/usr/local/bin/gfx2gfx"

echo "See also:"
echo "http://document.issuu.com/$doc/document.xml"

mkdir "$doc"
cd "$doc"

for (( slide=1; slide<=$numSlides; slide++ ))
do
	wget --no-check-certificate -O "slide-$slide.swf" "https://page.issuu.com/$doc/swf/page_$slide.swf"
	"$gfx2gfx" -p400 "slide-$slide.swf" -o "slide-$slide.pdf"
done
echo "Now you can merge all slides to one PDF!"

Notes

To merge separate PDF to one document, you can use "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" -o pages.pdf page*.pdf (OSX only) or gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=pages.pdf page*.pdf or convert of ImageMagic convert -compress jpeg -quality 90 page*.pdf pages.pdf (but rasterise all pages) or pdftk or pdfunite or use pdfmerge from PDFlib-Lite (exist in C, PHP, Java, etc.)...

Works on OSX 10.11.3

Based on:

See also

Thanks for your usefull comments

@dllud
Copy link

dllud commented Nov 12, 2013

Works perfectly. Thanks for creating this HOWTO.

Here is a time saver for those using Debian or some Debian based distro. To install SWFTools' compilation dependencies run:

sudo apt-get install libfontconfig1-dev libfftw3-dev libzzip-dev libpoppler-dev

@TonyStark
Copy link

How to prevent downscale image and keep HQ image??
NOTICE Downscaling 318x292 image (dpi 143.753888, 160x146 on page) to 159x146 (dpi 72)

@JustOff
Copy link

JustOff commented Nov 4, 2014

The q&d way to prevent downscale is patch i->config_maxdpi = 72; in lib\devices\pdf.c

@jayachar88
Copy link

Unable to build on Debian. Have taken care of all the prerequisites, including building/install PDFlib from source. The 'make' fails with:

make[2]: Entering directory `/home/bdutta/swftools/lib'
cd pdf;make libgfxpdf
make[3]: Entering directory `/home/bdutta/swftools/lib/pdf'
make[3]: Nothing to be done for `libgfxpdf'.
make[3]: Leaving directory `/home/bdutta/swftools/lib/pdf'
make[2]: Leaving directory `/home/bdutta/swftools/lib'
/home/bdutta/swftools/src
g++ -DHAVE_CONFIG_H pdf2pdf.o -o pdf2pdf ../lib/libgfxswf.a ../lib/librfxswf.a ../lib/libgfxpdf.a ../lib/libgfx.a ../lib/libbase.a -L/usr/local/lib -lzzip -lpdf -lz -lm -L/usr/lib/i386-linux-gnu  -lz -lpng12 -lfontconfig -lfreetype -lfftw3f -lstdc++
g++: error: ../lib/libgfxpdf.a: No such file or directory
make[1]: *** [pdf2pdf] Error 1
make[1]: Leaving directory `/home/bdutta/swftools/src'
make: *** [all] Error 2

Tried to look at the reason why libgfxpdf.a is not found, and I realized that there are no production rules to building it in the makefile. Wonder why no one else has seen this problem ? While I suspect that I might be doing something wrong, but I've rerun through the instructions very thoroughly, several times.

@ardavank
Copy link

Unable to build on Mac OS X Yosemite 10.10.2

3 warnings generated.
gcc -DHAVE_CONFIG_H wav2swf.o -o wav2swf ../lib/librfxswf.a ../lib/libbase.a -L/opt/local/lib -L/usr/local/lib -lpdf -lz -lm  -L/opt/X11/lib -lfreetype -lz -lbz2
Undefined symbols for architecture x86_64:
  "_compute_scalefacs_short", referenced from:
      _short_block_scalefacs in librfxswf.a(vbrquantize.o)
  "_find_scalefac_ave", referenced from:
      _short_block_sf in librfxswf.a(vbrquantize.o)
      _long_block_sf in librfxswf.a(vbrquantize.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [wav2swf] Error 1
make: *** [all] Error 2
gcc -c -DHAVE_CONFIG_H   -I/opt/local/include -I/opt/X11/include/freetype2 -I/opt/X11/include -Ilame -Ilib/lame -fPIC -Wimplicit -Wreturn-type -Wno-write-strings -Wformat -O -fomit-frame-pointer  gfx2gfx.c -o gfx2gfx.o
cd ../lib;/Applications/Xcode.app/Contents/Developer/usr/bin/make libgfxpdf.a;cd -
cd pdf;/Applications/Xcode.app/Contents/Developer/usr/bin/make libgfxpdf
make[2]: Nothing to be done for `libgfxpdf'.
/Users/Ardavan/Downloads/SWFTOOLS/swftools/src
g++ -DHAVE_CONFIG_H gfx2gfx.o -o gfx2gfx ../lib/libgfxswf.a ../lib/librfxswf.a ../lib/libgfxpdf.a ../lib/libgfx.a ../lib/libbase.a -L/opt/local/lib -L/usr/local/lib -lpdf -lz -lm  -L/opt/X11/lib -lfreetype -lz -lbz2 -lstdc++
clang: error: no such file or directory: '../lib/libgfxpdf.a'
make: *** [gfx2gfx] Error 1

@domdomegg
Copy link

Managed to get it compiled without any errors, only now it seems to produce thin black borders around images.

Example pdf - http://www50.zippyshare.com/v/AV9orjSw/file.html

@domdomegg
Copy link

@AWDGroup

Do:

cd ../lib
make
cd ../src
make gfx2gfx

@m-p-y
Copy link

m-p-y commented Apr 11, 2015

@TonyStark

Here is a patch that implements a parameter -r for gfx2gfx, taking the max. resolution as argument:

diff --git a/src/gfx2gfx.c b/src/gfx2gfx.c
index fcace34..957b472 100644
--- a/src/gfx2gfx.c
+++ b/src/gfx2gfx.c
@@ -50,6 +50,7 @@ static gfxsource_t*driver = 0;

 static char * outputname = 0;
 static int loglevel = 3;
+static char * maxdpi = "72";
 static char * pagerange = 0;
 static char * filename = 0;
 static const char * format = 0;
@@ -77,6 +78,11 @@ int args_callback_option(char*name,char*val) {
         setConsoleLogging(loglevel);
        return 0;
     }
+    else if (!strcmp(name, "r"))
+    {
+           maxdpi = val;
+               return 1;
+    }
     else if (name[0]=='p')
     {
        do {
@@ -110,7 +116,7 @@ int args_callback_option(char*name,char*val) {
     }
     else if (!strcmp(name, "V"))
     {  
-       printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
+       printf("gfx2gfx - part of %s %s\n", PACKAGE, VERSION);
        exit(0);
     }
     else 
@@ -126,6 +132,7 @@ struct options_t options[] =
  {"q","quiet"},
  {"V","version"},
  {"s","set"},
+ {"r","resolution"},
  {"p","pages"},
  {0,0}
 };
@@ -264,6 +271,7 @@ int main(int argn, char *argv[])
            msg("<error> Invalid output format: %s", format);
            exit(1);
        }
+       out->setparameter(out, "maxdpi", maxdpi);

         int pagenr;
         for(pagenr = 1; pagenr <= doc->num_pages; pagenr++) 

@mvelbaum
Copy link

@jayachar88
To properly build on debian/ubuntu you need (in addition to what @dllud said):

@Karting06
Copy link

Thank you for this.
I also needed to install g++ on Kubuntu 14.04 LTS (sudo apt-get install g++).
I also needed to run the "make" script twice (the first time, it stops before the end due to an error, the second it compiles it without problems).

@MarioVillafaina
Copy link

Unable to build on Mac OS X El Capitan 10.11.3

3 warnings generated.
gcc -DHAVE_CONFIG_H wav2swf.o -o wav2swf ../lib/librfxswf.a ../lib/libbase.a -L/opt/local/lib -L/opt/local/lib -L/usr/local/lib -lpdf -lz -lm  -L/opt/X11/lib -lfreetype
Undefined symbols for architecture x86_64:
  "_compute_scalefacs_short", referenced from:
      _short_block_scalefacs in librfxswf.a(vbrquantize.o)
  "_find_scalefac_ave", referenced from:
      _short_block_sf in librfxswf.a(vbrquantize.o)
      _long_block_sf in librfxswf.a(vbrquantize.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [wav2swf] Error 1
make: *** [all] Error 2
gcc -c -DHAVE_CONFIG_H  -I/opt/local/include -I/opt/local/include -I/opt/X11/include/freetype2 -Ilame -Ilib/lame -fPIC -Wimplicit -Wreturn-type -Wno-write-strings -Wformat -O -fomit-frame-pointer  gfx2gfx.c -o gfx2gfx.o
cd ../lib;/Applications/Xcode.app/Contents/Developer/usr/bin/make libgfxpdf.a;cd -
cd pdf;/Applications/Xcode.app/Contents/Developer/usr/bin/make libgfxpdf
make[2]: Nothing to be done for `libgfxpdf'.
/Users/user/Desktop/swftools/src
g++ -DHAVE_CONFIG_H gfx2gfx.o -o gfx2gfx ../lib/libgfxswf.a ../lib/librfxswf.a ../lib/libgfxpdf.a ../lib/libgfx.a ../lib/libbase.a -L/opt/local/lib -L/opt/local/lib -L/usr/local/lib -lpdf -lz -lm  -L/opt/X11/lib -lfreetype -lstdc++
clang: error: no such file or directory: '../lib/libgfxpdf.a'
make: *** [gfx2gfx] Error 1

@sathish100
Copy link

I am getting the below error message when I run the command "sudo port install pdflib patch".

Warning: xcodebuild exists but failed to execute
Warning: Xcode does not appear to be installed; most ports will likely fail to build.
---> Cleaning pdflib
Error: Port patch not found

Kindly check and advise.

Regards
Sathish

@SergeGardien
Copy link

I've succesfully compiled SWFTools for Mac OS X 10.11.6 by using the above bash script and by fixing the jpeg.c file (as per thread https://github.com/matthiaskramm/swftools/issues/37) given that I had some errors.

In particular I've compiled gfx2gfx without errors but if I use it with the command:
gfx2gfx test.swf -o test.pdf
I get the following errors:

Error: ID 142 unknown
Error: ID 145 unknown
Error: ID 148 unknown
Error: ID 151 unknown
Error: ID 154 unknown
Error: ID 157 unknown
Error: ID 160 unknown
Error: ID 163 unknown
Error: ID 166 unknown
Error: ID 169 unknown
Error: ID 172 unknown
Error: ID 175 unknown
Error: ID 178 unknown
Error: ID 181 unknown
Error: ID 184 unknown
Error: ID 187 unknown
Error: ID 190 unknown
Error: ID 193 unknown
Error: ID 196 unknown
Error: ID 199 unknown
Error: ID 202 unknown
Error: ID 205 unknown
Error: ID 208 unknown
Error: ID 211 unknown
Error: ID 214 unknown
Error: ID 217 unknown
Error: ID 220 unknown
Error: ID 223 unknown
Error: ID 226 unknown
Error: ID 229 unknown
Error: ID 232 unknown
Error: ID 235 unknown
Error: ID 238 unknown
Error: ID 241 unknown
Error: ID 244 unknown
Error: ID 247 unknown
Error: ID 250 unknown
Error: ID 253 unknown
Error: ID 256 unknown
Error: ID 259 unknown
Error: ID 262 unknown
Error: ID 265 unknown
Error: ID 268 unknown
Error: ID 271 unknown
Error: ID 274 unknown
Error: ID 277 unknown
Error: ID 280 unknown
Error: ID 283 unknown
Error: ID 286 unknown
Error: ID 289 unknown
Error: ID 292 unknown
Error: ID 295 unknown
Error: ID 298 unknown

Do you have any idea what this is? How can I fix it?

@kumakichi
Copy link

@jayachar88

I came across the same problem as yours, and i'm using Ubuntu 16.04, and i installed all the dependencies:

sudo apt-get install libfontconfig1-dev libfftw3-dev libzzip-dev libpoppler-dev libjpeg62-dev libgif-dev libpng12-dev libfontconfig1-dev libfftw3-dev libzzip-dev libpoppler-dev

at last, i found the reason,it is the PATH

i ever do some arm development, when searching freetype-config, it is found in arm cross compile directory, not /usr/bin

after reset PATH, then do ./configure,you can see there indeed has content in lib/pdf/Makefile

maybe we are the same kind of problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment