Avatar of Drachs
Drachs
 asked on

Flex Printing

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);
		removeChild(page);
		printJob.send();
		}					
	}
 
public function onPrint():void
	{		
	printJob.start();
				
	page.addChild(panelTopFiveUsers);
	page.addChild(panelTopFiveSites);
	page.addChild(bd1);
	addChild(page);
				
	page.width=printJob.pageWidth;
        page.height=printJob.pageHeight;
        		
	Printing = true;
	bd1.load();
	}
 
// 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()
			{
			super();
			}
		
		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

Apache Flex

Avatar of undefined
Last Comment
Drachs

8/22/2022 - Mon
Gary Benade

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
Drachs

ASKER
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.
Drachs

ASKER
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 is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Drachs

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.