在 AIR 生成PDF和显示

1. 下载 AlivePDF
   http://code.google.com/p/alivepdf/downloads/list

2. 解压 “AlivePDF 0.1.4.9.zip”, 把 "AlivePDF.swc" 复制到 project 的 /libs 目录内

在 AIR 生成PDF和显示_第1张图片

3. 生成 PDF 并在浏览器显示

  <mx:Script>
    <![CDATA[
      import flash.filesystem.File;
      
      import org.alivepdf.pdf.PDF;  
      import org.alivepdf.saving.Method;   
      import org.alivepdf.fonts.*;   
      import org.alivepdf.pages.Page;   
      import org.alivepdf.display.Display;   
      import org.alivepdf.layout.*; 

      private var mypdf:PDF;  
      private var myfile:File; 
      
      import flash.net.navigateToURL;
      
      import mx.utils.UIDUtil;

      
      //create a pdf file
      private function unitsPrintToPDF():void  
      { 
        var mypdf:PDF = new PDF( Orientation.PORTRAIT, Unit.MM, Size.A4 ); 
        mypdf.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE );
        var newPage:Page = new Page ( Orientation.PORTRAIT, Unit.MM, Size.A4 );

        mypdf.addPage(newPage);
        mypdf.setFont(FontFamily.ARIAL , Style.NORMAL, 12);
        mypdf.addText("Hello World: 15,20",15,20);
        
        mypdf.addPage();
        mypdf.addText("This is the 2nd page 第二页 - 5,15",5,15);
        mypdf.drawCircle(25,35,15);
        
        //画线
        mypdf.moveTo ( 75, 35 );
        mypdf.lineTo ( 125, 35 );
        mypdf.moveTo ( 25, 55 );
        mypdf.lineTo ( 185, 55 );        
        mypdf.end();
        
        //以 uuid 为文件名,避免名字重复
        var myuuid:String = UIDUtil.createUID();
        var uupdf:String = myuuid + ".pdf";

        var fs:FileStream = new FileStream();  
        myfile = File.desktopDirectory.resolvePath( uupdf );
        fs.open( myfile, FileMode.WRITE);   
        var bytes:ByteArray = mypdf.save(Method.LOCAL);   
        fs.writeBytes(bytes);   
        fs.close(); 
		
	//在新开的浏览器窗口显示 pdf 
	navigateToURL(new URLRequest(File.desktopDirectory.nativePath +"/"+ uupdf));
      }   
      
      ]]>
  </mx:Script>



当然,你的电脑需要能正常显示PDF文件,即已安装 Adobe Reader 或 FOXIT READER.

你可能感兴趣的:(浏览器,Google,Flash,Adobe,AIR)