Flex 2008-05-08 16:26:20 阅读386 评论0 字号:大中小 订阅
05-21改动:添加下载功能。
前些日子项目有这个需求,需要Web客户端能够打印PDF。研究了一下,AIR程序打印PDF还好办,在本地写入文件不会有安全问题,Web打印的话就要费点劲了,用到的包AlivePDF(开源)。
这是我的思路:先在本地将要打印的部分转换为PDF格式的字节数组,然后发送给服务器端的WebService(我的是用C#写的,将mxml代码中的webservice地址改成你的),然后写入到服务器上(记得给文件夹开写入权限!同时给IIS帐号permission指定写入权限,不报IO流错误的话就不会理会这些了),然后再下载给用户,花了几天时间,终于实现了。
首先在项目中添加AlivePDF包,直接将.swc文件拷贝到libs文件夹中,AlivePDF包请百度或Google之。
以下是代码:
PrintTest.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" preinitialize="init()">
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
import org.alivepdf.layout.Layout;
import org.alivepdf.display.Display;
import org.alivepdf.layout.Unit;
import org.alivepdf.layout.Orientation;
import org.alivepdf.pdf.PDF;
import org.alivepdf.saving.Method;
import org.alivepdf.layout.Size;
[Bindable] private var arr:Array=new Array(); //For DataGrid Data
[Bindable] private var bytes:ByteArray;
private function init():void
{
arr.push({first:"a",second:"1",third:"我"});
arr.push({first:"b",second:"2",third:"你"});
arr.push({first:"c",second:"3",third:"他"});
arr.push({first:"d",second:"4",third:"它"});
}
/**
* Print PDF
* Flex3+AlivePDF+C#
* Examples by Kelvin
* MSN:[email protected](内部用)
* MSN:[email protected]
*
* 说明:
* //前两行代码不做说明。
* 初始时要新增一页,调用方法:addPage();当每想要添加一页时调用此方法即可。
* 添加内容,调用方法addImage();此方法N多参数,不一一说明,只需要将要打印的对象传进去即可,例如addImage(img);
* 或者照此传入5个参数(img ,1,null,null,true),要打印的部分将分做为一整页内容输出,
* 注意:如果在此之前没有addPage();那么img将会覆盖掉前面添加的内容。
* */
private var myPDF:PDF;
public function generate ( pEvt:MouseEvent ):void
{
enabled=false;
myPDF = new PDF( Orientation.PORTRAIT, Unit.MM, Size.A4 );//
myPDF.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE ); //
myPDF.addPage();
myPDF.addImage (dg ,1,null,null,true);
bytes= myPDF.savePDF(Method.LOCAL);
//Call Webservice and Send Data
ws.Kelvin_PrintPDF(bytes);
}
[Bindable] private var down:FileReference;
private function result(event:ResultEvent):void
{
//Alert.show(event.result.toString());
enabled=true;
down=new FileReference();
down.addEventListener(Event.COMPLETE,downloadHandler);
down.download(new URLRequest("http://www.xxxx.com/printpdf/"+event.result.toString()));
}
private function downloadHandler(event:Event):void
{
Alert.show("success");
}
private function fault(event:FaultEvent):void
{
Alert.show(event.toString());
enabled=true;
}
]]>
</mx:Script>
<!--http://localhost/printpdf/service.asmx?WSDL-->
<mx:WebService id="ws" result="result(event)" fault="fault(event)"
wsdl=http://www.xxxx.com/printpdf/service.asmx?WSDL useProxy="false" showBusyCursor="true"/>
<mx:Image id="img" source="test.jpg"/>
<mx:DataGrid id="dg" x="106" y="136" width="566" height="383" dataProvider="{arr}">
<mx:columns>
<mx:DataGridColumn headerText="first" dataField="first"/>
<mx:DataGridColumn headerText="second" dataField="second"/>
<mx:DataGridColumn headerText="third" dataField="third"/>
</mx:columns>
</mx:DataGrid>
<mx:Button x="602" y="53" label="Print" click="generate(event)"/>
<mx:ProgressBar x="106" y="546" source="{down}"/>
</mx:Application>
(WebService)Service.cs:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
using System.Text;
using System.Collections;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
/*
* By kelvin
* 打印PDF
* MSN:[email protected]
* 此方法只是将接收过来的字节数据通过流写入本地
* */
[WebMethod]
public string Kelvin_PrintPDF(byte[] buffer)
{
string strInfo = "Success";
string path="";
string pathname="PDF/";
string date=""+DateTime.Now.Year + DateTime.Now.Month+DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond;
path = pathname + date + ".pdf";
FileStream filestream=new FileStream(Server.MapPath(path), FileMode.Create);
BinaryWriter writer=new BinaryWriter(filestream);
try
{
if (buffer != null)
writer.Write(buffer);
else
return "Error";
}
catch
{
strInfo = "Error";
}
finally
{
writer.Close();
filestream.Close();
l
评论