Load PDF from Memory ASP.Net
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开发者_JAVA技巧 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";
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;
}
}
精彩评论