diff --git a/Rotativa.Demo/Controllers/HomeController.cs b/Rotativa.Demo/Controllers/HomeController.cs index 6732af5..2971a81 100644 --- a/Rotativa.Demo/Controllers/HomeController.cs +++ b/Rotativa.Demo/Controllers/HomeController.cs @@ -1,5 +1,7 @@ using System; using System.IO; +using System.Threading.Tasks; +using System.Web.Hosting; using System.Web.Mvc; using Rotativa.Demo.Models; using Rotativa.Options; @@ -80,6 +82,29 @@ public ActionResult TestView() }; } + public ActionResult TestQueueablePdf() + { + var queueableViewAsPdf = new QueueableViewAsPdf(ControllerContext, "Index") + { + FileName = "TestView.pdf", + PageSize = Size.A3, + PageOrientation = Orientation.Landscape, + PageMargins = {Left = 0, Right = 0} + }; + + HostingEnvironment.QueueBackgroundWorkItem(e => + { + // Put this step in a background work item as it takes most of the time, + // especially when there are multiple files need to be generated. + + var file = queueableViewAsPdf.BuildFile(); + // Email the file as an attachment. + }); + + ViewBag.Message = "File will be sent to your email shortly"; + return View(); + } + public ActionResult TestViewImage() { // The more usual way of using this would be to have a Model object that you would pass into ViewAsImage diff --git a/Rotativa.Demo/Models/AccountModels.cs b/Rotativa.Demo/Models/AccountModels.cs index b970d60..40c09d3 100644 --- a/Rotativa.Demo/Models/AccountModels.cs +++ b/Rotativa.Demo/Models/AccountModels.cs @@ -23,7 +23,7 @@ public class ChangePasswordModel [DataType(DataType.Password)] [Display(Name = "Confirm new password")] - [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")] + [System.ComponentModel.DataAnnotations.Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")] public string ConfirmPassword { get; set; } } @@ -61,7 +61,7 @@ public class RegisterModel [DataType(DataType.Password)] [Display(Name = "Confirm password")] - [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] + [System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] public string ConfirmPassword { get; set; } } } diff --git a/Rotativa.Demo/Rotativa.Demo.csproj b/Rotativa.Demo/Rotativa.Demo.csproj index eca67c2..52df142 100644 --- a/Rotativa.Demo/Rotativa.Demo.csproj +++ b/Rotativa.Demo/Rotativa.Demo.csproj @@ -1,5 +1,5 @@  - + Debug @@ -13,7 +13,7 @@ Properties Rotativa.Demo Rotativa.Demo - v4.0 + v4.5.2 false true ..\ @@ -28,6 +28,7 @@ true + true @@ -37,6 +38,7 @@ DEBUG;TRACE prompt 4 + false pdbonly @@ -45,23 +47,26 @@ TRACE prompt 4 + false + - - + + + - + @@ -183,6 +188,9 @@ + + + 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) diff --git a/Rotativa.Demo/Views/Home/Index.cshtml b/Rotativa.Demo/Views/Home/Index.cshtml index 8000b1f..93825bf 100644 --- a/Rotativa.Demo/Views/Home/Index.cshtml +++ b/Rotativa.Demo/Views/Home/Index.cshtml @@ -12,6 +12,7 @@
  • @Html.ActionLink("Test URL", "TestUrl", "Home")
  • @Html.ActionLink("Test External URL", "TestExternalUrl", "Home")
  • @Html.ActionLink("Test View", "TestView", "Home")
  • +
  • @Html.ActionLink("Test generating pdf in QueueBackgroundWorkItem", "TestQueueablePdf", "Home")
  • @Html.ActionLink("Test View Image", "TestViewImage", "Home")
  • @Html.ActionLink("Test Save on Server", "TestSaveOnServer", new { fileName = "test.pdf"})
  • @Html.ActionLink("Logged In Test", "AuthorizedTest", "Home")
  • diff --git a/Rotativa.Demo/Views/Home/TestQueueablePdf.cshtml b/Rotativa.Demo/Views/Home/TestQueueablePdf.cshtml new file mode 100644 index 0000000..1626458 --- /dev/null +++ b/Rotativa.Demo/Views/Home/TestQueueablePdf.cshtml @@ -0,0 +1,5 @@ +@{ + ViewBag.Title = "TestQueueablePdf Page"; +} + +

    @ViewBag.Message

    diff --git a/Rotativa.Demo/Web.config b/Rotativa.Demo/Web.config index f353760..ea4de54 100644 --- a/Rotativa.Demo/Web.config +++ b/Rotativa.Demo/Web.config @@ -3,90 +3,83 @@ For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=152368 --> - - + - + - + - - - - - + + + + + - - + - - + - - + - - - + + - - + - - - - - + + + + + - - - + + - - - + + - + \ No newline at end of file diff --git a/Rotativa.UnitTests/Rotativa.UnitTests.csproj b/Rotativa.UnitTests/Rotativa.UnitTests.csproj index 1a689f5..ad20c56 100644 --- a/Rotativa.UnitTests/Rotativa.UnitTests.csproj +++ b/Rotativa.UnitTests/Rotativa.UnitTests.csproj @@ -1,5 +1,5 @@  - + Debug AnyCPU @@ -10,10 +10,11 @@ Properties Rotativa.UnitTests Rotativa.UnitTests - v4.0 + v4.5.2 512 ..\ true + true @@ -23,6 +24,7 @@ DEBUG;TRACE prompt 4 + false pdbonly @@ -31,6 +33,7 @@ TRACE prompt 4 + false diff --git a/Rotativa/QueueableViewAsPdf.cs b/Rotativa/QueueableViewAsPdf.cs new file mode 100644 index 0000000..f75cce7 --- /dev/null +++ b/Rotativa/QueueableViewAsPdf.cs @@ -0,0 +1,82 @@ +using System.Web; +using System.Web.Mvc; +using Rotativa.Extensions; + +namespace Rotativa +{ + public class QueueableViewAsPdf : AsPdfResultBase + { + private readonly ControllerContext _context; + private readonly object _model; + private string _viewName; + private readonly string _masterName; + + public string ViewHtmlString { get; private set; } + + public QueueableViewAsPdf(ControllerContext context) + { + _context = context; + + ViewHtmlString = GetHtmlFromView(); + } + + public QueueableViewAsPdf(ControllerContext context, string viewName) + { + _context = context; + _viewName = viewName; + + ViewHtmlString = GetHtmlFromView(); + } + + public QueueableViewAsPdf(ControllerContext context, object model) + { + _context = context; + _model = model; + + ViewHtmlString = GetHtmlFromView(); + } + + public QueueableViewAsPdf(ControllerContext context, string viewName, object model) + { + _context = context; + _viewName = viewName; + _model = model; + + ViewHtmlString = GetHtmlFromView(); + } + + public QueueableViewAsPdf(ControllerContext context, string viewName, string masterName, object model) + { + _context = context; + _viewName = viewName; + _masterName = masterName; + _model = model; + + ViewHtmlString = GetHtmlFromView(); + } + + private string GetHtmlFromView() + { + this.WkhtmlPath = HttpContext.Current.Server.MapPath("~/Rotativa"); + + if (string.IsNullOrEmpty(_viewName)) + _viewName = _context.RouteData.GetRequiredString("action"); + + var viewResult = ViewEngines.Engines.FindView(_context, _viewName, _masterName); + var html = _context.GetHtmlFromView(viewResult, _viewName, _model); + + return html; + } + + public byte[] BuildFile() + { + var fileContent = WkhtmltopdfDriver.ConvertHtml(this.WkhtmlPath, this.GetConvertOptions(), ViewHtmlString); + return fileContent; + } + + protected override string GetUrl(ControllerContext context) + { + return string.Empty; + } + } +} \ No newline at end of file diff --git a/Rotativa/Rotativa.csproj b/Rotativa/Rotativa.csproj index 8bb3874..2c924a6 100644 --- a/Rotativa/Rotativa.csproj +++ b/Rotativa/Rotativa.csproj @@ -55,6 +55,7 @@ +