11 Replies Latest reply on May 4, 2009 9:20 PM by Michael Borbor

    printing with referenced images

    deuce4

      I have a print component in my Flex application which works fine if I embed the header image to be printed.  But if I merely link it as an external file, it appears to load, but somehow the print code creates the page too rapidly and the header does not get included in the print output (though it appears in the component micorseconds after the print dialog closes). 

       

      I tried a workaround to give the image a chance to upload into the print component completely (see below), but it is inconsistent and only works for faster connections.

       

      callPrint is only called when creationComplete of the component, but it does not appear to be the case--the image is not completely created when the print function is called by  creationComplete="callPrint()"

       

      Has anyone else experienced a problem with this, and is there a workaround?

       

      thanks

       

      WORKAROUND ATTEMPT CODE BELOW:

       

      public var printTimer:Timer=new Timer(500); //might have to tweak this value for slower connections?

       

      public function callPrint():void{

             printTimer.addEventListener(TimerEvent.TIMER,okayNowPrint);

          printTimer.start();

      }

            

      public function okayNowPrint(evt:TimerEvent):void{

      var printJob:FlexPrintJob = new FlexPrintJob();

      if (printJob.start()){

      printJob.addObject(this,FlexPrintJobScaleType.SHOW_ALL);

      printJob.send();

      }

      parentApplication.removeChild(this);

      evt.currentTarget.stop();

      printTimer.reset();

      }

              

        • 1. Re: printing with referenced images
          Michael Borbor Level 4

          How do you load the image? If you use a SWFLoader or a Loader you could add an event listener to the Loader and then print only when the image is fully loaded you could use a global boolean var to determine the status of the load image, or anything in that direction will help you with the desire result.

          • 2. Re: printing with referenced images
            deuce4 Level 1

            I tried using the Loader with an event listener, and the problem was the same.  It has something to do with the print aspect of the code--somehow the component is sent to the printer before everything is actually complete, though it seems like the timing is only microseconds off.

             

            The global boolean var sounds interesting.  As there are other aspects of the component that need to be complete (  creationComplete="callPrint()" ), how could several conditionals be coded to allow the print to continue?  Any chance for some sample code?

             

            cheers

            • 3. Re: printing with referenced images
              Michael Borbor Level 4

              It a simple idea, lets say you have 3 assets that need to be loaded in order to the printing continue, so you create 3 boolean vars , the you create an event listener for each asset that needs to be load, and within each event listener that is triggered you change the value of the related boolean var to true, and also call the callPrint(), and also add some logic to callPrint() in order to continue only when the 3 boolean vars in this case are true.

              • 4. Re: printing with referenced images
                deuce4 Level 1

                I'll give it a go with your suggestions, do you recommend loader or swfloader?  The image is a jpg. Previously I tried both URLloader and loader, maybe I did something wrong, because it still required the timer workaround above.

                • 5. Re: printing with referenced images
                  Michael Borbor Level 4

                  SWFLoader maybe, they're kinda the same.

                  • 6. Re: printing with referenced images
                    deuce4 Level 1

                    oh, also, one of the issues with the loaders is that I am trying to include the image in the mxml, rather than an actionscript addChild.

                     

                    SO here is the code in mxml:

                     

                    <mx:Image source="@Embed(source='logos/printHeader.jpg')"  width="540" height="84"/>

                     

                    Course, I'm trying to get rid of the Embed.

                     

                     

                    I tried creating a [Bindable] image variable to work on the loader call, is this the correct tact?  Any rough outlines of how you would work it?

                     

                    in the mxml:

                    <mx:Image source="{header.source}"  width="540" height="84"/>

                    in the actionscript:

                    [Bindable]public var header:Image;

                     

                    thanks for your responses!

                    • 7. Re: printing with referenced images
                      Michael Borbor Level 4

                      You could do this

                       

                      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
                                          creationComplete="init()">
                           <mx:Script>
                                <![CDATA[
                      
                      
                                     private var ldr:Loader = new Loader();
                                     private var urlReq:URLRequest = new URLRequest("picture.jpg");
                      
                                     private function init():void{
                                          ldr.load(urlReq);
                                          ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
                      
                                     }
                      
                                     private function loaded(event:Event):void
                                     {
                                          trace("I was loaded");
                                          myImage.source=ldr;
                                     }
                                ]]>
                           </mx:Script>
                      
                           <mx:Image id="myImage" />
                      </mx:Application>
                      
                      • 8. Re: printing with referenced images
                        deuce4 Level 1

                        STILL doesn't work--this is baffling me.  Is it possibly because my print component is a VBox???

                         

                        Here is my code:

                         

                        private var ldr:Loader = new Loader();

                        private var urlReq:URLRequest = new URLRequest('logos/printHeader.jpg');

                         

                         

                        public function init():void{  //called by creationComplete

                        ldr.load(urlReq);

                        ldr.contentLoaderInfo.addEventListener( Event.COMPLETE, loaded);

                        }

                         

                        private function loaded(event:Event):void{

                        trace("printHeader loaded");

                        printHeader.source=ldr;

                        callPrint();

                        }

                         

                        public function callPrint():void{

                        var printJob:FlexPrintJob = new FlexPrintJob();

                        if (printJob.start()){

                        printJob.addObject(this,FlexPrintJobScaleType.SHOW_ALL);

                        printJob.send();

                        }

                        parentApplication.removeChild(this);

                                

                        }

                         

                        It's something to do with the fact that callPrint gets called early, for some reason.  Only by microseconds, but the image is always blank, even though if I make the component visible it appears almost simultaneously with the print dialog (but NOT immediately).

                         

                        ??

                        • 9. Re: printing with referenced images
                          Michael Borbor Level 4

                          Yep must be the fact that you call callPrint() early, try with this little modified version of your code.

                           

                          private var myImgLoaded:Boolean=false;               
                                         private var ldr:Loader = new Loader();
                                         private var urlReq:URLRequest = new URLRequest('logos/printHeader.jpg');
                          
                                         public function init():void{  //called by creationComplete
                          
                                              ldr.load(urlReq);
                          
                                              ldr.contentLoaderInfo.addEventListener( Event.COMPLETE, loaded);
                          
                                         }
                          
                                         private function loaded(event:Event):void{
                                              trace("printHeader loaded");
                                              printHeader.source=ldr;
                                              myImgLoaded=true;
                                              callPrint();
                                         }
                          
                                         public function callPrint():void{
                          
                                              var printJob:FlexPrintJob = new FlexPrintJob();
                          
                                              if (myImgLoaded){
                          
                                                   if (printJob.start()){
                          
                                                        printJob.addObject(this,FlexPrintJobScaleType.SHOW_ALL);
                          
                                                        printJob.send();
                          
                                                   }
                          
                                              }
                          
                                              parentApplication.removeChild(this);
                          
                                         }
                          
                          • 10. Re: printing with referenced images
                            deuce4 Level 1

                            Fantastic--got it working now, thanks to your help!

                             

                            Much Appreciated!

                            • 11. Re: printing with referenced images
                              Michael Borbor Level 4

                              Great! You're welcome. I told you a boolean var could do the trick