Skip to content

Commit

Permalink
Added PartialViewAsPdf
Browse files Browse the repository at this point in the history
  • Loading branch information
webgio committed Dec 7, 2012
1 parent bb867bf commit 00db69b
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 191 deletions.
4 changes: 2 additions & 2 deletions Rotativa.Demo/Controllers/HomeController.cs
Expand Up @@ -91,13 +91,13 @@ public ActionResult TestSaveOnServer(string fileName)

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

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

Expand Down
307 changes: 162 additions & 145 deletions Rotativa.Tests/RotativaTests.cs
@@ -1,145 +1,162 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using SharpTestsEx;

namespace Rotativa.Tests
{
[TestFixture]
public class RotativaTests
{
private IWebDriver selenium;
private StringBuilder verificationErrors;

[TestFixtureSetUp]
public void SetupTest()
{
selenium = new FirefoxDriver();
selenium.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 10));
verificationErrors = new StringBuilder();
}

[SetUp]
public void TestSetUp()
{
var rotativaDemoUrl = ConfigurationManager.AppSettings["RotativaDemoUrl"];
selenium.Navigate().GoToUrl(rotativaDemoUrl);
}

[TestFixtureTearDown]
public void FixtureTearDown()
{
if (selenium != null) selenium.Close();
}

[Test]
public void Is_the_site_reachable()
{
Assert.AreEqual("Home Page", selenium.Title);
}

[Test]
public void Can_print_the_test_pdf()
{

var testLink = selenium.FindElement(By.LinkText("Test"));
var pdfHref = testLink.GetAttribute("href");
using (var wc = new WebClient())
{
var pdfResult = wc.DownloadData(new Uri(pdfHref));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains("My MVC Application").Should().Be.True();
}
}

[Test]
public void Can_print_the_authorized_pdf()
{

var testLink = selenium.FindElement(By.LinkText("Logged In Test"));
var pdfHref = testLink.GetAttribute("href");
testLink.Click();
var username = selenium.FindElement(By.Id("UserName"));
username.SendKeys("admin");
var password = selenium.FindElement(By.Id("Password"));
password.SendKeys("admin");
password.Submit();
var manage = selenium.Manage();
var cookies = manage.Cookies.AllCookies;
using (var wc = new WebClient())
{
foreach (var cookie in cookies)
{
var cookieText = cookie.Name + "=" + cookie.Value;
wc.Headers.Add(HttpRequestHeader.Cookie, cookieText);
}
var pdfResult = wc.DownloadData(new Uri(pdfHref));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains("My MVC Application").Should().Be.True();
pdfTester.PdfContains("admin").Should().Be.True();
}
}

[Test]
public void Can_print_the_pdf_from_a_view()
{

var testLink = selenium.FindElement(By.LinkText("Test View"));
var pdfHref = testLink.GetAttribute("href");
using (var wc = new WebClient())
{
var pdfResult = wc.DownloadData(new Uri(pdfHref));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains("My MVC Application").Should().Be.True();
}
}

[Test]
public void Can_print_the_pdf_from_a_vie_with_non_ascii_chars()
{

var testLink = selenium.FindElement(By.LinkText("Test View"));
var pdfHref = testLink.GetAttribute("href");
using (var wc = new WebClient())
{
var pdfResult = wc.DownloadData(new Uri(pdfHref));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains("àéù").Should().Be.True();
}
}

[Test]
public void Can_print_the_pdf_from_a_view_with_a_model()
{

var testLink = selenium.FindElement(By.LinkText("Test ViewAsPdf with a model"));
var pdfHref = testLink.GetAttribute("href");
var title = "This is my test!";
using (var wc = new WebClient())
{
var pdfResult = wc.DownloadData(new Uri(pdfHref + "/" + title));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains(title).Should().Be.True();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using SharpTestsEx;

namespace Rotativa.Tests
{
[TestFixture]
public class RotativaTests
{
private IWebDriver selenium;
private StringBuilder verificationErrors;

[TestFixtureSetUp]
public void SetupTest()
{
selenium = new FirefoxDriver();
selenium.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 10));
verificationErrors = new StringBuilder();
}

[SetUp]
public void TestSetUp()
{
var rotativaDemoUrl = ConfigurationManager.AppSettings["RotativaDemoUrl"];
selenium.Navigate().GoToUrl(rotativaDemoUrl);
}

[TestFixtureTearDown]
public void FixtureTearDown()
{
if (selenium != null) selenium.Close();
}

[Test]
public void Is_the_site_reachable()
{
Assert.AreEqual("Home Page", selenium.Title);
}

[Test]
public void Can_print_the_test_pdf()
{

var testLink = selenium.FindElement(By.LinkText("Test"));
var pdfHref = testLink.GetAttribute("href");
using (var wc = new WebClient())
{
var pdfResult = wc.DownloadData(new Uri(pdfHref));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains("My MVC Application").Should().Be.True();
}
}

[Test]
public void Can_print_the_authorized_pdf()
{

var testLink = selenium.FindElement(By.LinkText("Logged In Test"));
var pdfHref = testLink.GetAttribute("href");
testLink.Click();
var username = selenium.FindElement(By.Id("UserName"));
username.SendKeys("admin");
var password = selenium.FindElement(By.Id("Password"));
password.SendKeys("admin");
password.Submit();
var manage = selenium.Manage();
var cookies = manage.Cookies.AllCookies;
using (var wc = new WebClient())
{
foreach (var cookie in cookies)
{
var cookieText = cookie.Name + "=" + cookie.Value;
wc.Headers.Add(HttpRequestHeader.Cookie, cookieText);
}
var pdfResult = wc.DownloadData(new Uri(pdfHref));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains("My MVC Application").Should().Be.True();
pdfTester.PdfContains("admin").Should().Be.True();
}
}

[Test]
public void Can_print_the_pdf_from_a_view()
{

var testLink = selenium.FindElement(By.LinkText("Test View"));
var pdfHref = testLink.GetAttribute("href");
using (var wc = new WebClient())
{
var pdfResult = wc.DownloadData(new Uri(pdfHref));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains("My MVC Application").Should().Be.True();
}
}

[Test]
public void Can_print_the_pdf_from_a_vie_with_non_ascii_chars()
{

var testLink = selenium.FindElement(By.LinkText("Test View"));
var pdfHref = testLink.GetAttribute("href");
using (var wc = new WebClient())
{
var pdfResult = wc.DownloadData(new Uri(pdfHref));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains("àéù").Should().Be.True();
}
}

[Test]
public void Can_print_the_pdf_from_a_view_with_a_model()
{

var testLink = selenium.FindElement(By.LinkText("Test ViewAsPdf with a model"));
var pdfHref = testLink.GetAttribute("href");
var title = "This is a test";
using (var wc = new WebClient())
{
var pdfResult = wc.DownloadData(new Uri(pdfHref));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains(title).Should().Be.True();
}
}

[Test]
public void Can_print_the_pdf_from_a_partial_view_with_a_model()
{

var testLink = selenium.FindElement(By.LinkText("Test PartialViewAsPdf with a model"));
var pdfHref = testLink.GetAttribute("href");
var content = "This is a test with a partial view";
using (var wc = new WebClient())
{
var pdfResult = wc.DownloadData(new Uri(pdfHref));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains(content).Should().Be.True();
}
}
}
}
46 changes: 3 additions & 43 deletions Rotativa/PartialViewAsPdf.cs
Expand Up @@ -26,50 +26,10 @@ public PartialViewAsPdf(string partialViewName, object model)
: base(partialViewName, model)
{
}

protected override byte[] CallTheDriver(ControllerContext context)
protected override ViewEngineResult GetView(ControllerContext context, string viewName, string masterName)
{
context.Controller.ViewData.Model = Model;

// use action name if the view name was not provided
if (string.IsNullOrEmpty(ViewName))
ViewName = context.RouteData.GetRequiredString("action");

using (var sw = new StringWriter())
{
ViewEngineResult partialViewResult = ViewEngines.Engines.FindPartialView(context, ViewName);

// view not found, throw an exception with searched locations
if (partialViewResult.View == null)
{
var locations = new StringBuilder();
locations.AppendLine();

foreach (string location in partialViewResult.SearchedLocations)
{
locations.AppendLine(location);
}

throw new InvalidOperationException(
string.Format("The partial view '{0}' was not found, searched locations: {1}", ViewName,
locations));
}

var viewContext = new ViewContext(context, partialViewResult.View, context.Controller.ViewData,
context.Controller.TempData, sw);
partialViewResult.View.Render(viewContext, sw);

StringBuilder html = sw.GetStringBuilder();

// replace href and src attributes with full URLs
string baseUrl = string.Format("{0}://{1}", HttpContext.Current.Request.Url.Scheme,
HttpContext.Current.Request.Url.Authority);
html.Replace(" href=\"/", string.Format(" href=\"{0}/", baseUrl));
html.Replace(" src=\"/", string.Format(" src=\"{0}/", baseUrl));

var fileContent = WkhtmltopdfDriver.ConvertHtml(WkhtmltopdfPath, GetConvertOptions(), html.ToString());
return fileContent;
}
return ViewEngines.Engines.FindPartialView(context, ViewName);
}
}
}
7 changes: 6 additions & 1 deletion Rotativa/ViewAsPdf.cs
Expand Up @@ -63,6 +63,11 @@ protected override string GetUrl(ControllerContext context)
{
return string.Empty;
}

protected virtual ViewEngineResult GetView(ControllerContext context, string viewName, string masterName)
{
return ViewEngines.Engines.FindView(context, ViewName, MasterName);
}

protected override byte[] CallTheDriver(ControllerContext context)
{
Expand All @@ -74,7 +79,7 @@ protected override byte[] CallTheDriver(ControllerContext context)

using (var sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindView(context, ViewName, MasterName);
ViewEngineResult viewResult = GetView(context, ViewName, MasterName);

// view not found, throw an exception with searched locations
if (viewResult.View == null)
Expand Down

0 comments on commit 00db69b

Please sign in to comment.