0

How do i generate a temporal PDF in my ASP.NET application so I get an temp URL for a generated PDF from an array of bytes ?

I use this but i think it could get simple:

//bytes[] arrayPDF exists before
string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".pdf";
File.WriteAllBytes(fileName, arrayPDF);

HtmlGenericControl obj = new HtmlGenericControl("embed");
obj.Attributes.Add("src", fileName);
obj.Attributes.Add("style","border-radius: 10px;position: relative;top:0;right:0;left:0;bottom:0;width:100%;height:620px;");
obj.Attributes.Add("height","600");
obj.Attributes.Add("type", "application/pdf");
form1.Controls.Add(obj);

My main problem is how to generate a temp file that i am sure that it would not stay in the server more than the request/show time

2
  • Generate it on the fly when requested from a dedicated URL. Don't generate it at the same time you generate the embedding page. Commented Feb 5, 2015 at 12:06
  • I have the bytes[] of the PDF and i want to show it in an object tag, appart from other divs, what you will recommend? Commented Feb 5, 2015 at 12:07

1 Answer 1

1

Use an ASP.NET ASHX Handler:

public class Handler : IHttpHandler 
{
    public void ProcessRequest (HttpContext context) 
    {
        // relevant context.Response lines
    }

    public bool IsReusable 
    {
        get { return false; }
    }
}

Also check this answer:

context.Response.Clear(); 
context.Response.ClearContent(); 
context.Response.ClearHeaders(); 
context.Response.ContentType = "application/pdf";

Stream fileStream = publishBookManager.GetFile(documentId);
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
  int read;
  while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
  {
    ms.Write(buffer, 0, read);
  }
}

context.Response.BinaryWrite(data); 
context.Response.Flush();   
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.