Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.8.1] - 2022-06-06

### Added

- Update whtml drivers 0.12.1 -> 0.12.4 https://github.com/wkhtmltopdf/wkhtmltopdf/releases/tag/0.12.4 (based on https://github.com/webgio/Rotativa/pull/115)
- Fix try to kill process when cancellation requested

## [1.8.0] - 2022-06-03

### Added

- Add timeout support https://github.com/webgio/Rotativa/issues/203

### Fixed

- fix when WkhtmlDriver exits with error code https://github.com/webgio/Rotativa/issues/189
- fix using HttpContext.Current instead passed ControllerContext https://github.com/webgio/Rotativa/pull/178

15 changes: 15 additions & 0 deletions Rotativa.Demo/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ public ActionResult ErrorTest()
return new ActionAsPdf("SomethingBad") { FileName = "Test.pdf" };
}

public ActionResult HttpStatus500Test()
{
return new UrlAsPdf("https://httpstat.us/500") { FileName = "Test.pdf" };

}
public ActionResult HttpStatus404Test()
{
return new UrlAsPdf("https://httpstat.us/404") { FileName = "Test.pdf" };
}
public ActionResult HttpStatus200Test()
{
return new UrlAsPdf("https://httpstat.us/200") { FileName = "Test.pdf" };

}

public ActionResult SomethingBad()
{
return Redirect("http://thisdoesntexists");
Expand Down
51 changes: 27 additions & 24 deletions Rotativa.Demo/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@

<h2>@ViewBag.Message</h2>
<p>
<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>
<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>
<li>@Html.ActionLink("HttpStatus 500 Test", "HttpStatus500Test", "Home")</li>
<li>@Html.ActionLink("HttpStatus 404 Test", "HttpStatus404Test", "Home")</li>
<li>@Html.ActionLink("HttpStatus 200 Test", "HttpStatus200Test", "Home")</li>
</ul>

</p>

Expand Down
35 changes: 35 additions & 0 deletions Rotativa.Tests/RotativaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,5 +360,40 @@ public void Can_print_image_from_page_with_external_css_file()
image.RawFormat.Should().Be.EqualTo(ImageFormat.Jpeg);
}
}

[Test]
public void HttpError_statuses_does_not_generate_pdf_file()
{
var testLink = selenium.FindElement(By.LinkText("HttpStatus 200 Test"));
var pdfHref = testLink.GetAttribute("href");

var content = "200 OK";
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();
}

testLink = selenium.FindElement(By.LinkText("HttpStatus 404 Test"));
pdfHref = testLink.GetAttribute("href");


using (var wc = new WebClient())
{
Assert.Throws(typeof(WebException), () => wc.DownloadData(new Uri(pdfHref)));
}

testLink = selenium.FindElement(By.LinkText("HttpStatus 500 Test"));
pdfHref = testLink.GetAttribute("href");


using (var wc = new WebClient())
{
Assert.Throws(typeof(WebException), () => wc.DownloadData(new Uri(pdfHref)));
}
}
}
}
100 changes: 79 additions & 21 deletions Rotativa.UnitTests/BinaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,102 @@ namespace Rotativa.UnitTests
[TestFixture]
public class BinaryTests
{
private const string TestUrl = "https://github.com/webgio/Rotativa";

[Test]
public void Can_build_the_pdf_binary()
{
var localPath = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory);
var solutionDir = localPath.Parent.Parent.Parent.FullName;
var wkhtmltopdfPath = Path.Combine(solutionDir, "Rotativa", "Rotativa");
var actionResult = new UrlAsPdf("https://github.com/webgio/Rotativa")
{
WkhtmltopdfPath = wkhtmltopdfPath
};
var builder = new TestControllerBuilder();
var controller = new HomeController();
builder.InitializeController(controller);
var pdfBinary = actionResult.BuildPdf(controller.ControllerContext);
//Arrange
var actionResult = CreatePdfActionResult();
var controller = CreateTestController();

//Act
var pdfBinary = actionResult.BuildFile(controller.ControllerContext);

//Assert
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfBinary);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains("Rotativa").Should().Be.True();
}

[Test]
public void Can_build_the_image_binary()
public void Failed_to_build_the_pdf_binary_when_timeout_exceed()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some code refactor & create tests for checking timeout's

{
var localPath = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory);
var solutionDir = localPath.Parent.Parent.Parent.FullName;
var wkhtmltoimagePath = Path.Combine(solutionDir, "Rotativa", "Rotativa");
var actionResult = new UrlAsImage("https://github.com/webgio/Rotativa")
//Arrange
var actionResult = CreatePdfActionResult(convertTimeout: 1);
var controller = CreateTestController();
void TestMethod()
{
WkhtmlPath = wkhtmltoimagePath
};
var builder = new TestControllerBuilder();
var controller = new HomeController();
builder.InitializeController(controller);
actionResult.BuildFile(controller.ControllerContext);
}
//Act

