How do I Print a dynamically created Flex component/chart that is not being displayed on the screen?

Posted by Adam Jones on Stack Overflow See other posts from Stack Overflow or by Adam Jones
Published on 2011-01-24T06:28:50Z Indexed on 2011/02/23 23:25 UTC
Read the original article Hit count: 205

Filed under:
|
|

I have a several chart components that I have created in Flex. Basically I have set up a special UI that allows the user to select which of these charts they want to print. When they press the print button each of the selected charts is created dynamically then added to a container. Then I send this container off to FlexPrintJob.

i.e.

    private function prePrint():void
    {
        var printSelection:Box = new Box();
        printSelection.percentHeight = 100;
        printSelection.percentWidth = 100;
        printSelection.visible = true;

        if (this.chkMyChart1.selected)
        {
            var rptMyChart1:Chart1Panel = new Chart1Panel();
            rptMyChart1.percentHeight = 100;
            rptMyChart1.percentWidth = 100;
            rptMyChart1.visible = true;
            printSelection.addChild(rptMyChart1);
        }

        print(printSelection);
    }


    private function print(container:Box):void
    {
        var job:FlexPrintJob;

        job = new FlexPrintJob();

        if (job.start()) {
            job.addObject(container, FlexPrintJobScaleType.MATCH_WIDTH);
            job.send();
        }

    }

This code works fine if the chart is actually displayed somewhere on the page but adding it dynamically as shown above does not. The print dialog will appear but nothing happens when I press OK.

So I really have two questions:

  1. Is it possible to print flex components/charts when they are not visible on the screen?
  2. If so, how do I do it / what am I doing wrong?

UPDATE:

Well, at least one thing wrong is my use of the percentages in the width and height. Using percentages doesn't really make sense when the Box is not contained in another object. Changing the height and width to fixed values actually allows the printing to progress and solves my initial problem.

    printSelection.height = 100;
    printSelection.width = 100;

But a new problem arises in that instead of seeing my chart, I see a black box instead. I have previously resolved this issue by setting the background colour of the chart to #FFFFFF but this doesn't seem to be working this time.

UPDATE 2:

I have seen some examples on the adobe site that add the container to the application but don't include it in the layout. This looks like the way to go.

i.e.

    printSelection.includeInLayout = false;
    addChild(printSelection);

© Stack Overflow or respective owner

Related posts about flex

Related posts about printing