From fb655b81a6230d4a84895adfdc70b963f925dd0b Mon Sep 17 00:00:00 2001 From: Peter Dolkens Date: Wed, 7 Aug 2013 17:37:05 +1000 Subject: [PATCH 1/4] Relative URL / CSS fixes --- Rotativa/ViewAsPdf.cs | 16 ++++++++++++---- Rotativa/WkhtmltopdfDriver.cs | 26 +++++++++++++------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/Rotativa/ViewAsPdf.cs b/Rotativa/ViewAsPdf.cs index e191bcf..d7d0a2f 100644 --- a/Rotativa/ViewAsPdf.cs +++ b/Rotativa/ViewAsPdf.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Text; +using System.Text.RegularExpressions; using System.Web; using System.Web.Mvc; @@ -8,6 +9,9 @@ namespace Rotativa { public class ViewAsPdf : AsPdfResultBase { + private Regex _urlRootReplacement = new Regex(@"( href=[""']| src=[""']|[ :]url\()/", RegexOptions.IgnoreCase | RegexOptions.Multiline); + private Regex _urlRelativeReplacement = new Regex(@"( href=[""']| src=[""']|[ :]url\()(?!(?:http|https|ftp):)", RegexOptions.IgnoreCase | RegexOptions.Multiline); + private string _viewName; public string ViewName @@ -101,11 +105,15 @@ protected override byte[] CallTheDriver(ControllerContext context) 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)); + string baseUrl = string.Format("{0}://{1}/", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority); + string relativeUrl = string.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority, HttpContext.Current.Request.Url.AbsolutePath); + + if (!relativeUrl.EndsWith("/")) relativeUrl = relativeUrl + "/"; + + string buffer = _urlRootReplacement.Replace(html.ToString(), String.Format("$1{0}", baseUrl)); + buffer = _urlRelativeReplacement.Replace(buffer, String.Format("$1{0}", relativeUrl)); - var fileContent = WkhtmltopdfDriver.ConvertHtml(WkhtmltopdfPath, GetConvertOptions(), html.ToString()); + var fileContent = WkhtmltopdfDriver.ConvertHtml(WkhtmltopdfPath, GetConvertOptions(), buffer); return fileContent; } } diff --git a/Rotativa/WkhtmltopdfDriver.cs b/Rotativa/WkhtmltopdfDriver.cs index e307f59..c427a13 100644 --- a/Rotativa/WkhtmltopdfDriver.cs +++ b/Rotativa/WkhtmltopdfDriver.cs @@ -53,19 +53,19 @@ private static byte[] Convert(string wkhtmltopdfPath, string switches, string ht } var proc = new Process - { - StartInfo = new ProcessStartInfo - { - FileName = Path.Combine(wkhtmltopdfPath, "wkhtmltopdf.exe"), - Arguments = switches, - UseShellExecute = false, - RedirectStandardOutput = true, - RedirectStandardError = true, - RedirectStandardInput = true, - WorkingDirectory = wkhtmltopdfPath, - CreateNoWindow = true - } - }; + { + StartInfo = new ProcessStartInfo + { + FileName = Path.Combine(wkhtmltopdfPath, "wkhtmltopdf.exe"), + Arguments = switches, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + RedirectStandardInput = true, + WorkingDirectory = wkhtmltopdfPath, + CreateNoWindow = true + } + }; proc.Start(); // generate PDF from given HTML string, not from URL From 61ef590929a18a362253f05b5ee23693302cb879 Mon Sep 17 00:00:00 2001 From: Peter Dolkens Date: Thu, 8 Aug 2013 09:20:28 +1000 Subject: [PATCH 2/4] Update to VS2012 (Backwards compatible with VS2010) --- Rotativa.Demo/Rotativa.Demo.csproj | 13 ++++++++++++- Rotativa.sln | 12 ++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Rotativa.Demo/Rotativa.Demo.csproj b/Rotativa.Demo/Rotativa.Demo.csproj index a4f16b7..daef675 100644 --- a/Rotativa.Demo/Rotativa.Demo.csproj +++ b/Rotativa.Demo/Rotativa.Demo.csproj @@ -1,5 +1,6 @@  + Debug AnyCPU @@ -17,6 +18,11 @@ true ..\ true + + + + + 4.0 true @@ -172,8 +178,13 @@ + + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + - + + + \ No newline at end of file diff --git a/Rotativa.sln b/Rotativa.sln index efe50ff..07ab80e 100644 --- a/Rotativa.sln +++ b/Rotativa.sln @@ -16,6 +16,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rotativa.Tests", "Rotativa. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rotativa.UnitTests", "Rotativa.UnitTests\Rotativa.UnitTests.csproj", "{26A2783C-662D-4885-BD2B-A9AFB55C6BF9}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rotativa.MVC4", "Rotativa.MVC4\Rotativa.MVC4.csproj", "{733E5EA3-068F-439D-B0F9-35D06C668657}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -38,6 +40,10 @@ Global {26A2783C-662D-4885-BD2B-A9AFB55C6BF9}.Debug|Any CPU.Build.0 = Debug|Any CPU {26A2783C-662D-4885-BD2B-A9AFB55C6BF9}.Release|Any CPU.ActiveCfg = Release|Any CPU {26A2783C-662D-4885-BD2B-A9AFB55C6BF9}.Release|Any CPU.Build.0 = Release|Any CPU + {733E5EA3-068F-439D-B0F9-35D06C668657}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {733E5EA3-068F-439D-B0F9-35D06C668657}.Debug|Any CPU.Build.0 = Debug|Any CPU + {733E5EA3-068F-439D-B0F9-35D06C668657}.Release|Any CPU.ActiveCfg = Release|Any CPU + {733E5EA3-068F-439D-B0F9-35D06C668657}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE