Skip to content

Commit

Permalink
Rotativa now can generate images as output
Browse files Browse the repository at this point in the history
Supported image output using wkhtmltoimage
  • Loading branch information
martinobordin committed Mar 19, 2015
1 parent 694b7b2 commit 53bed7a
Show file tree
Hide file tree
Showing 31 changed files with 1,620 additions and 362 deletions.
8 changes: 8 additions & 0 deletions Rotativa.Demo/Controllers/AjaxTestsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public ActionResult Index()
};
}

public ActionResult IndexImage()
{
return new ViewAsImage("Index")
{
//CustomSwitches = "--enable-javascript --window-status jsdone"
};
}

public ActionResult Index2()
{
return View("Index");
Expand Down
4 changes: 4 additions & 0 deletions Rotativa.Demo/Controllers/CssTestsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ public ActionResult Index()
return new ViewAsPdf();
}

public ActionResult IndexImage()
{
return new ViewAsImage("Index");
}
}
}
59 changes: 59 additions & 0 deletions Rotativa.Demo/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public ActionResult Test()
return new ActionAsPdf("Index", new { name = "Giorgio" }) { FileName = "Test.pdf" };
}

public ActionResult TestImage()
{
return new ActionAsImage("Index", new { name = "Giorgio" }) { FileName = "Test.jpg" };
}

public ActionResult TestImagePng()
{
return new ActionAsImage("Index", new { name = "Giorgio" }) { FileName = "Test.png", Format = ImageFormat.png };
}

public ActionResult TestUrl()
{
// Now I realize that this isn't very expressive example of why this can be useful.
Expand Down Expand Up @@ -70,6 +80,21 @@ public ActionResult TestView()
};
}

public ActionResult TestViewImage()
{
// The more usual way of using this would be to have a Model object that you would pass into ViewAsImage
// and work with that Model inside your View.
// Good example could be an Order Summary page on some fictional E-shop.

// Probably the biggest advantage of this approach is that you have Session object available.

ViewBag.Message = string.Format("Hello {0} to ASP.NET MVC!", "Giorgio III.");
return new ViewAsImage("Index")
{
FileName = "TestView.png",
};
}

public ActionResult TestSaveOnServer(string fileName)
{
// The more usual way of using this would be to have a Model object that you would pass into ViewAsPdf
Expand All @@ -89,18 +114,46 @@ public ActionResult TestSaveOnServer(string fileName)
};
}

public ActionResult TestImageSaveOnServer(string fileName)
{
// The more usual way of using this would be to have a Model object that you would pass into ViewAsPdf
// and work with that Model inside your View.
// Good example could be an Order Summary page on some fictional E-shop.

// Probably the biggest advantage of this approach is that you have Session object available.
var filePath = Path.Combine(Server.MapPath("/App_Data"), fileName);
ViewBag.Message = string.Format("Hello {0} to ASP.NET MVC!", "Giorgio III.");
return new ViewAsImage("Index")
{
FileName = fileName,
SaveOnServerPath = filePath
};
}

public ActionResult TestViewWithModel(string id)
{
var model = new TestViewModel { DocTitle = id, DocContent = "This is a test" };
return new ViewAsPdf(model);
}

public ActionResult TestImageViewWithModel(string id)
{
var model = new TestViewModel { DocTitle = id, DocContent = "This is a test" };
return new ViewAsImage("TestViewWithModel", model);
}

public ActionResult TestPartialViewWithModel(string id)
{
var model = new TestViewModel { DocTitle = id, DocContent = "This is a test with a partial view" };
return new PartialViewAsPdf(model);
}

public ActionResult TestImagePartialViewWithModel(string id)
{
var model = new TestViewModel { DocTitle = id, DocContent = "This is a test with a partial view" };
return new PartialViewAsImage("TestPartialViewWithModel", model);
}

public ActionResult ErrorTest()
{
return new ActionAsPdf("SomethingBad") { FileName = "Test.pdf" };
Expand All @@ -118,6 +171,12 @@ public ActionResult AuthorizedTest()
return new ActionAsPdf("AuthorizedIndex") { FileName = "AuthorizedTest.pdf" };
}

[Authorize]
public ActionResult AuthorizedTestImage()
{
return new ActionAsImage("AuthorizedIndex") { FileName = "AuthorizedTest.jpg" };
}

public ActionResult RouteTest()
{
return new RouteAsPdf("TestRoute", new {name = "Giorgio"}) { FileName = "Test.pdf" };
Expand Down
1 change: 1 addition & 0 deletions Rotativa.Demo/Rotativa.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
<Content Include="Content\themes\base\jquery.ui.tabs.css" />
<Content Include="Content\themes\base\jquery.ui.theme.css" />
<Content Include="Global.asax" />
<Content Include="Rotativa\wkhtmltoimage.exe" />
<Content Include="Rotativa\wkhtmltopdf.exe" />
<Content Include="Scripts\jquery-1.5.1-vsdoc.js" />
<Content Include="Scripts\jquery-1.5.1.js" />
Expand Down
2 changes: 1 addition & 1 deletion Rotativa.Demo/Rotativa.Demo.csproj.user
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ProjectFiles</ProjectView>
<ProjectView>ShowAllFiles</ProjectView>
</PropertyGroup>
<ProjectExtensions>
<VisualStudio>
Expand Down
Binary file added Rotativa.Demo/Rotativa/wkhtmltoimage.exe
Binary file not shown.
8 changes: 8 additions & 0 deletions Rotativa.Demo/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@
<ul>
<li>@Html.ActionLink("Home", "Index", new {name = "Giorgio"})</li>
<li>@Html.ActionLink("Test", "Test", "Home")</li>
<li>@Html.ActionLink("Test Image", "TestImage", "Home")</li>
<li>@Html.ActionLink("Test Image Png", "TestImagePng", "Home")</li>
<li>@Html.ActionLink("Test URL", "TestUrl", "Home")</li>
<li>@Html.ActionLink("Test External URL", "TestExternalUrl", "Home")</li>
<li>@Html.ActionLink("Test View", "TestView", "Home")</li>
<li>@Html.ActionLink("Test View Image", "TestViewImage", "Home")</li>
<li>@Html.ActionLink("Test Save on Server", "TestSaveOnServer", new { fileName = "test.pdf"})</li>
<li>@Html.ActionLink("Logged In Test", "AuthorizedTest", "Home")</li>
<li>@Html.ActionLink("Logged In Test Image", "AuthorizedTestImage", "Home")</li>
<li>@Html.ActionLink("Route Test", "RouteTest", "Home")</li>
<li>@Html.ActionLink("Test ViewAsPdf with a model", "TestViewWithModel")</li>
<li>@Html.ActionLink("Test ViewAsImage with a model", "TestImageViewWithModel")</li>
<li>@Html.ActionLink("Test PartialViewAsPdf with a model", "TestPartialViewWithModel")</li>
<li>@Html.ActionLink("Test PartialViewAsImage with a model", "TestImagePartialViewWithModel")</li>
<li>@Html.ActionLink("Error Test", "ErrorTest", "Home")</li>
<li>@Html.ActionLink("Binary Test", "BinaryTest", "Home")</li>
<li>@Html.ActionLink("Ajax Test", "Index", "AjaxTests")</li>
<li>@Html.ActionLink("Ajax Image Test", "IndexImage", "AjaxTests")</li>
<li>@Html.ActionLink("External CSS Test", "Index", "CssTests")</li>
<li>@Html.ActionLink("External CSS Test Image", "IndexImage", "CssTests")</li>
</ul>

</p>
Expand Down
Loading

0 comments on commit 53bed7a

Please sign in to comment.