0

i'm pulling my hair out. need to print html generated invoices from a distant server, using

the print class, event if bitmap is set as false, will render the invoice a bitmap. at least the text is blurry and not usable. alivepdf could be a solution but i need to print straight, not save the pdf locally. i don't even understand, giving the fact this print class sucks badly, flex won't allow a simple javascript print function from the remote page.

i beg for some help here !

thank you

  • 1
    Help us help you: post code, libraries, etc. Are you using PrintJob? – Dominic Tancredi Dec 10 '11 at 14:39
  • yes i do: import mx.printing.*; private function doPrint():void { var printJob:FlexPrintJob = new FlexPrintJob(); printJob.printAsBitmap=false; if (printJob.start() != true) return; printJob.addObject(myHTML, FlexPrintJobScaleType.SHOW_ALL); printJob.send(); } – shad0wproxy Dec 13 '11 at 04:07

1 Answers1

1

Why don't you use the browser to print?

Here's an example:

Put this in your index.html template:

<script language="JavaScript">
    function printPage(htmlPage)
    {
        var w = window.open("about:blank");
        w.document.write(htmlPage);
        w.print();
        w.close();
    }
</script>

Put this in your Flex Project. What you're doing is checking to see if you have access to ExternalInterface to have access to the browser. Then you're going to use the ExternalInterface static method of "call" to call the javascript:

    import mx.controls.Alert;
    import flash.external.ExternalInterface;   

    public static function PrintHtmlPage(pHtmlPage:String):void
    {
        if (ExternalInterface.available)
        {
            try  
            {
                  ExternalInterface.call("printPage",pHtmlPage);
            }
            catch (error:SecurityError) { Alert.show("Security Error"); }
            catch (error:Error) { Alert.show("Error");}
        }
        else { Alert.show("ExternalInterface not avalible");}
    }

Now the user can print clean html from their browser!

http://cookbooks.adobe.com/post_How_to_print_in_Flex_using_browser_capabilities-11468.html

EDIT:

If you are using AIR and need to do this, you can try using AlivePDF and following this tutorial:

  1. use alivepdf (http://alivepdf.bytearray.org/), and look at this tutorial for printing from AIR (http://murrayhopkins.wordpress.com/2011/01/07/using-alivepdf-to-print-from-air-javascript-via-actionscript3-part-1/)
  2. parse the HTML into Spark components, and then add them as a Sprite, then use printAsBitmap = true in your option, and FlexPrintJob
Dominic Tancredi
  • 41,134
  • 7
  • 34
  • 50
  • thank you dominic for your answer but i don't use an html template. i call a distant php page that will produce invoices on the fly. but maybe i could use an html template and call the pho page in iframe ? – shad0wproxy Dec 13 '11 at 13:34
  • And i forgot to specify: i'm making a desktop (air) app. the cookbook displays the code for a web app. – shad0wproxy Dec 13 '11 at 15:21
  • I've modified my answer above with two solutions. – Dominic Tancredi Dec 13 '11 at 16:01
  • this is copied from adobe docs. anyway it is a simple example for a very simple needs. Nothis which could be useful in nowdays requiremenets, especially for a dinamic sites. – Yordan Yanakiev Jan 18 '12 at 07:50
  • It's sourced back to adobe's website as a reference properly. The solution responds to the case of printing from a Flex / Flash / Actionscript environment to the browser. If you want to create a "future-proof" solution, design your own language and browsing experience and send me an invite. – Dominic Tancredi Jan 18 '12 at 08:16