views:

266

answers:

1

I am using ITextSharp to generate pdf on the fly and then saving it to disk and display it using Frame.

The Frame has an attribute called src where I pass the generated file name.

This all is working fine what I want to achieve is passing the generated pdf file to Frame without saving it to disk.

HtmlToPdfBuilder builder = new HtmlToPdfBuilder(PageSize.LETTER);
HtmlPdfPage first = builder.AddPage();

//import an entire sheet
builder.ImportStylesheet(Request.PhysicalApplicationPath + "CSS\\Stylesheet.css");
string coupon = CreateCoupon();
first.AppendHtml(coupon);

byte[] file = builder.RenderPdf();
File.WriteAllBytes(Request.PhysicalApplicationPath+"final.pdf", file);
printable.Attributes["src"] = "final.pdf";
+2  A: 

I've done exactly what you're trying to do. You'll want to create a handler (.ashx). After you've created your PDF load it in your handler using the code below:

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
public class MapHandler : IHttpHandler, IReadOnlySessionState 
{ 

    public void ProcessRequest(HttpContext context) { 
        CreateImage(context); 
    } 

    private void CreateImage(HttpContext context) { 

        string documentFullname = // Get full name of the PDF you want to display... 

        if (File.Exists(documentFullname)) { 

            byte[] buffer; 

            using (FileStream fileStream = new FileStream(documentFullname, FileMode.Open, FileAccess.Read, FileShare.Read)) 
            using (BinaryReader reader = new BinaryReader(fileStream)) { 
                buffer = reader.ReadBytes((int)reader.BaseStream.Length); 
            } 

            context.Response.ContentType = "application/pdf"; 
            context.Response.AddHeader("Content-Length", buffer.Length.ToString()); 
            context.Response.BinaryWrite(buffer); 
            context.Response.End(); 

        } else { 
            context.Response.Write("Unable to find the document you requested."); 
        } 
    } 

    public bool IsReusable { 
        get { 
            return false; 
        } 
    } 
Jay Riggs
+1, same here. Works great
Dustin Laine
I think you misunderstand--he wants to write out the generated pdf without ever writing it to disk. If you could incorporate his pdf generation code into your CreateImage function so that the pdf is created in memory and written to the response in one go, then this would be a good answer.
patmortech