2012-12-19 141 views
0

我有一個.NET網站和一個頁面,導航到(使用EssentialObjects PDF庫)時將返回生成的PDF。從Server.Execute調用創建ZIP文件

我想有另一個頁面,可以在一個循環中對Server.Execute進行多次調用,並執行URL到PDF頁面,並使用返回的響應在內存中創建PDF,並使用zip進行壓縮DotNetZip並將包含多個PDF的最終zip文件返回給用戶。

我該怎麼做呢?

回答

1

對於任何想要這樣做的人我最終使用此代碼。這可以很容易地放在一個循環中以獲取大量頁面,將它們轉換爲PDF,並將它們全部壓縮到一個zip文件中。

Dictionary<string, byte[]> pdfByteList = new Dictionary<string, byte[]>(); 
string pageName = "TestPage"; 
string htmlToConvert = GetHTML(pageName); //Perform the Server.Execute() code here and return the HTML 

PdfConverter converter = new PdfConverter(); 
byte[] pdfBytes = converter.GetPdfBytesFromHtmlString(htmlToConvert); 
pdfByteList.Add(pageName, pdfBytes); 

using (ZipFile zip = new ZipFile()) 
{ 
    int i = 0; 
    foreach(KeyValuePair<string,byte[]> kvp in pdfByteList) 
    { 
     zip.AddEntry(kvp.Key, kvp.Value); 
     i++; 
    } 

    Response.Clear(); 
    Response.BufferOutput = true; // false = stream immediately 
    Response.ContentType = "application/zip"; 
    Response.AddHeader("content-disposition", "attachment; filename=TheZipFile.zip"); 
    zip.Save(Response.OutputStream); 
}