Skip to content

Instantly share code, notes, and snippets.

@davestewart
Created September 14, 2011 22:43
Show Gist options
  • Save davestewart/1218032 to your computer and use it in GitHub Desktop.
Save davestewart/1218032 to your computer and use it in GitHub Desktop.
A4Page class
package utils
{
import flash.display.Bitmap;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.printing.*;
import com.greensock.layout.*
/**
* @usage var page:A4Page = new A4Page(new Content());
* page.print();
* @author Dave Stewart
*/
public class A4Page extends Sprite
{
// ---------------------------------------------------------------------------------------------------------------------
// { region: Variables
// stage instances
var page :Sprite;
var area :AutoFitArea;
// properties
var loader :Loader;
// variables
// ---------------------------------------------------------------------------------------------------------------------
// { region: Instatiation
public function A4Page(content:DisplayObject = null)
{
// elements
page = new Sprite();
addChild(page);
area = new AutoFitArea(page, 10, 10, 822, 575);
// content
if (content)
{
this.content = content;
}
}
// ---------------------------------------------------------------------------------------------------------------------
// { region: Public methods
/**
* Prints the content
*/
public function print():void
{
var printJob :PrintJob = new PrintJob();
var state :Boolean = printJob.start();
if(state)
{
trace
(
[
printJob.paperWidth,
printJob.paperHeight,
printJob.pageWidth,
printJob.pageHeight,
printJob.orientation
]
);
if (printJob.orientation == PrintJobOrientation.PORTRAIT)
{
page.rotation = -90;
}
printJob.addPage(page);
printJob.send();
}
}
// ---------------------------------------------------------------------------------------------------------------------
// { region: Accessors
/**
* Sets content and scales it to A4 page size
*/
public function set content(content:DisplayObject):void
{
page.addChild(content);
area.attach(content, ScaleMode.PROPORTIONAL_INSIDE, AlignMode.CENTER, AlignMode.CENTER, true);
}
public function set url(url:String)
{
// loader
loader = new Loader();
loader.load(new URLRequest(url));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
addChild(loader);
}
// ---------------------------------------------------------------------------------------------------------------------
// { region: Handlers
protected function onLoadComplete(event:Event):void
{
// set bitmap and smoothing
var bitmap :Bitmap = loader.content as Bitmap
bitmap.smoothing = true;
// dispatch complete event
this.content = loader;
// print
print();
}
// ---------------------------------------------------------------------------------------------------------------------
// { region: Protected methods
// ---------------------------------------------------------------------------------------------------------------------
// { region: Utilities
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment