Force landscape printing with PrintJob
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
April 11th, 2007 at 4:40 pm
Thank you for figuring this out, I found it very useful!
June 13th, 2007 at 4:53 am
This solution you posted on flashcoders many months ago really helped last summer, so just wanted to say thanks
October 25th, 2007 at 11:55 pm
Hi, tnx for this example, it really helped me too.
December 19th, 2007 at 1:12 pm
[...] 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 [...]
February 27th, 2008 at 12:47 pm
This help me a lot, please keep up the good work.
July 7th, 2008 at 1:04 pm
Hello, thank you very much!
Very good code, you are a blessed person!
August 4th, 2008 at 4:17 am
Been looking for something like this for ages! Any chance of this in AS3?