2012-12-19 72 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); 
}