網頁轉化為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判斷即可.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM