AIR的NativeProcess调用本机程序下令 打印

AIR的NativeProcess调用本机程序命令 打印
有个功能需求也是自己一直想实现的,就是文档在线分享。这种豆丁网、百度文库已经使用了。网上关于这种实现方式也有几种,最简单的是调用flashpaper打印,还有就是先通过例如openOffice.org之类的将文档转换为pdf,然后PDF再转换为swf。flashpaper早已被adobe抛弃,永远的停在了2.0。而且没有linux版,因为就是为了方便windows用户将office文档转换为pdf而出现的一个插件。openOffice我还没尝试,查看了一些资料,调用它的第三方包php的好像就只有puno,而puno官网看得我稀里糊涂,一个完整的例子都没有。完全不适合新手,但我暂时也没那么多时间折腾在那了。考虑到服务器性能跟尽快实现的难易程度,最终我不得不将设想做出了调整:不采用用户上传再后台转换,而采用上传之前本机先转换好再上传。

这样做就牺牲了体验,而且要转换就得在用户机子上装上flashpaper,我很不甘心。但是这个部分以后可以再完善。当然,这只是个毕业设计,这样暂时是已经足够了,慢慢来吧。

具体的实现方式如源码所示,调用了flashprinter.exe将参数传递给它,打印。这个例子只要改成将要转换的地址跟存放地址传入就可以用了。

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
   xmlns:s="library://ns.adobe.com/flex/spark"
   xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[

protected var npsi:NativeProcessStartupInfo;
protected var nativeProcess:NativeProcess;
protected var file:File = new File("c:\\flashpaper\\FlashPaper2.2\\FlashPrinter.exe");
protected var args:Vector.<String> = new Vector.<String>();


protected function doConvert(event:MouseEvent):void
{
args.push('c:/jiaoan.doc');
args.push('-o');
args.push('c:/b.swf');

npsi = new NativeProcessStartupInfo();
npsi.arguments = args;
npsi.executable = file;

nativeProcess = new NativeProcess();
//nativeProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA,onStandardOutputData);
//nativeProcess.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onError);
nativeProcess.start(npsi);

}

]]>
</fx:Script>

<s:Button id="btn1" x="50" y="0" label="WORD转换为SWF" click="doConvert(event)"/>

</s:WindowedApplication>


这里有可能会遇到些问题,如:
1. 报Error #3219: The NativeProcess could not be started. ‘Not supported in current profile.’ 解决办法是在-app.xml文件中加入这一行(直接加在后面就可以)
extendedDesktop
2. debug时先是打开office,然后flashpaper转换好后提示没有权限保存要你手动点保存。这个是写成了这样单反斜杠的缘故