maltaisn / swf-convert

Utility to convert static SWF files to PDF or SVG
GNU Lesser General Public License v3.0
23 stars 5 forks source link
conversion flash pdf svg swf

swf-convert

Command line application for converting SWF files to PDF or SVG. This program differs from existing alternatives by the fact that it's explicitly meant to convert back files that were originally in PDF (or other vector formats) and have been converted to SWF using programs such as swf-tools' pdf2swf or Adobe InDesign.

As such, only a subset of the SWF specification is supported, with no support for animations. See limitations for more details. Only static files can be converted. For example, this program could be be used to convert a book composed of one SWF file per page.

To reduce output file size, several features are available:

Table of contents

Download

version

The latest release can be found under the Releases section. Java 8 or later is required to run this program. The same build should be able to run on all desktop platforms.

Usage

The JAR file can be run using the following command:

java -jar swf-convert.jar [main options] <output format> <input files> [output options]

It can also take a configuration file:

java -jar swf-convert.jar @config.txt

Multiple input files or folders can be specified. A file collection is created for each file or folder specified. Some arguments will require to have the same number of arguments as there are input collections.

It is recommended to use -Xmx to increase heap size when converting large input collections (>1000 files) since the program uses a lot of memory, particularly for PDF frame rasterization. As much as 10 GB has been required in my case...

Main options

For use in place of [main options] in the above command.

Shared options

For use in place of [output options] in the above command. Shared by all output formats.

Fonts
Images
Downsampling images

PDF options

For use in place of [output options] in the above command, with the pdf output format. PDF output will produce one page per frame. The frames of all files in a collection are written to the same output file.

Rasterization options

SVG options

For use in place of [output options] in the above command, with the svg output format. SVG will produce one file per input frame.

When images and fonts are not embedded, the files are placed in the same directory as the output.

IR options

For use in place of [output options] in the above command, with the ir output format. IR will produce one JSON file per input frame.

When converting SWF to the chosen output format, the program first converts the SWF toa SVG-like intermediate representation in order to abstract the difficulties presented by the SWF format. For debugging purposes, it's possible to output this IR as JSON structures. Images and fonts are written as files.

Advanced options

For use in place of [output options] in the above command.

Keep in mind that these options are meant for advanced use or for debugging purposes. Otherwise common uses include:

Examples

1. PDF to SWF and back.

Here an arbitrary PDF with 92 pages is converted to SWF files with swf-tools' pdf2swf:

pdf2swf -o pages/%.swf -z input.pdf

The result is 92 SWF files named 1.swf to 92.swf in the pages directory. Now let's convert them back to a single PDF file using swf-convert:

java -jar swfconvert.jar pdf pages/ -o report.pdf 
    --image-format jpg --ignore-empty 
    -DfontScale2=[0.05,0.05,20,-20]

Additionally we'll ignore empty frames, use only JPG images, and we'll use the special font scale option needed for swf-tools. In a few seconds, the report.pdf file is created. 40 fonts were created out of the 719 contained in all input files, and 333 duplicate images were removed out of 344 images!

2. Self-contained SVG

A SWF file with a single frame is converted to a SVG:

java -jar swfconvert.jar --log 4
  svg input.swf -o output.svg
  --downsample-images --max-dpi 30 --image-format jpg
  --images-mode base64 --fonts-mode none
  --transform-precision 2 --no-prolog

Images are embedded and paths are used instead of fonts. Precision for the transform attribute is also increased to avoid rounding issues with fonts-mode none. To avoid making the SVG too big, images are also downsampled.

Limitations

swf-convert can only convert static files, with no support for animations or actions. Most other limitations arise from the fact that I had no test data to test some features with, so I opted for a lazy implementation. Here's a detailed list of current limitations.

Nearly all of these limitations will result in an exception being thrown, and the conversion will fail. If you ever have an use case needing support for one of the above, please open an issue and provide the required test data, I'll do my best to implement it. Again, this tool was implemented lazily as to cover my own use case and nothing more. More test data will surely allow to fill the holes.

When I say test data, I mean SWF files that can be converted and the result is visually compared with the original. I unfortunately didn't spend the time implementing automated testing that would do that.

PDF limitations

SVG limitations

Building

The project is built with Gradle, which can be run with:

./gradlew <tasks> [options]

Useful tasks are:

Contributing

All contributions are welcome. Please read contribution guidelines.

Changelog

View the CHANGELOG.md file for detailed release notes.

Licenses

This program is licensed under LGPL v3, see the license file for more details. It uses modified code for the following libraries, which can be found in the libsrc directory:

Other libraries are also used:

References