If you've ever tried to print a PDF file from a Flash projector you know what a pain it can be. Does the user have Acrobat installed? Which version? Why does Acrobat stay open after the printing is done? How do I print multiple PDF files? The solution is a lot simpler than you might think.



I'm not going to bore you with all of the arcane workarounds that people have proposed over the years, they're all crap. You can google them if you're really interested. Instead, I'm just going to present the solution so you can start using it.

Your new secret weapon for dealing with PDF files

The secret is actually a not-very-secret utility created by Krzysztof Kowalczyk called SumatraPDF. This is a very small (1.5MB), standalone, super-fast, feature-rich and FREE PDF viewer with a host of very useful command line options.

Go ahead and download the portable (ZIP) version of SumatraPDF and I'll show you how to use it to print PDF files using SWF Studio and how to print a PDF file from a standard Flash projector.

Printing PDF files from SWF Studio

The first example we'll look at is using SWF Studio to print a list of PDF files. The list could be a list of files selected by the user or a hard coded list in your projector. SWF Studio gives you a number of ways to create the file list and this example can be used to print any nunber of PDF files. For now, we'll send it a list of fully qualified paths to PDF files.

1. Extract SumatraPDF.exe from the ZIP file (it's the only file in there).
2. Create a new FLA (AS2 or AS3) called printpdf.fla and put this code in it.

ssCore.init();
ssDefaults.synchronousCommands = true;

function printPDFFiles(files:Array)
{
   // this is where we'll create a BAT file
   
   var batfile:String = "";
   
   // set up the defaults we'll be using
   
   var sumatra:String = '"' + ssGlobals.ssTempDir + "\\SumatraPDF.exe" + '"';
   var options:String = "-restrict -reuse-instance -print-to-default ";

   for (var i=0; i<files.length; i++)
   {
      var path:String = '"' + files[ i ] + '"';

      // tell SumatraPDF to quit when it's done
      // printing the last file
      
      if (i == files.length-1)
         options += "-exit-on-print ";

      // add the ith print command to our bar file
      
      batfile += sumatra + " " + options + " " + path + "\r\n";
   }

   // save the contents of batfile to a file and launch it
   // the printing will start immediately
   
   r = ssCore.FileSys.writeToFile({path:"tempdir://printpdfs.bat", data:batfile});

   if (r.success)
      ssCore.Shell.open({path:"tempdir://printpdfs.bat", winState:"HIDDEN"});
   else
      ssCore.App.showMsgBox({prompt:"whooops, something went wrong"});
}

// create an array of files that we want to print
// and then pass the array to printPDFFiles()

var files:Array = [
   ssGlobals.ssStartDir+"\\test1.pdf",
   ssGlobals.ssStartDir+"\\test2.pdf"
];

printPDFFiles(files);

3. Publish your FLA to create printpdf.swf
4. Create a new SWF Studio project file using printpdf.swf as the main movie
5. Drop a copy of SumatraPDF.exe on the Files Tab
6. Enable the FileSys plugin on the Plugins Tab
7. Build your project to create printpdf.exe
8. Put a copy of printpdf.exe, test1.pdf and test2.pdf in the same folder
9. Run printpdf.exe, this will send the two PDF files to the default printer

Printing PDF files from a standard Flash projector

We can't create BAT files on-the-fly like we did with SWF Studio so you have to prepare a BAT file for every PDF (or combination of PDF files) that you want to print. Even with this limitation, this solution is miles ahead of what you've probably been doing to print PDFs.

1. Create a BAT file that contains the follwoing code and call it print1.bat

SumatraPDF.exe -exit-on-print -print-to-default test.pdf
2. Download a copy of proxy.exe and rename it to print1.exe
3. Put print1.bat and print1.exe in a new folder called fscommand
4. Put test.pdf in the fscommand folder
5. Put a copy of SumatraPDF.exe in the fscommand folder
6. Create a new FLA called printpdf.fla and put the folling code in it

fscommand("EXEC", "print1.exe");
7. Publish your FLA as a Windows Projector to create printpdf.exe
8. Put a copy of printpdf.exe beside the fscommand folder you created
9. Run printpdf.exe, this will send test.pdf to the default printer