Skip to content

Instantly share code, notes, and snippets.

@liudonghua123
Forked from mems/gist:5301297
Created January 3, 2018 03:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save liudonghua123/4cb98f4ef1a12eb23fb9056377795c9d to your computer and use it in GitHub Desktop.
Save liudonghua123/4cb98f4ef1a12eb23fb9056377795c9d 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

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