Products
PDFKit.NET 5.0
Created
4/4/2019
Tags
Generate PDF

In PDFKit.NET 5.0 we decided to remove the Document.Write(HttpResponse) because it required the client to add a reference to System.Web even if this overload was not used. For example in a console or WPF application. The compiler needed a reference to System.Web to select the right overload at compile time.

If you are upgrading from 4.0 to 5.0, you may get a compile error because of this. The following extension method solves this:

using TallComponents.PDF;
using System.Web;

namespace YourCompany.PDF
{
  public static class DocumentExtensions
  {
    public static void Write(this Document document, HttpResponse response)
    {
      if (null == response) throw new ArgumentNullException(nameof(response));
      if (!response.IsClientConnected) throw new ArgumentException("Client is not connected.", nameof(response));
         
      response.Clear();
      response.ContentType = "application/pdf";
      response.ContentEncoding = new System.Text.UTF8Encoding();     

      document.Write(response.OutputStream);   

      // Flush requires full trust
      response.Flush(); // make sure all data is sent.
        
      // preventing further writing to the stream should be done by calling HttpApplication.CompleteRequest.
      // do not call response.End(), this will throw a ThreadAbortException.
      // do not call response.Close(), this will break Chrome support
    }
  }   
}

Usage

Document document;
HttpResponse response;

document.Write(response);