Skip to content

Commit

Permalink
no more link rewrite, introduced base tag
Browse files Browse the repository at this point in the history
  • Loading branch information
webgio committed Feb 25, 2015
1 parent 5891a52 commit 694b7b2
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Rotativa.Demo/Content/csstest.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#cssRevealedContent {
display: block;
}

20 changes: 20 additions & 0 deletions Rotativa.Demo/Controllers/CssTestsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Rotativa.Demo.Controllers
{
public class CssTestsController : Controller
{
//
// GET: /CssTests/

public ActionResult Index()
{
return new ViewAsPdf();
}

}
}
5 changes: 5 additions & 0 deletions Rotativa.Demo/Rotativa.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<ItemGroup>
<Compile Include="Controllers\AccountController.cs" />
<Compile Include="Controllers\AjaxTestsController.cs" />
<Compile Include="Controllers\CssTestsController.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\TestController.cs" />
<Compile Include="Global.asax.cs">
Expand All @@ -75,6 +76,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Content\csstest.css" />
<Content Include="Content\themes\base\images\ui-bg_flat_0_aaaaaa_40x100.png" />
<Content Include="Content\themes\base\images\ui-bg_flat_75_ffffff_40x100.png" />
<Content Include="Content\themes\base\images\ui-bg_glass_55_fbf9ee_1x400.png" />
Expand Down Expand Up @@ -176,6 +178,9 @@
<ItemGroup>
<Content Include="Views\AjaxTests\Index.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\CssTests\Index.cshtml" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
Expand Down
1 change: 0 additions & 1 deletion Rotativa.Demo/Views/AjaxTests/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
}
@section head
{
<base href="http://localhost:12237/">
}

<h2>Index</h2>
Expand Down
19 changes: 19 additions & 0 deletions Rotativa.Demo/Views/CssTests/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@{
ViewBag.Title = "Index";
}

@section head
{
<style type="text/css">
#cssRevealedContent {
display: none;
}
</style>
<link href="/Content/csstest.css" rel="stylesheet" />
}

<h2>Index</h2>
<div id="cssRevealedContent">
<p>Hi guys, this content shows up thanks to css file.</p>

</div>
1 change: 1 addition & 0 deletions Rotativa.Demo/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<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("External CSS Test", "Index", "CssTests")</li>
</ul>

</p>
Expand Down
4 changes: 2 additions & 2 deletions Rotativa.Demo/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<HEAD>
<meta charset="utf-8" />
@RenderSection("head", required: false)
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
</head>
</HEAD>
<body>
<div class="page">
<header>
Expand Down
16 changes: 16 additions & 0 deletions Rotativa.Tests/RotativaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,21 @@ public void Can_print_pdf_from_page_with_content_from_ajax_request()
pdfTester.PdfContains(content).Should().Be.True();
}
}

[Test]
public void Can_print_pdf_from_page_with_external_css_file()
{
var testLink = selenium.FindElement(By.LinkText("External CSS Test"));
var pdfHref = testLink.GetAttribute("href");
var content = "Hi guys, this content shows up thanks to css file.";
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();
}
}
}
}
11 changes: 6 additions & 5 deletions Rotativa/ViewAsPdf.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

Expand Down Expand Up @@ -98,14 +99,14 @@ protected override byte[] CallTheDriver(ControllerContext context)
var viewContext = new ViewContext(context, viewResult.View, context.Controller.ViewData, context.Controller.TempData, sw);
viewResult.View.Render(viewContext, sw);

StringBuilder html = sw.GetStringBuilder();
string html = sw.GetStringBuilder().ToString();

// replace href and src attributes with full URLs
// add base tag to the html doc, to allow wkhtmltopdf to resolve links etc.
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));
html = Regex.Replace(html, "<head>", string.Format("<head><base href=\"{0}\" />", baseUrl),
RegexOptions.IgnoreCase);

var fileContent = WkhtmltopdfDriver.ConvertHtml(WkhtmltopdfPath, GetConvertOptions(), html.ToString());
var fileContent = WkhtmltopdfDriver.ConvertHtml(WkhtmltopdfPath, GetConvertOptions(), html);
return fileContent;
}
}
Expand Down

0 comments on commit 694b7b2

Please sign in to comment.