If you're looking to convert a large amount of PDFs into FlashPaper files, have I got the script for you...
I was in a situation where I needed to convert thousands of existing PDFs into FlashPaper documents so that they could be loaded into a Flex 2 application (see my previous entry on How to load FlashPaper Documents in Flex 2). A separate business process created new PDF documents daily and placed them in a particular directory. This directory needed to be monitored, and all changes (old files being updated / new files being added) needed to be reflected in FlashPaper to keep everything in sync.
This type of repetitive, automated task is a perfect reason to know a scripting language on the side. As far as scripting languages go, Python probably would've been a great fit here. But, to be honest, I've never had a need to learn the ins and outs of Python. Believe it or not, everything that I've ever needed to do scripting-wise I've done successfully with ANT.
Now, when you think about ANT, you typically think about it for automating build processes since that's where its roots are and because it does that job quite well. But, really, when you explore the multitude of available tasks you can see just how powerful of a general purpose scripting tool it is. Because of my familiarity with ANT, it was pretty easy to create a "build" script to perform the automated conversion process.
Below is my ANT script for converting an entire directory of PDF files into FlashPaper files:
<!--
Copyright (c) 2006 Darron Schall
Released under the MIT License:
http://www.opensource.org/licenses/mit-license.php
-->
<project default="convert" basedir=".">
<!-- The location of the FlashPrinter.exe in the FlashPaper installation directory -->
<property name="flashprinter.exe" value="C:\Program Files\Macromedia\FlashPaper 2\FlashPrinter.exe" />
<!-- The target directory to look for PDF files to convert -->
<!--<property name="target.dir" value="C:\temp" />-->
<!-- The output directory to placed converted PDF files - must including the trailing \ -->
<!--<property name="output.dir" value="${target.dir}" />-->
<!-- Extension for ANT to allow for tasks like for and propertyregex -->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${basedir}/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<!-- Run the conversion -->
<target name="convert">
<!-- Loop over all of the PDF files in the directory -->
<for param="file">
<!-- Include all of the PDF files in the target directory as part of the loop -->
<fileset dir="${target.dir}" includes="*.pdf"/>
<sequential>
<!--
Get the file name so we can use the same name in the output. This grabs
everything from the trailing slash to the file extension.
-->
<propertyregex property="filenameMinusExtension" override="yes" input="@{file}"
regexp=".*\\([^\\]+)\.pdf" replace="\1" />
<!--
Compare the timestamp from the PDF to the FlashPaper, and convert to FlashPaper
if either PDF is newer, or if the FlashPaper version does not exist yet.
-->
<outofdate>
<sourcefiles>
<pathelement path="@{file}" />
</sourcefiles>
<targetfiles path="${output.dir}${filenameMinusExtension}.swf" />
<sequential>
<!-- Convert the PDF to FlashPaper -->
<exec executable="${flashprinter.exe}">
<arg line=""@{file}" -o "${output.dir}${filenameMinusExtension}.swf"" />
</exec>
<!--
Give some time for FlashPrinter to finish the conversion before
moving on to the next file to convert. This value is not scientific,
and you might need to adjust accordingly.
-->
<sleep seconds="30" />
</sequential>
</outofdate>
</sequential>
</for>
</target>
</project>
In order to run this script, you'll need to download the Ant Contrib Tasks support library and place it in your ANT lib directory. The ant-contrib library adds some useful tasks, such as <for>. Also, it should be obvious, but you'll have to have FlashPaper 2 installed (the registered version, so you don't have to click "try" for every PDF to convert).
The script is smart enough to only do the conversion if the target FlashPaper doesn't already exist (such as when a new PDF is added), or if the PDF file has changed and the FlashPaper version is out of date. Thus, you can run the script over and over on the same directory without having it convert all of the files every time.
Usage is simple. Copy the above code block into build.xml, then open a command window to where you saved the build.xml file and run:
ant -Dtarget.dir="C:\Documents And Settings\All Users\PdfDir" -Doutput.dir="C:\Pdf2FlashPaperResults\"
You'll need to have ANT in your PATH environment variable for the above command to work.
At runtime, you pass in the target.dir and output.dir properties (notice the -D in front of the property name). The target.dir property is the directory that contains the PDF files. The output.dir is the directory where you want the generated FlashPaper documents to be placed, and the trailing backslash needs to be included in the output.dir value. If you don't want to set these values at runtime, you can uncomment the lines in the build script to set default values, and then just run "ant".
Note that the build script has a "sleep for 30 seconds" after each conversion is started. You might need to modify this value, but 30 seconds worked for me through a little trial and error. The sleep is necessary to allow enough time for FlashPaper to finish the conversion process. It seems that the executable returns a result immediately instead of returning when the conversion is finished, so if you omit the sleep, the loop over the PDF files runs quite fast you'll get some unexpected results and missing conversions.
If you have any improvement suggestions, feel free to share. I've done enough testing with this to verify that it's working as expected for my use case, but I'm always open to bug fixes or improvements.
Enjoy!
Tags: FlashPaper, ANT
Leave a comment