0

I have a .NET website and a page that will return a generated PDF when navigated to (using EssentialObjects PDF library).

I would like to have another page that would be able to make multiple calls to Server.Execute in a loop and execute the URL to the PDF page and, using the returned response, create the PDF's in memory and zip them using DotNetZip and return this final zip file containing the multiple PDF's to the user.

How would I go about doing this?

sbonkosky
  • 2,537
  • 1
  • 22
  • 31

1 Answers1

1

For anybody looking to do this I ended up using this code. This could then easily be put in a loop to get a whole bunch of pages, convert them to PDF's, and zip them all in a single zip file.

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);
}
sbonkosky
  • 2,537
  • 1
  • 22
  • 31