如何使用Web处理程序创建PDF?
问题描述:
我对网络处理程序的了解很少.我所知道的是,Web处理程序用于创建一些动态文件创建目的.
I have pretty less knowledge in web handlers. All I know is web handlers are used for creating some dynamic file creation purpose.
我也知道如何添加Web处理程序.
And also I know how to add the Web handlers.
但是,我需要在ASP.NET项目中使用Web处理程序来创建PDF.
But, I need to use web handlers in my ASP.NET project for PDF creation purpose.
答
您可以使用HTTP处理程序来提供PDF,如下所示:
You can have a HTTP handler to serve out your PDFs like this:
public void ProcessRequest (HttpContext context) {
// Get your file as byte[]
string filename = "....file name.";
byte[] data = get your PDF file content here;
context.Response.Clear();
context.Response.AddHeader("Pragma", "public");
context.Response.AddHeader("Expires", "0");
context.Response.AddHeader("Content-Type", ContentType);
context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", filename));
context.Response.AddHeader("Content-Transfer-Encoding", "binary");
context.Response.AddHeader("Content-Length", data.Length.ToString());
context.Response.BinaryWrite(data);
context.Response.End();
}