Force landscape printing with PrintJob

March 16th, 2007 by Steven Sacks

So you want to make sure that you always print in landscape mode even if the user doesn't choose landscape, but the PrintJob property "landscape" is read only, and there's no way to force the user to choose landscape. In addition, PrintJob has some calculation inconsistencies with rotated clips. There is a solution, and here it is.

Pass this function a movieclip and it will make sure it prints in landscape mode. It rotates the clip, handles all the measurements, and rotates it back when it's done printing. This all happens invisibly to the user.

function printImage(mc:MovieClip):Void
{
        var realW:Number = mc._width;
        var realH:Number = mc._height;
        var orgX:Number = mc._x;
        var orgY:Number = mc._y;
        var pj:PrintJob = new PrintJob();
        var pageCount:Number = 0;
        if (pj.start())
        {
                mc._x = 0;
                mc._y = 0;
                var cXscale:Number, cYscale:Number;
                if (pj.orientation.toLowerCase() != "landscape")
                {
                        mc._rotation = 90;
                        mc._x = mc._width;
                        cXscale = (pj.pageWidth / realH) * 100;
                        cYscale = (pj.pageHeight / realW) * 100;
                }
                else
                {
                        cXscale = (pj.pageWidth / realW) * 100;
                        cYscale = (pj.pageHeight / realH) * 100;
                }
                mc._xscale = mc._yscale = Math.min(cXscale, cYscale);
                if (pj.addPage(mc, {xMin:0, xMax:realW, yMin:0, yMax:realH})) pageCount++;
        }
        if (pageCount > 0) pj.send();
        mc._xscale = mc._yscale = 100;
        mc._rotation = 0;
        mc._x = orgX;
        mc._y = orgY;
        delete pj;
}

Posted in Actionscript, Flash, Tips/Tricks

7 Responses

  1. Adam Smith

    Thank you for figuring this out, I found it very useful!

  2. Pete

    This solution you posted on flashcoders many months ago really helped last summer, so just wanted to say thanks

  3. Oskars

    Hi, tnx for this example, it really helped me too.

  4. Printing in landscape from Flash « PA/DE/NJ Distance Learning Association

    [...] Printing in landscape from Flash Flash developer Steven Sacks has code available on his site that allows you to print a .swf file in landscape format.  If the printer is set to portrait, Sasks’ code changes the orientation of the movieclip and adjusts the width to take up the space available on the page.  http://www.stevensacks.net/2007/03/16/force-landscape-printing-with-printjob [...]

  5. rafael artusi

    This help me a lot, please keep up the good work.

  6. Fabio (Brasil)

    Hello, thank you very much!
    Very good code, you are a blessed person!

  7. Fenn

    Been looking for something like this for ages! Any chance of this in AS3?

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

About Steven Sacks

I am a professional Flash developer with over 13 years of programming experience. I have consulted for high-profile agencies and companies in San Francisco, Los Angeles, Atlanta and New York, and developed numerous award-winning websites and rich internet applications for clients including Adobe, Fox Sports, FX Networks, Anheuser-Busch, GE, DirecTV, ESPN, The Weather Channel, Home Depot, and Coca-Cola.

I am the author of the open-source Gaia Framework for Adobe Flash, which dramatically reduces development time and makes developing Flash sites much easier.