网页转化为PDF,并用流输出.

客户有个需求,就是需要保存当前网页为PDF.

1.直接使用非常成熟的类.ABCpdf.

下载下来以后,从安装包里面,找到几个dll文件即可.:ABCpdf.dll,ABCpdfCE7.dll,

2.新建文件.引入命名空间

   using WebSupergoo.ABCpdf7;
   using WebSupergoo.ABCpdf7.Objects;
   using WebSupergoo.ABCpdf7.Atoms;

3.直接上代码,该代码会直接把文档以流的方式输出.如需生成文件.直接修改theDoc.Save()中参数即可.

   

            Doc theDoc = new Doc();  //创建一个Doc对象
XSettings.License = "key";

theDoc.Font = theDoc.AddFont("宋体", "ChineseS");
theDoc.Rect.Inset(24, 48);
//Rect默认是文档整个页面大小, 这里的Inset表示将Rect左右留出24的空白,上下留出48的空白

        theDoc.MediaBox.String = "0 0 810 480";  //设置添加新页面时,页面的大小,即文档的大小.
        theDoc.Rect.String = "10 0 800 485";     //当前输出区间,即显示的大小.

           int theID = theDoc.AddImageUrl(url, true, 1000, false);

       
       //PDF分页
while (true)
{
if (!theDoc.Chainable(theID))
break;
theDoc.Page = theDoc.AddPage();
theID = theDoc.AddImageToChain(theID);
}
string sFile = user_Id.ToString();
Response.Clear();
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.AddHeader("Content-Disposition", "attachment; filename=" + sFile + ".pdf");
Response.ContentType = "application/octet-stream";
//直接输出流
theDoc.Save(Response.OutputStream);
Response.Flush();
theDoc.Clear();


4.需要注意的是,出现 HTML render is blank. 的错误...经我发现.这是由于AddImageUrl(url)..这个方法中..可能url的这个页面.对session的判定.使插件不能访问这个页面,需要删除那个页面的session判断即可.

 

posted @ 2012-02-28 17:20  eastday  阅读(3864)  评论(0编辑  收藏  举报