Delete a dynamically created PDF file immediately after it is displayed to the user

I create a PDF file on the fly using ITextSharp and ASP.NET 1.1. My process is as follows:

  • Create file on server
  • Redirect the browser to a new PDF file so that it is displayed to the user

What I would like to do is remove the PDF from the server as soon as it is displayed in the users browser. The PDF file is large, so it cannot be stored in memory; initial recording to the server is required. I am currently using a solution that periodically checks files and then deletes them, but I would prefer a solution that deletes the file immediately after it is downloaded to the client machine. Is there any way to do this?

+6
c # pdf-generation
source share
4 answers

Instead of redirecting the browser to the created file, you can use the file yourself using your own HttpHandler. You can then delete the file immediately after serving it, or even create a file in memory.

Write the PDF file directly to the client:

public class MyHandler : IHttpHandler { public void ProcessRequest(System.Web.HttpContext context) { context.Response.ContentType = "application/pdf"; // ... PdfWriter.getInstance(document, context.Response.OutputStream); // ... 

or read the already generated file 'filename', open the file, delete it:

 context.Response.Buffer = false; context.Response.BufferOutput = false; context.Response.ContentType = "application/pdf"; Stream outstream = context.Response.OutputStream; FileStream instream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = instream.Read(buffer, 0, BUFFER_SIZE)) > 0) { outstream.Write(buffer, 0, len); } outstream.Flush(); instream.Close(); // served the file -> now delete it File.Delete(filename); 

I have not tried this code. This is exactly how I think it will work ...

+6
source share

Inspired by f3lix's answer (thanks f3lix!) I came up with the following VB.net code -

 HttpContext.Current.Response.ClearContent() HttpContext.Current.Response.ClearHeaders() HttpContext.Current.Response.ContentType = "application/pdf" HttpContext.Current.Response.TransmitFile(PDFFileName) HttpContext.Current.Response.Flush() HttpContext.Current.Response.Close() File.Delete(PDFFileName) 

This seems to work - is it the WriteFile method that I used less efficiently than the stream methods used by f3lix? Is there an affordable method more efficient than any of our solutions?

EDIT (03/19/2009) Based on the comments below, I changed the "WriteFile" method to "TransmitFile" because it seems to send the file to the client in chunks, rather than writing the entire file to the web server before sending. Further information can be found here .

+5
source share

Or you can simply return it to the browser without writing to disk at all:

 byte[] pdf; using (MemoryStream ms = new MemoryStream()) { Document doc = new Document(); PdfWriter.GetInstance(doc, ms); doc.AddTitle("Document Title"); doc.Open(); doc.Add(new Paragraph("My paragraph.")); doc.Close(); pdf = ms.GetBuffer(); } Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachment;filename=MyDocument.pdf"); Response.OutputStream.Write(pdf, 0, pdf.Length); 
+3
source share

Decision:

 Response.TransmitFile(PDFFileName) Response.Flush() Response.Close() File.Delete(PDFFileName) 

It just doesn't work for me (the file never makes it to the client). Reading in a byte array and calling Response.BinaryWrite is not an option, as the file can be large. The only hack for this is to start an asynchronous process, waiting for the file to exit, and then delete it?

0
source share

All Articles