42 lines
1.6 KiB
C#
42 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Web.Mvc;
|
|
|
|
namespace EnVisage.Code
|
|
{
|
|
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
|
|
public class ExportableToPdfAttribute : ActionFilterAttribute
|
|
{
|
|
private readonly string fileNameFormat;
|
|
|
|
public ExportableToPdfAttribute(string fileNameFormat)
|
|
{
|
|
this.fileNameFormat = fileNameFormat;
|
|
}
|
|
|
|
public override void OnActionExecuting(ActionExecutingContext filterContext)
|
|
{
|
|
if (filterContext.RequestContext.HttpContext.Request.QueryString.AllKeys.Contains("ExportToPdf"))
|
|
{
|
|
var urlHelper = new UrlHelper(filterContext.RequestContext);
|
|
var actionName = filterContext.ActionDescriptor.ActionName;
|
|
var controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
|
|
var scheme = filterContext.HttpContext.Request.Url.Scheme;
|
|
var url = urlHelper.Action(actionName, controllerName, null, scheme);
|
|
var exporter = new PDFExporter();
|
|
var pdfBuffer = exporter.ExportPage(url, 3, true, filterContext.HttpContext.Request.Cookies);
|
|
|
|
var fileResult = new FileContentResult(pdfBuffer, "application/pdf");
|
|
var fileName = string.Format(fileNameFormat, DateTime.Today);
|
|
fileResult.FileDownloadName = fileName;
|
|
filterContext.Result = fileResult;
|
|
}
|
|
else
|
|
{
|
|
base.OnActionExecuting(filterContext);
|
|
}
|
|
}
|
|
}
|
|
} |