Wednesday, November 28, 2012

Printing from Flash


It seems like an oxymoron, but there can be times you need to print documents from within Flash.  Whether you need to print certificates of completion, supplemental materials, or something that the user works to create within the Flash environment, there can be legitimate reasons you need to kill trees and use toner.

The simplest way to offer something printable through your module is to create a PDF that is linked from the Flash (AS3) document with the following code:
var url:URLRequest = new URLRequest(“myDocument.pdf”);
navigateToURL(url,”blank”);
However, that only works if you want your learner to print a document that is previously created and static.  If you want a custom document that includes material the user helps to create while using your Flash module, the work needs to be done in Flash.

At a high level, you will create a single movieclip (mc) in Flash that will later be printed out.  When printing, the mc will scale to fit on the page, so it is helpful to set up your mc using the aspect ratio of the anticipated printout.  For example, if you are expecting a printout on an 8.5” x 11” paper, you can set your printable mc to 850 x 1100. 

As the data displayed on the screen is most likely formatted differently than your printout, you should position your printable mc well off the stage.  It will not be visible to the user, but it will be available for printing.

In the example below, we are going to print out a certificate of completion.  We have created a mc named print1_mc.  On that mc, we have two text fields that will be populated with content from the module: name and score.  To set the text fields on the print1_mc, you execute the code as you would for a mc on the stage, except specify the mc first: print1_mc.txtName = strName; print1_mc.txtScore = intScore;

The following code should be executed with some trigger event (either a button click, a frame access, etc).

// set up the print job to communicate with the printer
var printJob:PrintJob = new PrintJob();
if(printJob.start())
{
     // scale your mc to match the width of the printout
     print1_mc.width = printJob.pageWidth;
     // adjust the height to the same scale
     print1_mc.scaleY = print1_mc.scaleX;
     if(print1_mc.height > printJob.pageHeight)
     {
          // if the mc is taller than the printout allows, scale to the height instead
          print1_mc.height = printJob.pageHeight;
          print1_mc.scaleX = print1_mc.scaleY;
     }
     // add your page to the print job
     printJob.addPage(print1_mc);
     // send the printout to the printer (actually opens the print dialogue)
     printJob.send();
}

If you want to print multiple pages, you should create an additional mc for each page.  Assuming you set all of their sizes the same, you can use the following code for each of them after you have scaled the first page:
print2_mc.scaleX = print2_mc.scaleY = print1_mc.scaleX;
printJob.addPage(print2_mc);
You can add that code for each subsequent page prior to the printJob.send() command in order to send them as a single job to the printer.

No comments:

Post a Comment