How to print with AS3
February 18th, 2010 in Flash | 18 CommentsIn this short tutorial, let’s see how we can print the content of our swf files using the PrintJob class with actionscript 3.
1. Create a new flash file (Actionscript 3.0) and save it as print.fla.
2. Rename “layer1″ to “Content” and here create the content that you want to print (images, text …). Convert the whole content into a movie clip and give it an instance name of “content_mc”.
3. Create a new layer at the top named “button” and create the print button. Give it an instance of print_btn.
4. Finally create the “actions” layer. Open the actions panel.
First add a Click event listener to the button that willl be handled by the printContent function.
Inside the function, in order to print the content we use the PrintJob class. To do so we create a new PrintJob instance.
If needed we resize the content to fit within the page dimensions. Then we add the content as a page to the print job and send it to the printer.
print_btn.addEventListener(MouseEvent.CLICK,printContent);
function printContent(evt:MouseEvent) {
var printJob:PrintJob = new PrintJob();
if (printJob.start()) {
if (content_mc.width>printJob.pageWidth) {
content_mc.width=printJob.pageWidth;
content_mc.scaleY=content_mc.scaleX;
}
printJob.addPage(content_mc);
printJob.send();
}
}
5. Test your movie to see the code in action and print your content …






Feb 18th, 2010
has this been tested on a mac?
i’ve used a print function in as3 before, but only works when printing from a PC.
any mac users able to print?
Feb 19th, 2010
yep, it works on a MAc, just “view demo” and try to print
Feb 20th, 2010
Great!
Feb 25th, 2010
Great tutorial… Thanks!
Mar 5th, 2010
gr8 snippet thanks
Mar 20th, 2010
ur website is very useful for learning about flash……..
thanks…
growth your service!!!
Mar 25th, 2010
Thanks alot…its working….:)
Apr 9th, 2010
I’d like to use it with ActionScript 2.0?
Any idea?
Apr 9th, 2010
can I print the whole swf? help
Apr 13th, 2010
MOISO > Yes you can, but you’ll need to put your whole content in a single Sprite. Basicaly, when building your application, go a bit like this:
var dummyStage:Sprite = new Sprite();
stage.addChild(dummyStage);
And then, you use dummyStage as your “Stage” instead of the normal Stage. When you want to print, just use printJob.start(dummyStage);
Your whole SWF will be printed.
Apr 14th, 2010
Hi,
thank You very much for this tutorial. I have one question- can I set what area should be printed? I mean- I have presentation with menu at the bottom and I don’t want print this menu and I can’t move everything into one MC. Can I manually set area of SWF to print?
Sincerelly
Apr 23rd, 2010
Ok, nevermind. I found solution.
May 5th, 2010
thanks…
thanks…
thanks…
thanks…
May 11th, 2010
[...] How to print with AS3 | RiaCodes Posted on May 11, 2010 by lookatme0128 How to print with AS3 | RiaCodes. [...]
May 26th, 2010
thanks so easy, eii could it be possible to put a tutorial to make a download button that the user can download pdf files, Word, ppt any file with AS3 flash cs4. (sorry about my english, use google translator)XD
Jun 12th, 2010
This is the coolest snippet ever. I have been trying to learn this for days and it seems so easy now that I have seen this. All the others use a class to accomplish a reletively easy task. Very good work, thanks for sharing with us all.
Jun 12th, 2010
It{s possible skip selection printer window? I must print “directly” the job in the default printer. I develping a kiosk application and need print a receipt (ticket)..
thanks a lot in advance
Jul 16th, 2010
[...] http://www.riacodes.com/flash/how-to-print-with-as3/ As3 Print Code Second Tutorial [...]