【问题标题】:How can I make evoPDF to work as ActionFilterAttribute in ASP.NET MVC如何使 evoPDF 在 ASP.NET MVC 中用作 ActionFilterAttribute
【发布时间】:2015-06-11 21:35:44
【问题描述】:
我只需要在 ASP.NET MVC 中从网站呈现 PDF。我找到了quite interesting article about generating PDF from ASP.NET MVC。如果页面通过 ActionFilterAttribute 呈现,我认为它可以做得更好。我的想法是这样的:
[EnableCompression]
[OutputCache(Duration=7200)]
[EvoPDFFilter]
public ActionResult DownloadAsPDF()
{
var model= GetModel();
return View(model); //return just HTML and convert it by filter to PDF
}
public class EvoPDFFilterAttribute: ActionFilterAttribute
{
//some code should be here. Is this solution even possible?
}
有可能吗? EvoPDFFilterAttribute 应该如何?
【问题讨论】:
标签:
asp.net-mvc
actionfilterattribute
evopdf
【解决方案1】:
我终于成功了:-)
using EvoPdf;
using System.IO;
using System.Web;
using System.Web.Mvc;
namespace MyProject.Helpers
{
/// <summary>
/// PDF filter based on code https://lostechies.com/jimmybogard/2014/02/04/rendering-asp-net-content-as-pdf/
/// </summary>
public class EvoPDFFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpRequestBase request = filterContext.HttpContext.Request;
HttpResponseBase response = filterContext.HttpContext.Response;
if (response.Filter == null)//RenderAction
return;
string acceptEncoding = request.Headers["Accept-Encoding"];
if (acceptEncoding == null)
return;
response.ContentType = "application/pdf";
var baseUrl = string.Format("{0}://{1}{2}/", request.Url.Scheme, request.Url.Authority, request.ApplicationPath.TrimEnd('/'));
response.Filter = new PdfFilter(response.Filter, baseUrl);
}
public class PdfFilter : Stream
{
private readonly Stream _oldFilter;
private readonly string _baseUrl;
private readonly MemoryStream _memStream;
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return true; }
}
public override bool CanRead
{
get { return false; }
}
public override long Position
{
get { return 0L; }
set { }
}
public override long Length
{
get { return 0L; }
}
public PdfFilter(Stream oldFilter, string baseUrl)
{
_oldFilter = oldFilter;
_baseUrl = baseUrl;
_memStream = new MemoryStream();
}
public override int Read(byte[] buffer, int offset, int count)
{
return 0;
}
public override long Seek(long offset, SeekOrigin origin)
{
return 0L;
}
public override void SetLength(long value)
{
}
public override void Write(byte[] buffer, int offset, int count)
{
_memStream.Write(buffer, offset, count);
}
public override void Flush()
{
}
public override void Close()
{
var converter = new PdfConverter
{
MediaType = "print",
};
converter.PdfDocumentOptions.LiveUrlsEnabled = false;
_memStream.Position = 0;
converter.SavePdfFromHtmlStreamToStream(_memStream, System.Text.Encoding.UTF8, _baseUrl, _oldFilter);
_oldFilter.Close();
}
}
}
}