Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

BuildPdf to get binary of pdf without writing response

  • Loading branch information...
commit 3a632e3c43ee32b7b331a65034253defa96d5279 1 parent b8da6f4
@webgio authored
View
9 Rotativa.Demo/Controllers/HomeController.cs
@@ -122,5 +122,14 @@ public ActionResult RouteTest()
{
return new RouteAsPdf("TestRoute", new {name = "Giorgio"}) { FileName = "Test.pdf" };
}
+
+ public ActionResult BinaryTest()
+ {
+ var pdfResult = new ActionAsPdf("Index", new { name = "Giorgio" }) { FileName = "Test.pdf" };
+
+ var binary = pdfResult.BuildPdf(this.ControllerContext);
+
+ return File(binary, "application/pdf");
+ }
}
}
View
45 Rotativa.Demo/Views/Home/Index.cshtml
@@ -1,23 +1,24 @@
-@{
- ViewBag.Title = "Home Page";
-}
-
-<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 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 Save on Server", "TestSaveOnServer", new { fileName = "test.pdf"})</li>
- <li>@Html.ActionLink("Logged In Test", "AuthorizedTest", "Home")</li>
- <li>@Html.ActionLink("Route Test", "RouteTest", "Home")</li>
- <li>@Html.ActionLink("Test ViewAsPdf with a model", "TestViewWithModel")</li>
- <li>@Html.ActionLink("Test PartialViewAsPdf with a model", "TestPartialViewWithModel")</li>
- <li>@Html.ActionLink("Error Test", "ErrorTest", "Home")</li>
- </ul>
-
-</p>
-
+@{
+ ViewBag.Title = "Home Page";
+}
+
+<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 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 Save on Server", "TestSaveOnServer", new { fileName = "test.pdf"})</li>
+ <li>@Html.ActionLink("Logged In Test", "AuthorizedTest", "Home")</li>
+ <li>@Html.ActionLink("Route Test", "RouteTest", "Home")</li>
+ <li>@Html.ActionLink("Test ViewAsPdf with a model", "TestViewWithModel")</li>
+ <li>@Html.ActionLink("Test PartialViewAsPdf with a model", "TestPartialViewWithModel")</li>
+ <li>@Html.ActionLink("Error Test", "ErrorTest", "Home")</li>
+ <li>@Html.ActionLink("Binary Test", "BinaryTest", "Home")</li>
+ </ul>
+
+</p>
+
<p>Some text with non ascii char àéù</p>
View
44 Rotativa.UnitTests/BinaryTests.cs
@@ -0,0 +1,44 @@
+using System.IO;
+using System.Web.Mvc;
+using MvcContrib.TestHelper;
+using NUnit.Framework;
+using Rotativa;
+using Rotativa.Demo.Controllers;
+using Rotativa.Tests;
+using SharpTestsEx;
+
+namespace Rotativa.UnitTests
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using MvcContrib.TestHelper.Fakes;
+
+ /// <summary>
+ /// Testing for binary generation
+ /// </summary>
+ [TestFixture]
+ public class BinaryTests
+ {
+ [Test]
+ public void Can_build_the_pdf_binary()
+ {
+ var localPath = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory);
+ var solutionDir = localPath.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);
+ var pdfTester = new PdfTester();
+ pdfTester.LoadPdf(pdfBinary);
+ pdfTester.PdfIsValid.Should().Be.True();
+ pdfTester.PdfContains("Rotativa").Should().Be.True();
+ }
+ }
+}
View
15 Rotativa.UnitTests/Rotativa.UnitTests.csproj
@@ -33,6 +33,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
+ <Reference Include="MvcContrib.TestHelper">
+ <HintPath>..\packages\MvcContrib.Mvc3.TestHelper-ci.3.0.100.0\lib\MvcContrib.TestHelper.dll</HintPath>
+ </Reference>
<Reference Include="MvcIntegrationTestFramework, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>MvcIntegrationTestFramework\MvcIntegrationTestFramework.dll</HintPath>
@@ -40,6 +43,9 @@
<Reference Include="nunit.framework, Version=2.6.0.12051, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.2.6.0.12054\lib\nunit.framework.dll</HintPath>
</Reference>
+ <Reference Include="Rhino.Mocks">
+ <HintPath>..\packages\RhinoMocks.3.6.1\lib\net\Rhino.Mocks.dll</HintPath>
+ </Reference>
<Reference Include="SharpTestsEx">
<HintPath>..\packages\SharpTestsEx.1.1.1\lib\SharpTestsEx.dll</HintPath>
</Reference>
@@ -57,11 +63,20 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="BinaryTests.cs" />
<Compile Include="SavePdfOnServerTests.cs" />
<Compile Include="WkhtmltopdfCommandLineStringTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
+ <ProjectReference Include="..\Rotativa.Demo\Rotativa.Demo.csproj">
+ <Project>{8EC08BFB-6ABA-4D71-8405-77AFF5859F3D}</Project>
+ <Name>Rotativa.Demo</Name>
+ </ProjectReference>
+ <ProjectReference Include="..\Rotativa.Tests\Rotativa.Tests.csproj">
+ <Project>{DE9EB72A-2AE7-42E8-9A44-AA525B463688}</Project>
+ <Name>Rotativa.Tests</Name>
+ </ProjectReference>
<ProjectReference Include="..\Rotativa\Rotativa.csproj">
<Project>{D93FAA11-31F0-4629-B53C-AA8680283529}</Project>
<Name>Rotativa</Name>
View
2  Rotativa.UnitTests/packages.config
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
+ <package id="MvcContrib.Mvc3.TestHelper-ci" version="3.0.100.0" targetFramework="net40" />
<package id="NUnit" version="2.6.0.12054" targetFramework="net40" />
+ <package id="RhinoMocks" version="3.6.1" targetFramework="net40" />
<package id="SharpTestsEx" version="1.1.1" targetFramework="net40" />
</packages>
View
22 Rotativa/AsPdfResultBase.cs
@@ -145,6 +145,7 @@ public string CookieName
[OptionFlag("")]
public string CustomSwitches { get; set; }
+ [Obsolete(@"Use BuildPdf(this.ControllerContext) method instead and use the resulting binary data to do what needed.")]
public string SaveOnServerPath { get; set; }
protected AsPdfResultBase()
@@ -204,7 +205,11 @@ private string GetWkParams(ControllerContext context)
{
var switches = string.Empty;
- HttpCookie authenticationCookie = context.HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
+ HttpCookie authenticationCookie = null;
+ if (context.HttpContext.Request.Cookies != null && context.HttpContext.Request.Cookies.AllKeys.Contains(FormsAuthentication.FormsCookieName))
+ {
+ authenticationCookie = context.HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
+ }
if (authenticationCookie != null)
{
var authCookieValue = authenticationCookie.Value;
@@ -226,15 +231,11 @@ protected virtual byte[] CallTheDriver(ControllerContext context)
return fileContent;
}
- public override void ExecuteResult(ControllerContext context)
+ public byte[] BuildPdf(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
- var response = PrepareResponse(context.HttpContext.Response);
-
- var switches = GetWkParams(context);
-
if (WkhtmltopdfPath == string.Empty)
WkhtmltopdfPath = HttpContext.Current.Server.MapPath("~/Rotativa");
@@ -245,6 +246,15 @@ public override void ExecuteResult(ControllerContext context)
File.WriteAllBytes(SaveOnServerPath, fileContent);
}
+ return fileContent;
+ }
+
+ public override void ExecuteResult(ControllerContext context)
+ {
+ var fileContent = BuildPdf(context);
+
+ var response = PrepareResponse(context.HttpContext.Response);
+
response.OutputStream.Write(fileContent, 0, fileContent.Length);
}
View
4 Rotativa/Rotativa.nuspec
@@ -2,7 +2,7 @@
<package >
<metadata>
<id>Rotativa</id>
- <version>1.5.0</version>
+ <version>1.6.0</version>
<title>Rotativa</title>
<authors>Giorgio Bozio</authors>
<owners>Giorgio Bozio</owners>
@@ -11,7 +11,7 @@
<!--<iconUrl></iconUrl>-->
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Extremely easy way to print Pdf in Aspnet Mvc</description>
- <releaseNotes>Added PartialViewAsPdf action result</releaseNotes>
+ <releaseNotes>Added BuildPdf that returns binary of the pdf</releaseNotes>
<copyright>Copyright 2012</copyright>
<tags>AspNetMvc Pdf</tags>
</metadata>
Please sign in to comment.
Something went wrong with that request. Please try again.