有些情况下,我们的应用程序可能不允许我们使用FelxPrintJob,下面的代码说明了在不使用FelxPrintJob的情况下,利用浏览器本身的打印功能,通过ExternalInterface调用直接进行打印。
首先需要准备下面这样的一段JavaScript脚本,放在index.template.html 的之前:
- <script language="JavaScript">
- function printPage(htmlPage)
- {
- var w = window.open("about:blank");
- w.document.write(htmlPage);
- w.print();
- w.close();
- }
- </script>
接下去是利用ExternalInterface进行调用:
- 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");}
- }
pHtmlPage是一串HTML标准的字符串,比如可以是这样:
- <html><h1>Helo World</h1></html>