2010-03-18 4 views
3

나는 ITextSharp를 사용하여 pdf를 즉시 생성 한 다음 디스크에 저장하고 프레임을 사용하여 표시합니다.메모리에서 PDF로드 ASP.Net

프레임에는 생성 된 파일 이름을 전달하는 src라는 속성이 있습니다.

이 모든 작업은 디스크에 저장하지 않고 생성 된 PDF 파일을 프레임에 전달하는 것입니다.

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

나는 당신이하려는 것을 정확히했습니다. 당신은 핸들러 (.ashx)를 만들고 싶을 것이다. PDF를 작성한 후에는 아래 코드를 사용하여 핸들러에로드하십시오.

[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; 
     } 
    } 
+0

+1 여기에서 코드를로드하십시오. 작품의 위대한 –

+0

난 당신이 오해라고 생각 - 그는 디스크에 쓰지 않고 생성 된 PDF를 작성하고 싶어. pdf 생성 코드를 CreateImage 함수에 통합하여 pdf가 메모리에 작성되고 한 번에 응답에 쓰여질 수 있다면 좋은 대답이 될 것입니다. – patmortech