We help IT Professionals succeed at work.

Flex Printing

Drachs asked
Last Modified: 2012-06-27
I'm trying to get printing working in flex for a reporting application I'm working on.  In particular, I have a control which extends "image".   It loads a chart from the webserver.   The control is programed to send the width and height numbers of itself as arguments with the url so that the script on the server can generate a chart the right size.

When I try to print this control I have to resize it using data the data returned from "printJob.pageWidth", but that property is only accessible after a call to printJob.start();.   I then have to come up with some way to wait until the new url is loaded so that I can continue with the printing.   I tried setting up a callback on "complete", but the callback takes over 20 seconds to fire off.   When it does, I get a "Error #2057: The page could not be added to the print job" exception from the call to printJob.addObject.

The image itself always loads in a fraction of a second, so I don't know why the complete call takes so long.   Does anybody have any idea's about how I can go about printing this control?
// The printing snippet
var Printing = false;
var page:VBox = new VBox();
var printJob:FlexPrintJob = new FlexPrintJob();
public function bdLoadComplete(bd:int):void
	if (Printing)
		Printing = false;
		printJob.addObject(page, FlexPrintJobScaleType.SHOW_ALL);
public function onPrint():void
	Printing = true;
// This is the definition of my bd1 object
package CIPA
	import mx.controls.Image;
	import mx.controls.Alert;
	public class BandwidthGraph extends Image
		public var url:String;
		private var urlWidth:Number = -1;
		private var urlHeight:Number = -1;
		public function BandwidthGraph()
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
			if ((unscaledWidth != 0 && unscaledHeight != 0) && (unscaledWidth != urlWidth || unscaledHeight != urlHeight))
				//Alert.show("Width:"+unscaledWidth+" Height: "+unscaledHeight);				
				super.source = url+"&width="+unscaledWidth+"&height="+unscaledHeight;
				urlWidth = unscaledWidth;
				urlHeight = unscaledHeight;
			super.updateDisplayList(unscaledWidth, unscaledHeight);	

Open in new window

Watch Question

This line caught my eye:
 if ((unscaledWidth != 0 && unscaledHeight != 0) && (unscaledWidth != urlWidth || unscaledHeight != urlHeight))

If the image size doesn't match the url size, you reload the image. Why are you doing this, especially in that section of the code? If you need to do that the proper way would be to add an event listener for Event.COMPLETE to your class, either in the constructor or better in a setter function for source, and then compare sizes when that event fires.

Also, you haven't set bd1's source property before calling load


I'm doing this because I want to reload a new image with the correct resolution every time the size of the image control is changed.   The image control is in a fluid layout, so that it adjusts to the size the user selects for his window.  If the user changes the size of the window, the image control changes in size and a new graph is loaded that looks good at the size it needs to be.


I set bd1's url property in the mxml and when the control receives it's first resize event it sets the source property in order to retreive the image.
This one is on us!
(Get your first solution completely free - no credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.