//Assert
Assert.Throws<TimeoutException>(TestMethod);

}

[Test]
public void Can_build_the_image_binary()
{
//Arrange
var actionResult = CreateImageActionResult();
var controller = CreateTestController();

//Act
var imageBinary = actionResult.BuildFile(controller.ControllerContext);

//Assert
var image = Image.FromStream(new MemoryStream(imageBinary));
image.Should().Not.Be.Null();
image.RawFormat.Should().Be.EqualTo(ImageFormat.Jpeg);
}

[Test]
public void Failed_to_build_the_image_binary_when_timeout_exceed()
{
//Arrange
var actionResult = CreateImageActionResult(convertTimeout: 1);
var controller = CreateTestController();
void TestMethod()
{
actionResult.BuildFile(controller.ControllerContext);
}
//Act

//Assert
Assert.Throws<TimeoutException>(TestMethod);
}

private static AsImageResultBase CreateImageActionResult(string url = TestUrl, int? convertTimeout = null)
=> new UrlAsImage(url)
{
WkhtmlPath = GetWkhtmlPath(),
ConvertTimeout = convertTimeout,
};

private static AsPdfResultBase CreatePdfActionResult(string url = TestUrl, int? convertTimeout = null)
=> new UrlAsPdf(url)
{
WkhtmlPath = GetWkhtmlPath(),
ConvertTimeout = convertTimeout,
};

private static string GetWkhtmlPath()
{
var localPath = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory);
var solutionDir = localPath.Parent.Parent.Parent.FullName;
var wkhtmlPath = Path.Combine(solutionDir, "Rotativa", "Rotativa");
return wkhtmlPath;
}

private static Controller CreateTestController()
{
var builder = new TestControllerBuilder();
var controller = new HomeController();
builder.InitializeController(controller);
return controller;
}
}
}
2 changes: 1 addition & 1 deletion Rotativa/AsImageResultBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public abstract class AsImageResultBase : AsResultBase

protected override byte[] WkhtmlConvert(string switches)
{
return WkhtmltoimageDriver.Convert(this.WkhtmlPath, switches);
return WkhtmltoimageDriver.Convert(this.WkhtmlPath, switches, timeout: ConvertTimeout);
}

protected override string GetContentType()
Expand Down
2 changes: 1 addition & 1 deletion Rotativa/AsPdfResultBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected AsPdfResultBase()

protected override byte[] WkhtmlConvert(string switches)
{
return WkhtmltopdfDriver.Convert(this.WkhtmlPath, switches);
return WkhtmltopdfDriver.Convert(this.WkhtmlPath, switches, timeout: ConvertTimeout);
}

protected override string GetContentType()
Expand Down
7 changes: 6 additions & 1 deletion Rotativa/AsResultBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public string CookieName

public ContentDisposition ContentDisposition { get; set; }

/// <summary>
/// Timeout for converting to PDF
/// </summary>
public int? ConvertTimeout { get; set; }

protected abstract string GetUrl(ControllerContext context);

/// <summary>
Expand Down Expand Up @@ -185,7 +190,7 @@ public byte[] BuildFile(ControllerContext context)
throw new ArgumentNullException("context");

if (this.WkhtmlPath == string.Empty)
this.WkhtmlPath = HttpContext.Current.Server.MapPath("~/Rotativa");
this.WkhtmlPath = context.HttpContext.Server.MapPath("~/Rotativa");

var fileContent = this.CallTheDriver(context);

Expand Down
2 changes: 1 addition & 1 deletion Rotativa/Extensions/ControllerContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static string GetHtmlFromView(this ControllerContext context, ViewEngineR
viewResult.View.Render(viewContext, sw);

string html = sw.GetStringBuilder().ToString();
string baseUrl = string.Format("{0}://{1}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority);
string baseUrl = string.Format("{0}://{1}", context.HttpContext.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority);
html = Regex.Replace(html, "<head>", string.Format("<head><base href=\"{0}\" />", baseUrl), RegexOptions.IgnoreCase);
return html;
}
Expand Down
2 changes: 1 addition & 1 deletion Rotativa/Rotativa.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Rotativa</id>
<version>1.7.3</version>
<version>1.8.1</version>
<title>Rotativa</title>
<authors>Giorgio Bozio</authors>
<owners>Giorgio Bozio</owners>
Expand Down
Loading