3 Replies Latest reply on Aug 26, 2009 9:53 AM by Joe ... Ward

    Cannot print off-screen HTML content from AIR application

    Tanmay Dalvi

      Hi,

       

      I am a newbie, and am trying to print a HTML from my air app, however, this HTML should never be seen on screen. I am using HTMLLoader for this, as per some sample I saw on the web.

       

      What happens, is that there is a print dialog, but it prints out a blank page.

       

      If this is a window application, and I click some button to print (just the HTMLLoader) it gets printed.

       

      Following is my code.

       

      var mySprite:Sprite = new mySprite()

      var loader:HTMLLoader = new HTMLLoader()
      loader.loadString("<html><center>ADDRESS</center><br/><center>Thu Aug 20 21:37:20 GMT+0530 2009</center><br/></html>")

      var html:HTML = new HTML()

      html.htmlLoader = loader

      mySprite.addChild(html);

       

       

      //After this its pretty standard

       

      var pJob:PrintJob = new PrintJob();
      html.width = pJob.pageWidth
      html.height = pJob.pageHeight
      loader.height = pJob.pageHeight
      loader.width = pJob.pageWidth
         
      if(!pJob.start())
      {
           throw new PrintingCanceled(" User Canceled Printing");
      }
      pJob.addPage(loader, null);
      pJob.send();

       

       

      Please let me know what I'm missing. Any help, or suggestions are welcome.

        • 1. Re: Cannot print off-screen HTML content from AIR application
          __antonio__ Level 3

          first, what environment are you using to build this in? (I build in Flex and Flash so this may be a little biased)

          I don't recognize PrintCancelled as a valid method. And I dont think Flash API has HTML as a method either.

           

          when you test to do you get compiler errors? you probably should get at least 4.

           

          Just breezing through your code, "var mySprite" cannot equal "new mySprite" you'll get : Error 1180: Call to a possibly undefined method mySprite.

          • 2. Re: Cannot print off-screen HTML content from AIR application
            Tanmay Dalvi Level 1

            No its not a compile issue. Sorry for the mistakes, I was just trying to simplify the code by removing unnecessary details from my production code.

             

            You're right, it should read "new Sprite()". PrintCancelled is just an exception we defined, you can skip that.

             

            html is of type mx:HTML (its a component for AIR).

             

            My environment is Flex Builder 3.1.0, building for Adobe AIR 1.x.Flex framework 3.1.0.

             

            The problem, in short is this. If I replace the whole HTML, HTMLLoader etc with any other component, say a text box, I can easily get the print. With HTMLLoader, I cannot.I get a blank page.

             

            I tried so far

            a. Printing after Event.Complete from HtmlLoader.

            b. Printing after HTML_RENDER

            c. Putting this in a simple AIR application, creating HTMLLoader in creationComplete handler and then printing on click of a button.

             

            Only (c) works. Unfortunately, in our production system, we wanted to do this in the background depending on configuration (that is go to either an IP printer, or use PrintJob to print based on configuration).

             

            Hope this helps.

            • 3. Re: Cannot print off-screen HTML content from AIR application
              Joe ... Ward Level 4

              Perhaps the size of the HTML control is never properly set because it isn't in another container. Flex components expect to get layout information from their parent. You generally can't use Flex components outside of the Flex framework (Sprite is a basic Flash object and isn't part of the Flex framework). You don't really need the HTML component for this anyway, just use the plain HTMLLoader object (the variable named loader in your code).

               

              After you set the size of the loader, you should wait for an event like render since the change isn't instantaneous.

               

              However, I don't think you will be pleased with the results of printing HTML. For something simple like your example, you would be better off using the TextField (which can display basic HTML).  For more complex layout, you could use the new Text Layout Framework (http://labs.adobe.com/technologies/textlayout/).

              1 person found this helpful