views:

231

answers:

1

Hi,

I am a newbie in Adobe AIR, 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("ADDRESS
Thu Aug 20 21:37:20 GMT+0530 2009
")

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.

A: 

are you sure the page is being loaded ? the frame doesn't have to be visible BUT it needs the content. meaning that you htmlLoader data must be loaded and "shown" in a canvas. Then you can put the canvas invisible if you don't want the users to see what will be printed.

you'll have to set the clipping on false. This however means that it will take some seconds before the printer dialog will get on the screen.

   1. // Event Handler - called when the print button is clicked  
   2. private function onPrintClick ():Void  
   3. {  
   4.     // Remove the clipping so all of the content is printed  
   5.     clipForPrinting( true );  
   6.       
   7.     // Start the delay to allow clipping to update before  
   8.     // printing anything.  
   9.     doLater( this, "doActualPrinting" );  
  10. }  
  11.   
  12. // Adjust the clipping to prepare for or recover from print.  
  13. private function clipForPrinting( printing:Boolean ):Void  
  14. {  
  15.     // Assume printing is true if not passed in  
  16.     if ( printing == undefined ) {  
  17.         printing = true;      
  18.     }  
  19.   
  20.     // Modify the root clipContent so the application's width/height  
  21.     // doesn't interfere with bounds of the print content  
  22.     Application.application.clipContent = !printing;  
  23.           
  24.     // Modify the container holding the content to be printed to remove  
  25.     // the scroll masking   
  26.     printArea.clipContent = !printing;  
  27.           
  28. }  
  29.   
  30. // Handles the actual printing of the content in the popup  
  31. private function doActualPrinting():Void  
  32. {  
  33.     var printJob:PrintJob = new PrintJob();  
  34.     // Keep track of # of pages - only print if there are pages to print  
  35.     var pageCount:Number = 0;  
  36.   
  37.     // Show the print dialog  
  38.     if ( printJob.start() ) {  
  39.         // The user has opted to print - add the pages that  
  40.         // need to be printed  
  41.         pageCount += PrintUtil.pagenate( printArea, printJob );  
  42.   
  43.         // Send the content to the printer  
  44.         if ( pageCount > 0 ) {  
  45.             printJob.send();  
  46.         }  
  47.     }  
  48.   
  49.     // Explicitly delete the printJob  
  50.     delete printJob;  
  51.   
  52.     // Fix clipping now that print is done  
  53.     clipForPrinting( false );  
  54. }

credits to the original: Link to source

Jozzeh
If I do it onclick of a button on screen, I can print it, but I wanted to do this in background. Its either printing on a IP printer or this, based on some configuration. Still, I think I now have some material to work with. Thanks a lot!
Tanmay

related questions