How to print with AS3
February 18th, 2010 in Flash | 35 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 [...]
Sep 11th, 2010
Do you know how to tell if a print job has finished? Is there an event to capture? Or even that a print job has been accepted by the user, not just canceled.
Sep 22nd, 2010
function PrintStage(evt:MouseEvent) {
var printJob:PrintJob = new PrintJob();
var options:PrintJobOptions = new PrintJobOptions();
options.printAsBitmap = true;
trace(“print called!”);
if (printJob.start()) {
var printSprite = new Sprite();
var bitmapData:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight);
bitmapData.draw(stage);
var screenShot:Bitmap = new Bitmap(bitmapData);
printSprite.addChild(screenShot);
//========== printjob bug fix – prevent blank pages: ==========
printSprite.x = 2000; //keep it hidden to the side of the stage
stage.addChild(printSprite); //add to stage – prevents blank pages
//=============================================================
trace(“before printSprite width: ” + printSprite.width + ” printJob.pageWidth: ” + printJob.pageWidth);
//scale it to fill the page (portrait orientation):
var myScale:Number;
myScale = Math.min(printJob.pageWidth/printSprite.width, printJob.pageHeight/printSprite.height);
printSprite.scaleX = printSprite.scaleY = myScale;
var printArea:Rectangle = new Rectangle(0, 0, printJob.pageWidth/myScale, printJob.pageHeight/myScale);
trace(“after printSprite width: ” + printSprite.width + ” printJob.pageWidth: ” + printJob.pageWidth);
printJob.addPage(printSprite,printArea,options);
printJob.send();
stage.removeChild(printSprite);
printSprite = null;
}
}
Oct 14th, 2010
following the sceptic comments above about if this works on macs I enthusiasticly tried your live demo and woohoo, it works.
so I go ahead an download the source, what?…the printing gives the mystical mac blank page print again. even downloading the swf from your webserver and executing it locally I get the same problem.
would have loved to believe that this is the fix for the blank page printing problem with macs, but for me, it unfortunately, this code has the same problem as oh so many others.
Nov 25th, 2010
wow, it’s work. thanks
Dec 17th, 2010
Beautiful!
SIMPLE AND WORKS!
THANKS YOU SO MUCHHHH…..!!! ^^
Dec 20th, 2010
THANK YOU MAN !!!!!!!!!!!
Jan 5th, 2011
can anyone help me i want to print with my own size limitation like ex. (400 x 600 pixels)
how can i add it on this codes so i can print it like that?
Jan 25th, 2011
Love it!
After the print job has completed, what is the code to resize it back to the original content size?
Mar 9th, 2011
It works well for a movieclip.
Thank you very much.
How about some code for printing text from a plain text control, ie. not a movieclip?
Apr 13th, 2011
@VLADOX: awesome! your codes work best to capture whole area of running movieclip!
May 26th, 2011
Thanks for that been looking everywhere great snippet of generic code
May 31st, 2011
This is so simple and perfect!! Question…. Right now my ‘content_mc’ Is bigger then the screen size (height). The height of the ‘content_mc’ is over 1500. My height of the movie is 600. When I print this out, It only prints half way. How can I print everything in that MC?
I will later on put that in a scroll. Would I have problems printing if I make this intro a scroll?
Aug 26th, 2011
Hello ADMIN… thanks for the article…
VLADOX,
Thanks for re-generating the code…
My problem is I want to print the web page instead of swf contents… i need to write this code on a small flash button that will be included on the top of my page. But it should capture and print the current loaded page of website.
If you could kindly help….
Sincerely.
Sep 6th, 2011
Thanx, for Flash sample. I have made some adjustments to script to print stage which is nor in the sprite:
function printContent() {
var printJob:PrintJob = new PrintJob();
var content_mc:MovieClip=MovieClip(root);
if (printJob.start()) {
content_mc.x=50;
content_mc.y=50;
………
You can even move entire stage, or make some changes to it while printing. Just bring it back after print job.
Good luck.
Oct 10th, 2011
thanks. I was looking that for my new flash project.
Nov 10th, 2011
Hi
It was an amazing code…….
Also how to print the all frames in a single shot???
Dec 22nd, 2011
Helpfull for starters