From 4a564118ff211b5675888f5ed071ef903e5c72a7 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Tue, 8 Sep 2020 20:34:59 +0300
Subject: [PATCH 01/29] Added a smoke test.
---
Wkhtmltopdf.NetCore.Test/SmokeTest.cs | 16 +++++++++
.../Wkhtmltopdf.NetCore.Test.csproj | 36 +++++++++++++++++++
Wkhtmltopdf.NetCore.sln | 6 ++++
3 files changed, 58 insertions(+)
create mode 100644 Wkhtmltopdf.NetCore.Test/SmokeTest.cs
create mode 100644 Wkhtmltopdf.NetCore.Test/Wkhtmltopdf.NetCore.Test.csproj
diff --git a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
new file mode 100644
index 0000000..cb208e7
--- /dev/null
+++ b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
@@ -0,0 +1,16 @@
+using System;
+using NUnit.Framework;
+
+namespace Wkhtmltopdf.NetCore.Test
+{
+ public class SmokeTest
+ {
+ [Test]
+ public void Test1()
+ {
+ WkhtmltopdfConfiguration.RotativaPath = AppDomain.CurrentDomain.BaseDirectory + "Rotativa";
+ var generatePdf = new GeneratePdf(null);
+ generatePdf.GetPDF("Hello World
This is html rendered text
");
+ }
+ }
+}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore.Test/Wkhtmltopdf.NetCore.Test.csproj b/Wkhtmltopdf.NetCore.Test/Wkhtmltopdf.NetCore.Test.csproj
new file mode 100644
index 0000000..6761594
--- /dev/null
+++ b/Wkhtmltopdf.NetCore.Test/Wkhtmltopdf.NetCore.Test.csproj
@@ -0,0 +1,36 @@
+
+
+
+ netcoreapp3.1
+ false
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+
+
diff --git a/Wkhtmltopdf.NetCore.sln b/Wkhtmltopdf.NetCore.sln
index 2240f5e..f71c90d 100644
--- a/Wkhtmltopdf.NetCore.sln
+++ b/Wkhtmltopdf.NetCore.sln
@@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.28010.2041
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wkhtmltopdf.NetCore", "Wkhtmltopdf.NetCore\Wkhtmltopdf.NetCore.csproj", "{AD64E16D-A86E-47AD-8DEF-0B1346A5E30D}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wkhtmltopdf.NetCore.Test", "Wkhtmltopdf.NetCore.Test\Wkhtmltopdf.NetCore.Test.csproj", "{DAB48830-F9C3-4257-A9BB-5797196CB654}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
{AD64E16D-A86E-47AD-8DEF-0B1346A5E30D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD64E16D-A86E-47AD-8DEF-0B1346A5E30D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD64E16D-A86E-47AD-8DEF-0B1346A5E30D}.Release|Any CPU.Build.0 = Release|Any CPU
+ {DAB48830-F9C3-4257-A9BB-5797196CB654}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {DAB48830-F9C3-4257-A9BB-5797196CB654}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {DAB48830-F9C3-4257-A9BB-5797196CB654}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {DAB48830-F9C3-4257-A9BB-5797196CB654}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
From c7cf359ce25a1e44cb43f0162e71745c0689931f Mon Sep 17 00:00:00 2001
From: gurustron
Date: Tue, 8 Sep 2020 20:59:28 +0300
Subject: [PATCH 02/29] Small clean up and a smoke test.
---
.../Implementation/ConvertOptions.cs | 8 +-
.../Implementation/GeneratePdf.cs | 77 ++++++-------------
.../Interfaces/IConvertOptions.cs | 8 +-
.../Implementation/Interfaces/IGeneratePdf.cs | 20 ++---
Wkhtmltopdf.NetCore/WkhtmlDriver.cs | 29 +++----
.../WkhtmltopdfConfiguration.cs | 2 -
6 files changed, 50 insertions(+), 94 deletions(-)
diff --git a/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs b/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
index de6f948..31afb46 100644
--- a/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
@@ -11,7 +11,7 @@ public class ConvertOptions : IConvertOptions
{
public ConvertOptions()
{
- this.PageMargins = new Margins();
+ PageMargins = new Margins();
}
///
@@ -109,8 +109,8 @@ public string GetConvertOptions()
{
var result = new StringBuilder();
- if (this.PageMargins != null)
- result.Append(this.PageMargins.ToString());
+ if (PageMargins != null)
+ result.Append(PageMargins);
result.Append(" ");
result.Append(GetConvertBaseOptions());
@@ -122,7 +122,7 @@ protected string GetConvertBaseOptions()
{
var result = new StringBuilder();
- var fields = this.GetType().GetProperties();
+ var fields = GetType().GetProperties();
foreach (var fi in fields)
{
var of = fi.GetCustomAttributes(typeof(OptionFlag), true).FirstOrDefault() as OptionFlag;
diff --git a/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs b/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
index da619b3..fac8eea 100644
--- a/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
@@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Mvc;
-using System;
using System.IO;
using System.Threading.Tasks;
using Wkhtmltopdf.NetCore.Interfaces;
@@ -8,8 +7,8 @@ namespace Wkhtmltopdf.NetCore
{
public class GeneratePdf : IGeneratePdf
{
- protected IConvertOptions _convertOptions;
- readonly IRazorViewToStringRenderer _engine;
+ private IConvertOptions _convertOptions;
+ private readonly IRazorViewToStringRenderer _engine;
public GeneratePdf(IRazorViewToStringRenderer engine)
{
@@ -27,22 +26,15 @@ public byte[] GetPDF(string html)
return WkhtmlDriver.Convert(WkhtmltopdfConfiguration.RotativaPath, _convertOptions.GetConvertOptions(), html);
}
- public async Task GetByteArray(string View, T model)
+ public async Task GetByteArray(string view, T model)
{
- try
- {
- var html = await _engine.RenderViewToStringAsync(View, model);
- return GetPDF(html);
- }
- catch (Exception ex)
- {
- throw ex;
- }
+ var html = await _engine.RenderViewToStringAsync(view, model);
+ return GetPDF(html);
}
- public async Task GetPdf(string View, T model)
+ public async Task GetPdf(string view, T model)
{
- var html = await _engine.RenderViewToStringAsync(View, model);
+ var html = await _engine.RenderViewToStringAsync(view, model);
var byteArray = GetPDF(html);
MemoryStream pdfStream = new MemoryStream();
pdfStream.Write(byteArray, 0, byteArray.Length);
@@ -50,9 +42,9 @@ public async Task GetPdf(string View, T model)
return new FileStreamResult(pdfStream, "application/pdf");
}
- public async Task GetPdfViewInHtml(string ViewInHtml, T model)
+ public async Task GetPdfViewInHtml(string viewInHtml, T model)
{
- var html = await _engine.RenderHtmlToStringAsync(ViewInHtml, model);
+ var html = await _engine.RenderHtmlToStringAsync(viewInHtml, model);
var byteArray = GetPDF(html);
MemoryStream pdfStream = new MemoryStream();
pdfStream.Write(byteArray, 0, byteArray.Length);
@@ -60,28 +52,21 @@ public async Task GetPdfViewInHtml(string ViewInHtml, T model)
return new FileStreamResult(pdfStream, "application/pdf");
}
- public async Task GetByteArrayViewInHtml(string ViewInHtml, T model)
+ public async Task GetByteArrayViewInHtml(string viewInHtml, T model)
{
- try
- {
- var view = await _engine.RenderHtmlToStringAsync(ViewInHtml, model);
- return GetPDF(view);
- }
- catch (Exception ex)
- {
- throw ex;
- }
+ var view = await _engine.RenderHtmlToStringAsync(viewInHtml, model);
+ return GetPDF(view);
}
- public void AddView(string path, string viewHTML) => _engine.AddView(path, viewHTML);
+ public void AddView(string path, string viewHtml) => _engine.AddView(path, viewHtml);
public bool ExistsView(string path) => _engine.ExistsView(path);
- public void UpdateView(string path, string viewHTML) => _engine.UpdateView(path, viewHTML);
+ public void UpdateView(string path, string viewHtml) => _engine.UpdateView(path, viewHtml);
- public async Task GetPdfViewInHtml(string ViewInHtml)
+ public async Task GetPdfViewInHtml(string viewInHtml)
{
- var html = await _engine.RenderHtmlToStringAsync(ViewInHtml);
+ var html = await _engine.RenderHtmlToStringAsync(viewInHtml);
var byteArray = GetPDF(html);
MemoryStream pdfStream = new MemoryStream();
pdfStream.Write(byteArray, 0, byteArray.Length);
@@ -89,22 +74,15 @@ public async Task GetPdfViewInHtml(string ViewInHtml)
return new FileStreamResult(pdfStream, "application/pdf");
}
- public async Task GetByteArrayViewInHtml(string ViewInHtml)
+ public async Task GetByteArrayViewInHtml(string viewInHtml)
{
- try
- {
- var view = await _engine.RenderHtmlToStringAsync(ViewInHtml);
- return GetPDF(view);
- }
- catch (Exception ex)
- {
- throw ex;
- }
+ var view = await _engine.RenderHtmlToStringAsync(viewInHtml);
+ return GetPDF(view);
}
- public async Task GetPdf(string View)
+ public async Task GetPdf(string view)
{
- var html = await _engine.RenderViewToStringAsync(View);
+ var html = await _engine.RenderViewToStringAsync(view);
var byteArray = GetPDF(html);
MemoryStream pdfStream = new MemoryStream();
pdfStream.Write(byteArray, 0, byteArray.Length);
@@ -112,17 +90,10 @@ public async Task GetPdf(string View)
return new FileStreamResult(pdfStream, "application/pdf");
}
- public async Task GetByteArray(string View)
+ public async Task GetByteArray(string view)
{
- try
- {
- var html = await _engine.RenderViewToStringAsync(View);
- return GetPDF(html);
- }
- catch (Exception ex)
- {
- throw ex;
- }
+ var html = await _engine.RenderViewToStringAsync(view);
+ return GetPDF(html);
}
}
}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore/Implementation/Interfaces/IConvertOptions.cs b/Wkhtmltopdf.NetCore/Implementation/Interfaces/IConvertOptions.cs
index e661eab..51ce2ce 100644
--- a/Wkhtmltopdf.NetCore/Implementation/Interfaces/IConvertOptions.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/Interfaces/IConvertOptions.cs
@@ -1,10 +1,4 @@
-using Wkhtmltopdf.NetCore.Options;
-using System.Collections.Generic;
-using System.Globalization;
-using System.Linq;
-using System.Text;
-
-namespace Wkhtmltopdf.NetCore.Interfaces
+namespace Wkhtmltopdf.NetCore.Interfaces
{
public interface IConvertOptions
{
diff --git a/Wkhtmltopdf.NetCore/Implementation/Interfaces/IGeneratePdf.cs b/Wkhtmltopdf.NetCore/Implementation/Interfaces/IGeneratePdf.cs
index d411eef..9578f06 100644
--- a/Wkhtmltopdf.NetCore/Implementation/Interfaces/IGeneratePdf.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/Interfaces/IGeneratePdf.cs
@@ -6,18 +6,18 @@ namespace Wkhtmltopdf.NetCore
{
public interface IGeneratePdf
{
- Task GetPdfViewInHtml(string ViewInHtml, T model);
- Task GetByteArrayViewInHtml(string ViewInHtml, T model);
- Task GetPdf(string View, T model);
- Task GetByteArray(string View, T model);
- Task GetPdfViewInHtml(string ViewInHtml);
- Task GetByteArrayViewInHtml(string ViewInHtml);
- Task GetPdf(string View);
- Task GetByteArray(string View);
+ Task GetPdfViewInHtml(string viewInHtml, T model);
+ Task GetByteArrayViewInHtml(string viewInHtml, T model);
+ Task GetPdf(string view, T model);
+ Task GetByteArray(string view, T model);
+ Task GetPdfViewInHtml(string viewInHtml);
+ Task GetByteArrayViewInHtml(string viewInHtml);
+ Task GetPdf(string view);
+ Task GetByteArray(string view);
void SetConvertOptions(IConvertOptions options);
byte[] GetPDF(string html);
- void UpdateView(string path, string viewHTML);
+ void UpdateView(string path, string viewHtml);
bool ExistsView(string path);
- void AddView(string path, string viewHTML);
+ void AddView(string path, string viewHtml);
}
}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore/WkhtmlDriver.cs b/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
index 2c22257..2c291ba 100644
--- a/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
+++ b/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
@@ -52,25 +52,18 @@ public static byte[] Convert(string wkhtmlPath, string switches, string html)
using (var proc = new Process())
{
- try
+ proc.StartInfo = new ProcessStartInfo
{
- proc.StartInfo = new ProcessStartInfo
- {
- FileName = rotativaLocation,
- Arguments = switches,
- UseShellExecute = false,
- RedirectStandardOutput = true,
- RedirectStandardError = true,
- RedirectStandardInput = true,
- CreateNoWindow = true
- };
-
- proc.Start();
- }
- catch (Exception ex)
- {
- throw ex;
- }
+ FileName = rotativaLocation,
+ Arguments = switches,
+ UseShellExecute = false,
+ RedirectStandardOutput = true,
+ RedirectStandardError = true,
+ RedirectStandardInput = true,
+ CreateNoWindow = true
+ };
+
+ proc.Start();
// generate PDF from given HTML string, not from URL
if (!string.IsNullOrEmpty(html))
diff --git a/Wkhtmltopdf.NetCore/WkhtmltopdfConfiguration.cs b/Wkhtmltopdf.NetCore/WkhtmltopdfConfiguration.cs
index f4f0053..a448cd3 100644
--- a/Wkhtmltopdf.NetCore/WkhtmltopdfConfiguration.cs
+++ b/Wkhtmltopdf.NetCore/WkhtmltopdfConfiguration.cs
@@ -1,7 +1,5 @@
using Microsoft.AspNetCore.Mvc;
-using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Razor;
-using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
From a67bf523cb693c9e296b7d822bbb816974959eb0 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Tue, 8 Sep 2020 21:16:27 +0300
Subject: [PATCH 03/29] some more clean up
---
.../Options/MarginsTest.cs | 26 +++++++++++++++++++
Wkhtmltopdf.NetCore.Test/SmokeTest.cs | 2 +-
.../Implementation/ConvertOptions.cs | 4 +--
.../Implementation/GeneratePdf.cs | 17 ++++++------
4 files changed, 38 insertions(+), 11 deletions(-)
create mode 100644 Wkhtmltopdf.NetCore.Test/Options/MarginsTest.cs
diff --git a/Wkhtmltopdf.NetCore.Test/Options/MarginsTest.cs b/Wkhtmltopdf.NetCore.Test/Options/MarginsTest.cs
new file mode 100644
index 0000000..f8e9260
--- /dev/null
+++ b/Wkhtmltopdf.NetCore.Test/Options/MarginsTest.cs
@@ -0,0 +1,26 @@
+using NUnit.Framework;
+using Wkhtmltopdf.NetCore.Options;
+
+namespace Wkhtmltopdf.NetCore.Test.Options
+{
+ public class MarginsTest
+ {
+ [TestCase("-B 1 -L 2 -R 3 -T 4", 1,2,3,4)]
+ [TestCase("-B 1 -L 2 -R 3", 1,2,3,null)]
+ [TestCase("-B 1 -L 2", 1,2,null,null)]
+ [TestCase("-B 1", 1,null,null,null)]
+ [TestCase("", null,null,null,null)]
+ public void Converts(string expected, int? bottom, int? left, int? right, int? top)
+ {
+ var margins = new Margins
+ {
+ Bottom = bottom,
+ Left = left,
+ Right = right,
+ Top = top,
+ };
+
+ Assert.AreEqual(expected, margins.ToString());
+ }
+ }
+}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
index cb208e7..0fe0043 100644
--- a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
+++ b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
@@ -6,7 +6,7 @@ namespace Wkhtmltopdf.NetCore.Test
public class SmokeTest
{
[Test]
- public void Test1()
+ public void CanConvert()
{
WkhtmltopdfConfiguration.RotativaPath = AppDomain.CurrentDomain.BaseDirectory + "Rotativa";
var generatePdf = new GeneratePdf(null);
diff --git a/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs b/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
index 31afb46..961d8d2 100644
--- a/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
@@ -122,8 +122,8 @@ protected string GetConvertBaseOptions()
{
var result = new StringBuilder();
- var fields = GetType().GetProperties();
- foreach (var fi in fields)
+ var properties = GetType().GetProperties();
+ foreach (var fi in properties)
{
var of = fi.GetCustomAttributes(typeof(OptionFlag), true).FirstOrDefault() as OptionFlag;
if (of == null)
diff --git a/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs b/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
index fac8eea..7721008 100644
--- a/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
@@ -1,14 +1,14 @@
-using Microsoft.AspNetCore.Mvc;
-using System.IO;
+using System.IO;
using System.Threading.Tasks;
+using Microsoft.AspNetCore.Mvc;
using Wkhtmltopdf.NetCore.Interfaces;
namespace Wkhtmltopdf.NetCore
{
public class GeneratePdf : IGeneratePdf
{
- private IConvertOptions _convertOptions;
private readonly IRazorViewToStringRenderer _engine;
+ private IConvertOptions _convertOptions;
public GeneratePdf(IRazorViewToStringRenderer engine)
{
@@ -23,7 +23,8 @@ public void SetConvertOptions(IConvertOptions convertOptions)
public byte[] GetPDF(string html)
{
- return WkhtmlDriver.Convert(WkhtmltopdfConfiguration.RotativaPath, _convertOptions.GetConvertOptions(), html);
+ return WkhtmlDriver.Convert(WkhtmltopdfConfiguration.RotativaPath, _convertOptions.GetConvertOptions(),
+ html);
}
public async Task GetByteArray(string view, T model)
@@ -36,7 +37,7 @@ public async Task GetPdf(string view, T model)
{
var html = await _engine.RenderViewToStringAsync(view, model);
var byteArray = GetPDF(html);
- MemoryStream pdfStream = new MemoryStream();
+ var pdfStream = new MemoryStream();
pdfStream.Write(byteArray, 0, byteArray.Length);
pdfStream.Position = 0;
return new FileStreamResult(pdfStream, "application/pdf");
@@ -46,7 +47,7 @@ public async Task GetPdfViewInHtml(string viewInHtml, T model)
{
var html = await _engine.RenderHtmlToStringAsync(viewInHtml, model);
var byteArray = GetPDF(html);
- MemoryStream pdfStream = new MemoryStream();
+ var pdfStream = new MemoryStream();
pdfStream.Write(byteArray, 0, byteArray.Length);
pdfStream.Position = 0;
return new FileStreamResult(pdfStream, "application/pdf");
@@ -68,7 +69,7 @@ public async Task GetPdfViewInHtml(string viewInHtml)
{
var html = await _engine.RenderHtmlToStringAsync(viewInHtml);
var byteArray = GetPDF(html);
- MemoryStream pdfStream = new MemoryStream();
+ var pdfStream = new MemoryStream();
pdfStream.Write(byteArray, 0, byteArray.Length);
pdfStream.Position = 0;
return new FileStreamResult(pdfStream, "application/pdf");
@@ -84,7 +85,7 @@ public async Task GetPdf(string view)
{
var html = await _engine.RenderViewToStringAsync(view);
var byteArray = GetPDF(html);
- MemoryStream pdfStream = new MemoryStream();
+ var pdfStream = new MemoryStream();
pdfStream.Write(byteArray, 0, byteArray.Length);
pdfStream.Position = 0;
return new FileStreamResult(pdfStream, "application/pdf");
From c4fa6b7fcd9e16c409eaa01f37b4ad563734778b Mon Sep 17 00:00:00 2001
From: gurustron
Date: Tue, 8 Sep 2020 23:27:16 +0300
Subject: [PATCH 04/29] ConvertOptions tests.
---
.../Options/ConvertOptionsTest.cs | 114 ++++++++++++++++++
.../Implementation/ConvertOptions.cs | 4 +-
2 files changed, 115 insertions(+), 3 deletions(-)
create mode 100644 Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTest.cs
diff --git a/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTest.cs b/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTest.cs
new file mode 100644
index 0000000..e97e138
--- /dev/null
+++ b/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTest.cs
@@ -0,0 +1,114 @@
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using NUnit.Framework;
+using Wkhtmltopdf.NetCore.Interfaces;
+using Wkhtmltopdf.NetCore.Options;
+
+namespace Wkhtmltopdf.NetCore.Test.Options
+{
+ public class ConvertOptionsTest
+ {
+ [Test]
+ public void ConvertsEmpty()
+ {
+ IConvertOptions options = new ConvertOptions();
+ Assert.AreEqual(string.Empty, options.GetConvertOptions());
+ }
+
+ [Test]
+ public void ConvertsAll()
+ {
+ var switches = new List();
+ var counter = 0;
+ IConvertOptions options = new ConvertOptions
+ {
+ Copies = AddWithFormat(counter++, "--copies {0}"),
+ EnableForms = AddWithValue(true, "--enable-forms"),
+ FooterHtml = AddWithFormat(nameof(ConvertOptions.FooterHtml), "--footer-html {0}"),
+ FooterSpacing = AddWithFormat(counter++, "--footer-spacing {0}"),
+ HeaderHtml = AddWithFormat(nameof(ConvertOptions.HeaderHtml), "--header-html {0}"),
+ HeaderSpacing = AddWithFormat(counter++, "--header-spacing {0}"),
+ PageHeight = AddWithFormat(counter++ + 0.5, "--page-height {0}"),
+ PageMargins = AddWithValues(new Margins(1, 2, 3, 4),
+ new[] {"-B 3", "-L 4", "-R 2", "-T 1"}),
+ PageOrientation = AddWithFormat(Orientation.Landscape, "-O {0}"),
+ PageSize = AddWithFormat(Size.Legal, "-s {0}"),
+ PageWidth = AddWithFormat(counter++ + 0.5, "--page-width {0}"),
+ IsGrayScale = AddWithValue(true, "-g"),
+ IsLowQuality = AddWithValue(true, "-l"),
+ Replacements = AddWithValues(new Dictionary
+ {
+ {"one", "1"},
+ {"two", "2"}
+ },
+ new[] {"--replace \"one\" \"1\"", "--replace \"two\" \"2\""})
+ };
+
+ var result = options.GetConvertOptions();
+
+ var allSwitches = typeof(ConvertOptions).GetProperties()
+ .Select(p => p.GetCustomAttribute())
+ .Where(a => a != null)
+ .Select(a => a.Name);
+ foreach (var @switch in allSwitches)
+ {
+ StringAssert.Contains(@switch, result, $"Switch \"{@switch}\" was not set up.");
+ }
+
+ var sb = new StringBuilder(result);
+ foreach (var @switch in switches)
+ {
+ StringAssert.Contains(@switch, result, $"Should contain \"{@switch}\".");
+ sb.Replace(@switch, string.Empty);
+ }
+ Assert.IsEmpty(sb.ToString().Trim());
+
+ // Local functions
+
+ T AddWithValue(T value, string switchToAdd)
+ {
+ switches.Add(switchToAdd);
+ return value;
+ }
+
+ T AddWithValues(T value, IEnumerable switchesToAdd)
+ {
+ switches.AddRange(switchesToAdd);
+ return value;
+ }
+
+ T AddWithFormat(T value, string formatToAdd)
+ {
+ switches.Add(string.Format(formatToAdd, value));
+ return value;
+ }
+ }
+
+ [Test]
+ public void ConvertsChild()
+ {
+ IConvertOptions options = new CustomConvertOptions
+ {
+ Test1 = nameof(CustomConvertOptions.Test1),
+ Test2 = true,
+ Ignored1 = "Ignored",
+ Ignored2 = "Ignored"
+ };
+
+ var result = options.GetConvertOptions();
+
+ StringAssert.Contains($"-t1 {nameof(CustomConvertOptions.Test1)}", result);
+ StringAssert.Contains($"-t2", result);
+ }
+
+ private class CustomConvertOptions : ConvertOptions
+ {
+ [OptionFlag("-t1")] public string Test1 { get; set; }
+ [OptionFlag("-t2")] public bool Test2 { get; set; }
+ [OptionFlag("-ignr")] public string Ignored1;
+ public string Ignored2;
+ }
+ }
+}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs b/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
index 961d8d2..a89a009 100644
--- a/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
@@ -109,9 +109,7 @@ public string GetConvertOptions()
{
var result = new StringBuilder();
- if (PageMargins != null)
- result.Append(PageMargins);
-
+ result.Append(PageMargins);
result.Append(" ");
result.Append(GetConvertBaseOptions());
From f71ffd5c2069683a9b189c92e5f9b37df2242380 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Tue, 8 Sep 2020 23:27:56 +0300
Subject: [PATCH 05/29] rename test classes
---
.../Options/{ConvertOptionsTest.cs => ConvertOptionsTests.cs} | 2 +-
.../Options/{MarginsTest.cs => MarginsTests.cs} | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
rename Wkhtmltopdf.NetCore.Test/Options/{ConvertOptionsTest.cs => ConvertOptionsTests.cs} (99%)
rename Wkhtmltopdf.NetCore.Test/Options/{MarginsTest.cs => MarginsTests.cs} (96%)
diff --git a/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTest.cs b/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
similarity index 99%
rename from Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTest.cs
rename to Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
index e97e138..40e5d1c 100644
--- a/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTest.cs
+++ b/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
@@ -8,7 +8,7 @@
namespace Wkhtmltopdf.NetCore.Test.Options
{
- public class ConvertOptionsTest
+ public class ConvertOptionsTests
{
[Test]
public void ConvertsEmpty()
diff --git a/Wkhtmltopdf.NetCore.Test/Options/MarginsTest.cs b/Wkhtmltopdf.NetCore.Test/Options/MarginsTests.cs
similarity index 96%
rename from Wkhtmltopdf.NetCore.Test/Options/MarginsTest.cs
rename to Wkhtmltopdf.NetCore.Test/Options/MarginsTests.cs
index f8e9260..360c0af 100644
--- a/Wkhtmltopdf.NetCore.Test/Options/MarginsTest.cs
+++ b/Wkhtmltopdf.NetCore.Test/Options/MarginsTests.cs
@@ -3,7 +3,7 @@
namespace Wkhtmltopdf.NetCore.Test.Options
{
- public class MarginsTest
+ public class MarginsTests
{
[TestCase("-B 1 -L 2 -R 3 -T 4", 1,2,3,4)]
[TestCase("-B 1 -L 2 -R 3", 1,2,3,null)]
From 3e068d773f2241abafc841f425e3a286c9ff3610 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Tue, 8 Sep 2020 23:35:46 +0300
Subject: [PATCH 06/29] end of files.
---
Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs | 2 +-
Wkhtmltopdf.NetCore.Test/Options/MarginsTests.cs | 2 +-
Wkhtmltopdf.NetCore.Test/SmokeTest.cs | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs b/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
index 40e5d1c..2239be3 100644
--- a/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
+++ b/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
@@ -111,4 +111,4 @@ private class CustomConvertOptions : ConvertOptions
public string Ignored2;
}
}
-}
\ No newline at end of file
+}
diff --git a/Wkhtmltopdf.NetCore.Test/Options/MarginsTests.cs b/Wkhtmltopdf.NetCore.Test/Options/MarginsTests.cs
index 360c0af..b3d0032 100644
--- a/Wkhtmltopdf.NetCore.Test/Options/MarginsTests.cs
+++ b/Wkhtmltopdf.NetCore.Test/Options/MarginsTests.cs
@@ -23,4 +23,4 @@ public void Converts(string expected, int? bottom, int? left, int? right, int? t
Assert.AreEqual(expected, margins.ToString());
}
}
-}
\ No newline at end of file
+}
diff --git a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
index 0fe0043..7a2af30 100644
--- a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
+++ b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
@@ -13,4 +13,4 @@ public void CanConvert()
generatePdf.GetPDF("Hello World
This is html rendered text");
}
}
-}
\ No newline at end of file
+}
From fa1198adfd12c11e197ba5a8fe180a7c009de959 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Tue, 8 Sep 2020 23:46:51 +0300
Subject: [PATCH 07/29] fix some inspections.
---
Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs | 3 +++
.../Implementation/RazorViewToStringRenderer.cs | 6 ++++--
.../Implementation/UpdateableFileProvider.cs | 1 -
3 files changed, 7 insertions(+), 3 deletions(-)
diff --git a/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs b/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
index 2239be3..64dcbed 100644
--- a/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
+++ b/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Text;
@@ -103,6 +104,8 @@ public void ConvertsChild()
StringAssert.Contains($"-t2", result);
}
+ [SuppressMessage("ReSharper", "NotAccessedField.Local")]
+ [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local")]
private class CustomConvertOptions : ConvertOptions
{
[OptionFlag("-t1")] public string Test1 { get; set; }
diff --git a/Wkhtmltopdf.NetCore/Implementation/RazorViewToStringRenderer.cs b/Wkhtmltopdf.NetCore/Implementation/RazorViewToStringRenderer.cs
index 41eee7e..2cae0ae 100644
--- a/Wkhtmltopdf.NetCore/Implementation/RazorViewToStringRenderer.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/RazorViewToStringRenderer.cs
@@ -98,10 +98,12 @@ private IView FindView(ActionContext actionContext, string viewName)
return findViewResult.View;
}
- var searchedLocations = getViewResult.SearchedLocations.Concat(findViewResult.SearchedLocations);
+ var searchedLocations = getViewResult.SearchedLocations
+ .Concat(findViewResult.SearchedLocations);
var errorMessage = string.Join(
Environment.NewLine,
- new[] { $"Unable to find view '{viewName}'. The following locations were searched:" }.Concat(searchedLocations)); ;
+ new[] {$"Unable to find view '{viewName}'. The following locations were searched:"}
+ .Concat(searchedLocations));
throw new InvalidOperationException(errorMessage);
}
diff --git a/Wkhtmltopdf.NetCore/Implementation/UpdateableFileProvider.cs b/Wkhtmltopdf.NetCore/Implementation/UpdateableFileProvider.cs
index a5863d5..c953951 100644
--- a/Wkhtmltopdf.NetCore/Implementation/UpdateableFileProvider.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/UpdateableFileProvider.cs
@@ -1,5 +1,4 @@
using System.Collections.Generic;
-using System.Threading;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
From 80ec39664a5fd34299e6ea8bde0b732457f15a2f Mon Sep 17 00:00:00 2001
From: gurustron
Date: Tue, 8 Sep 2020 23:48:42 +0300
Subject: [PATCH 08/29] fix some inspections.
---
.../Interfaces/IRazorViewToStringRenderer.cs | 4 ++--
.../Implementation/RazorViewToStringRenderer.cs | 8 ++++----
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/Wkhtmltopdf.NetCore/Implementation/Interfaces/IRazorViewToStringRenderer.cs b/Wkhtmltopdf.NetCore/Implementation/Interfaces/IRazorViewToStringRenderer.cs
index b102cae..e0a36f6 100644
--- a/Wkhtmltopdf.NetCore/Implementation/Interfaces/IRazorViewToStringRenderer.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/Interfaces/IRazorViewToStringRenderer.cs
@@ -8,8 +8,8 @@ public interface IRazorViewToStringRenderer
Task RenderHtmlToStringAsync(string html, TModel model);
Task RenderViewToStringAsync(string viewName);
Task RenderHtmlToStringAsync(string html);
- void UpdateView(string path, string viewHTML);
+ void UpdateView(string path, string viewHtml);
bool ExistsView(string path);
- void AddView(string path, string viewHTML);
+ void AddView(string path, string viewHtml);
}
}
diff --git a/Wkhtmltopdf.NetCore/Implementation/RazorViewToStringRenderer.cs b/Wkhtmltopdf.NetCore/Implementation/RazorViewToStringRenderer.cs
index 2cae0ae..add42da 100644
--- a/Wkhtmltopdf.NetCore/Implementation/RazorViewToStringRenderer.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/RazorViewToStringRenderer.cs
@@ -108,14 +108,14 @@ private IView FindView(ActionContext actionContext, string viewName)
throw new InvalidOperationException(errorMessage);
}
- public void AddView(string path, string viewHTML)
+ public void AddView(string path, string viewHtml)
{
if (ExistsView(path))
{
throw new Exception($"View {path} already exists");
}
- UpdateableFileProvider.Views.Add($"/Views/{path}.cshtml", new ViewFileInfo(viewHTML));
+ UpdateableFileProvider.Views.Add($"/Views/{path}.cshtml", new ViewFileInfo(viewHtml));
}
public bool ExistsView(string path)
@@ -123,11 +123,11 @@ public bool ExistsView(string path)
return UpdateableFileProvider.Views.Any(x => x.Key == $"/Views/{path}.cshtml");
}
- public void UpdateView(string path, string viewHTML)
+ public void UpdateView(string path, string viewHtml)
{
if (ExistsView(path))
{
- UpdateableFileProvider.UpdateContent(viewHTML, $"/Views/{path}.cshtml");
+ UpdateableFileProvider.UpdateContent(viewHtml, $"/Views/{path}.cshtml");
}
else
{
From 3efa5ae2d9f0044c9015c550898d037544918252 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Tue, 8 Sep 2020 23:49:28 +0300
Subject: [PATCH 09/29] fix some inspections.
---
.../Options/ConvertOptionsTests.cs | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs b/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
index 64dcbed..9c218f2 100644
--- a/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
+++ b/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
@@ -25,18 +25,18 @@ public void ConvertsAll()
var counter = 0;
IConvertOptions options = new ConvertOptions
{
- Copies = AddWithFormat(counter++, "--copies {0}"),
+ Copies = AddWithFormat(++counter, "--copies {0}"),
EnableForms = AddWithValue(true, "--enable-forms"),
FooterHtml = AddWithFormat(nameof(ConvertOptions.FooterHtml), "--footer-html {0}"),
- FooterSpacing = AddWithFormat(counter++, "--footer-spacing {0}"),
+ FooterSpacing = AddWithFormat(++counter, "--footer-spacing {0}"),
HeaderHtml = AddWithFormat(nameof(ConvertOptions.HeaderHtml), "--header-html {0}"),
- HeaderSpacing = AddWithFormat(counter++, "--header-spacing {0}"),
- PageHeight = AddWithFormat(counter++ + 0.5, "--page-height {0}"),
+ HeaderSpacing = AddWithFormat(++counter, "--header-spacing {0}"),
+ PageHeight = AddWithFormat(++counter + 0.5, "--page-height {0}"),
PageMargins = AddWithValues(new Margins(1, 2, 3, 4),
new[] {"-B 3", "-L 4", "-R 2", "-T 1"}),
PageOrientation = AddWithFormat(Orientation.Landscape, "-O {0}"),
PageSize = AddWithFormat(Size.Legal, "-s {0}"),
- PageWidth = AddWithFormat(counter++ + 0.5, "--page-width {0}"),
+ PageWidth = AddWithFormat(++counter + 0.5, "--page-width {0}"),
IsGrayScale = AddWithValue(true, "-g"),
IsLowQuality = AddWithValue(true, "-l"),
Replacements = AddWithValues(new Dictionary
From a567f37fd5ea933bd44a0ea1f0f614a19131fa8e Mon Sep 17 00:00:00 2001
From: gurustron
Date: Wed, 9 Sep 2020 00:24:18 +0300
Subject: [PATCH 10/29] rename variable
---
Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs b/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
index a89a009..694c84c 100644
--- a/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
@@ -121,17 +121,17 @@ protected string GetConvertBaseOptions()
var result = new StringBuilder();
var properties = GetType().GetProperties();
- foreach (var fi in properties)
+ foreach (var pi in properties)
{
- var of = fi.GetCustomAttributes(typeof(OptionFlag), true).FirstOrDefault() as OptionFlag;
+ var of = pi.GetCustomAttributes(typeof(OptionFlag), true).FirstOrDefault() as OptionFlag;
if (of == null)
continue;
- object value = fi.GetValue(this, null);
+ object value = pi.GetValue(this, null);
if (value == null)
continue;
- if (fi.PropertyType == typeof(Dictionary))
+ if (pi.PropertyType == typeof(Dictionary))
{
var dictionary = (Dictionary)value;
foreach (var d in dictionary)
@@ -139,7 +139,7 @@ protected string GetConvertBaseOptions()
result.AppendFormat(" {0} \"{1}\" \"{2}\"", of.Name, d.Key, d.Value);
}
}
- else if (fi.PropertyType == typeof(bool))
+ else if (pi.PropertyType == typeof(bool))
{
if ((bool)value)
result.AppendFormat(CultureInfo.InvariantCulture, " {0}", of.Name);
From 18f76fd3cf7ef5a730ddd54ee3b0782edbee658d Mon Sep 17 00:00:00 2001
From: gurustron
Date: Wed, 9 Sep 2020 00:28:19 +0300
Subject: [PATCH 11/29] Add tested property.
---
Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs b/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
index 9c218f2..fc9af22 100644
--- a/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
+++ b/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
@@ -95,7 +95,8 @@ public void ConvertsChild()
Test1 = nameof(CustomConvertOptions.Test1),
Test2 = true,
Ignored1 = "Ignored",
- Ignored2 = "Ignored"
+ Ignored2 = "Ignored",
+ Ignored3 = "Ignored"
};
var result = options.GetConvertOptions();
@@ -111,7 +112,8 @@ private class CustomConvertOptions : ConvertOptions
[OptionFlag("-t1")] public string Test1 { get; set; }
[OptionFlag("-t2")] public bool Test2 { get; set; }
[OptionFlag("-ignr")] public string Ignored1;
- public string Ignored2;
+ public string Ignored2 { get; set; }
+ public string Ignored3;
}
}
}
From 7854649b6e0c6d308af6d644ba0eccaa10e0c76b Mon Sep 17 00:00:00 2001
From: gurustron
Date: Wed, 9 Sep 2020 00:30:13 +0300
Subject: [PATCH 12/29] refactor test.
---
Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs b/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
index fc9af22..45d8acf 100644
--- a/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
+++ b/Wkhtmltopdf.NetCore.Test/Options/ConvertOptionsTests.cs
@@ -101,8 +101,7 @@ public void ConvertsChild()
var result = options.GetConvertOptions();
- StringAssert.Contains($"-t1 {nameof(CustomConvertOptions.Test1)}", result);
- StringAssert.Contains($"-t2", result);
+ Assert.AreEqual($"-t1 {nameof(CustomConvertOptions.Test1)} -t2", result);
}
[SuppressMessage("ReSharper", "NotAccessedField.Local")]
From 887979ffa57137b6eca99ac2b09cb209290f1148 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Wed, 9 Sep 2020 15:07:55 +0300
Subject: [PATCH 13/29] add support for absolute paths.
---
Wkhtmltopdf.NetCore.Test/SmokeTest.cs | 24 +++++++++-
.../Configuration/AbsolutePathProvider.cs | 21 +++++++++
.../Interfaces/IWkhtmltopdfPathProvider.cs | 7 +++
.../Configuration/LegacyPathProvider.cs | 37 +++++++++++++++
.../WkhtmltopdfConfiguration.cs | 3 ++
.../Implementation/GeneratePdf.cs | 13 +++---
Wkhtmltopdf.NetCore/WkhtmlDriver.cs | 45 ++++++++++++-------
7 files changed, 127 insertions(+), 23 deletions(-)
create mode 100644 Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
create mode 100644 Wkhtmltopdf.NetCore/Configuration/Interfaces/IWkhtmltopdfPathProvider.cs
create mode 100644 Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
rename Wkhtmltopdf.NetCore/{ => Configuration}/WkhtmltopdfConfiguration.cs (96%)
diff --git a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
index 7a2af30..ac9e8af 100644
--- a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
+++ b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
@@ -5,12 +5,34 @@ namespace Wkhtmltopdf.NetCore.Test
{
public class SmokeTest
{
+ [SetUp]
+ public void Setup()
+ {
+#pragma warning disable 612
+ WkhtmltopdfConfiguration.RotativaPath = AppDomain.CurrentDomain.BaseDirectory + "Rotativa";
+#pragma warning restore 612
+ }
+
[Test]
public void CanConvert()
{
- WkhtmltopdfConfiguration.RotativaPath = AppDomain.CurrentDomain.BaseDirectory + "Rotativa";
var generatePdf = new GeneratePdf(null);
generatePdf.GetPDF("Hello World
This is html rendered text");
}
+
+ [Test]
+ public void CanConvertWithLegacyProvider()
+ {
+ var generatePdf = new GeneratePdf(null, new LegacyPathProvider());
+ generatePdf.GetPDF("Hello World
This is html rendered text");
+ }
+
+ [Test]
+ public void CanConvertWithAbsoluteProvider()
+ {
+ var path = new LegacyPathProvider().GetPath();
+ var generatePdf = new GeneratePdf(null, new AbsolutePathProvider(path));
+ generatePdf.GetPDF("Hello World
This is html rendered text");
+ }
}
}
diff --git a/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
new file mode 100644
index 0000000..a7415e7
--- /dev/null
+++ b/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
@@ -0,0 +1,21 @@
+namespace Wkhtmltopdf.NetCore
+{
+ ///
+ /// Provides absolute path to wkthmltopdf/wkthmltoimage.
+ ///
+ public class AbsolutePathProvider : IWkhtmltopdfPathProvider
+ {
+ private readonly string _path;
+
+ ///
+ /// Constructs .
+ ///
+ /// Absolute path to wkthmltopdf/wkthmltoimage.
+ public AbsolutePathProvider(string path)
+ {
+ _path = path;
+ }
+
+ public string GetPath() => _path;
+ }
+}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore/Configuration/Interfaces/IWkhtmltopdfPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/Interfaces/IWkhtmltopdfPathProvider.cs
new file mode 100644
index 0000000..2641d4d
--- /dev/null
+++ b/Wkhtmltopdf.NetCore/Configuration/Interfaces/IWkhtmltopdfPathProvider.cs
@@ -0,0 +1,7 @@
+namespace Wkhtmltopdf.NetCore
+{
+ public interface IWkhtmltopdfPathProvider
+ {
+ string GetPath();
+ }
+}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
new file mode 100644
index 0000000..f0c36f4
--- /dev/null
+++ b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
@@ -0,0 +1,37 @@
+using System;
+using System.IO;
+using System.Runtime.InteropServices;
+
+namespace Wkhtmltopdf.NetCore
+{
+ public class LegacyPathProvider : IWkhtmltopdfPathProvider
+ {
+ public string GetPath()
+ {
+#pragma warning disable 612
+ var wkhtmlPath = WkhtmltopdfConfiguration.RotativaPath;
+#pragma warning restore 612
+
+ string rotativaLocation;
+ if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+ {
+ rotativaLocation = Path.Combine(wkhtmlPath, "Windows", "wkhtmltopdf.exe");
+ }
+ else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
+ {
+ rotativaLocation = Path.Combine(wkhtmlPath, "Mac", "wkhtmltopdf");
+ }
+ else
+ {
+ rotativaLocation = Path.Combine(wkhtmlPath, "Linux", "wkhtmltopdf");
+ }
+
+ if (!File.Exists(rotativaLocation))
+ {
+ throw new Exception("wkhtmltopdf not found, searched for " + rotativaLocation);
+ }
+
+ return rotativaLocation;
+ }
+ }
+}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore/WkhtmltopdfConfiguration.cs b/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
similarity index 96%
rename from Wkhtmltopdf.NetCore/WkhtmltopdfConfiguration.cs
rename to Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
index a448cd3..091b749 100644
--- a/Wkhtmltopdf.NetCore/WkhtmltopdfConfiguration.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
@@ -10,6 +10,7 @@ namespace Wkhtmltopdf.NetCore
{
public static class WkhtmltopdfConfiguration
{
+ [Obsolete]
public static string RotativaPath { get; set; }
///
@@ -19,12 +20,14 @@ public static class WkhtmltopdfConfiguration
/// Optional. Relative path to the directory containing wkhtmltopdf. Default is "Rotativa". Download at https://wkhtmltopdf.org/downloads.html
public static IServiceCollection AddWkhtmltopdf(this IServiceCollection services, string wkhtmltopdfRelativePath = "Rotativa")
{
+#pragma warning disable 612
RotativaPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, wkhtmltopdfRelativePath);
if (!Directory.Exists(RotativaPath))
{
throw new Exception("Folder containing wkhtmltopdf not found, searched for " + RotativaPath);
}
+#pragma warning restore 612
var fileProvider = new UpdateableFileProvider();
services.TryAddTransient();
diff --git a/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs b/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
index 7721008..6467eef 100644
--- a/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
@@ -8,11 +8,13 @@ namespace Wkhtmltopdf.NetCore
public class GeneratePdf : IGeneratePdf
{
private readonly IRazorViewToStringRenderer _engine;
+ private readonly IWkhtmltopdfPathProvider _pathProvider;
private IConvertOptions _convertOptions;
- public GeneratePdf(IRazorViewToStringRenderer engine)
+ public GeneratePdf(IRazorViewToStringRenderer engine, IWkhtmltopdfPathProvider pathProvider = null)
{
_engine = engine;
+ _pathProvider = pathProvider ?? new LegacyPathProvider();
_convertOptions = new ConvertOptions();
}
@@ -21,11 +23,10 @@ public void SetConvertOptions(IConvertOptions convertOptions)
_convertOptions = convertOptions;
}
- public byte[] GetPDF(string html)
- {
- return WkhtmlDriver.Convert(WkhtmltopdfConfiguration.RotativaPath, _convertOptions.GetConvertOptions(),
- html);
- }
+ public byte[] GetPDF(string html) => WkhtmlDriver.Convert(
+ _pathProvider,
+ _convertOptions.GetConvertOptions(),
+ html);
public async Task GetByteArray(string view, T model)
{
diff --git a/Wkhtmltopdf.NetCore/WkhtmlDriver.cs b/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
index 2c291ba..5975f0d 100644
--- a/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
+++ b/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
@@ -6,7 +6,7 @@
namespace Wkhtmltopdf.NetCore
{
- public abstract class WkhtmlDriver
+ public static class WkhtmlDriver
{
///
/// Converts given URL or HTML string to PDF.
@@ -15,21 +15,9 @@ public abstract class WkhtmlDriver
/// Switches that will be passed to wkhtmltopdf binary.
/// String containing HTML code that should be converted to PDF.
/// PDF as byte array.
+ [Obsolete()]
public static byte[] Convert(string wkhtmlPath, string switches, string html)
{
- // switches:
- // "-q" - silent output, only errors - no progress messages
- // " -" - switch output to stdout
- // "- -" - switch input to stdin and output to stdout
- switches = "-q " + switches + " -";
-
- // generate PDF from given HTML string, not from URL
- if (!string.IsNullOrEmpty(html))
- {
- switches += " -";
- html = SpecialCharsEncode(html);
- }
-
string rotativaLocation;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
@@ -50,11 +38,36 @@ public static byte[] Convert(string wkhtmlPath, string switches, string html)
throw new Exception("wkhtmltopdf not found, searched for " + rotativaLocation);
}
+ return Convert(new AbsolutePathProvider(rotativaLocation), switches, html);
+ }
+
+ ///
+ /// Converts given URL or HTML string to PDF.
+ ///
+ /// Path to wkthmltopdf\wkthmltoimage.
+ /// Switches that will be passed to wkhtmltopdf binary.
+ /// String containing HTML code that should be converted to PDF.
+ /// PDF as byte array.
+ public static byte[] Convert(IWkhtmltopdfPathProvider pathProvider, string switches, string html)
+ {
+ // switches:
+ // "-q" - silent output, only errors - no progress messages
+ // " -" - switch output to stdout
+ // "- -" - switch input to stdin and output to stdout
+ switches = "-q " + switches + " -";
+
+ // generate PDF from given HTML string, not from URL
+ if (!string.IsNullOrEmpty(html))
+ {
+ switches += " -";
+ html = SpecialCharsEncode(html);
+ }
+
using (var proc = new Process())
{
proc.StartInfo = new ProcessStartInfo
{
- FileName = rotativaLocation,
+ FileName = pathProvider.GetPath(),
Arguments = switches,
UseShellExecute = false,
RedirectStandardOutput = true,
@@ -100,7 +113,7 @@ public static byte[] Convert(string wkhtmlPath, string switches, string html)
}
}
}
-
+
///
/// Encode all special chars
///
From 89a31b49fa2259618e66ef99bb6a1d8132ba500a Mon Sep 17 00:00:00 2001
From: gurustron
Date: Wed, 9 Sep 2020 17:37:56 +0300
Subject: [PATCH 14/29] WIP
---
.../Configuration/WkhtmltopdfConfiguration.cs | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs b/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
index 091b749..c9d843d 100644
--- a/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
@@ -29,6 +29,11 @@ public static IServiceCollection AddWkhtmltopdf(this IServiceCollection services
}
#pragma warning restore 612
+ return AddCore(services);
+ }
+
+ private static IServiceCollection AddCore(IServiceCollection services)
+ {
var fileProvider = new UpdateableFileProvider();
services.TryAddTransient();
services.TryAddSingleton(fileProvider);
From f352554c81e53006c5b5f478b731a3ab34ac2b87 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Wed, 9 Sep 2020 19:07:33 +0300
Subject: [PATCH 15/29] New DI.
---
Wkhtmltopdf.NetCore.Test/SmokeTest.cs | 37 ++++++++++++++-
.../Configuration/LegacyPathProvider.cs | 2 +
.../Configuration/WkhtmltopdfConfiguration.cs | 45 +++++++++++++++----
.../Implementation/GeneratePdf.cs | 2 +-
Wkhtmltopdf.NetCore/WkhtmlDriver.cs | 2 +-
5 files changed, 75 insertions(+), 13 deletions(-)
diff --git a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
index ac9e8af..1a8a141 100644
--- a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
+++ b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
@@ -1,4 +1,6 @@
using System;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
using NUnit.Framework;
namespace Wkhtmltopdf.NetCore.Test
@@ -19,14 +21,14 @@ public void CanConvert()
var generatePdf = new GeneratePdf(null);
generatePdf.GetPDF("Hello World
This is html rendered text");
}
-
+
[Test]
public void CanConvertWithLegacyProvider()
{
var generatePdf = new GeneratePdf(null, new LegacyPathProvider());
generatePdf.GetPDF("Hello World
This is html rendered text");
}
-
+
[Test]
public void CanConvertWithAbsoluteProvider()
{
@@ -34,5 +36,36 @@ public void CanConvertWithAbsoluteProvider()
var generatePdf = new GeneratePdf(null, new AbsolutePathProvider(path));
generatePdf.GetPDF("Hello World
This is html rendered text");
}
+
+ [Test]
+ public void LegacyResolutionWorks()
+ {
+ using var host = Host.CreateDefaultBuilder()
+ .ConfigureWebHostDefaults(webBuilder =>
+ {
+#pragma warning disable 612
+ webBuilder.ConfigureServices(services => services.AddWkhtmltopdf());
+#pragma warning restore 612
+ })
+ .Build();
+
+ using var serviceScope = host.Services.CreateScope();
+ var generatePdf = serviceScope.ServiceProvider.GetService();
+ generatePdf.GetPDF("Hello World
This is html rendered text");
+ }
+
+ [Test]
+ public void ResolutionWorks()
+ {
+ using var host = Host.CreateDefaultBuilder()
+ .ConfigureWebHostDefaults(webBuilder => webBuilder
+ .ConfigureServices(services =>
+ services.AddMvc().AddWkhtmltopdf()))
+ .Build();
+
+ using var serviceScope = host.Services.CreateScope();
+ var generatePdf = serviceScope.ServiceProvider.GetService();
+ generatePdf.GetPDF("Hello World
This is html rendered text");
+ }
}
}
diff --git a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
index f0c36f4..96307ef 100644
--- a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
@@ -6,6 +6,8 @@ namespace Wkhtmltopdf.NetCore
{
public class LegacyPathProvider : IWkhtmltopdfPathProvider
{
+ internal static LegacyPathProvider Default { get; } = new LegacyPathProvider();
+
public string GetPath()
{
#pragma warning disable 612
diff --git a/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs b/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
index c9d843d..3a9580d 100644
--- a/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
@@ -10,15 +10,17 @@ namespace Wkhtmltopdf.NetCore
{
public static class WkhtmltopdfConfiguration
{
- [Obsolete]
- public static string RotativaPath { get; set; }
+ [Obsolete] public static string RotativaPath { get; set; }
///
/// Setup Rotativa library
///
/// The IServiceCollection object
- /// Optional. Relative path to the directory containing wkhtmltopdf. Default is "Rotativa". Download at https://wkhtmltopdf.org/downloads.html
- public static IServiceCollection AddWkhtmltopdf(this IServiceCollection services, string wkhtmltopdfRelativePath = "Rotativa")
+ /// Optional. Relative path to the directory containing wkhtmltopdf.
+ /// Default is "Rotativa". Download at https://wkhtmltopdf.org/downloads.html
+ [Obsolete]
+ public static IServiceCollection AddWkhtmltopdf(this IServiceCollection services,
+ string wkhtmltopdfRelativePath = "Rotativa")
{
#pragma warning disable 612
RotativaPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, wkhtmltopdfRelativePath);
@@ -29,17 +31,42 @@ public static IServiceCollection AddWkhtmltopdf(this IServiceCollection services
}
#pragma warning restore 612
- return AddCore(services);
+ var fileProvider = new UpdateableFileProvider();
+ services.AddMvc()
+ .AddRazorRuntimeCompilation(options => options.FileProviders.Add(fileProvider))
+ .SetCompatibilityVersion(CompatibilityVersion.Latest);
+ return AddCore(services, fileProvider);
}
- private static IServiceCollection AddCore(IServiceCollection services)
+ ///
+ /// Setup Rotativa library.
+ ///
+ /// The .
+ /// A factory used for creating service instances.
+ ///
+ /// The of the .
+ ///
+ /// The .
+ public static IMvcBuilder AddWkhtmltopdf(
+ this IMvcBuilder builder,
+ Func factory = null,
+ ServiceLifetime lifetime = ServiceLifetime.Singleton
+ ) where T : class, IWkhtmltopdfPathProvider
{
+ builder.Services.TryAdd(factory == null
+ ? new ServiceDescriptor(typeof(IWkhtmltopdfPathProvider), typeof(T), lifetime)
+ : new ServiceDescriptor(typeof(IWkhtmltopdfPathProvider), factory, lifetime));
+
var fileProvider = new UpdateableFileProvider();
- services.TryAddTransient();
+ AddCore(builder.Services, fileProvider);
+ return builder;
+ }
+
+ private static IServiceCollection AddCore(IServiceCollection services, UpdateableFileProvider fileProvider)
+ {
services.TryAddSingleton(fileProvider);
+ services.TryAddTransient();
services.TryAddSingleton();
- services.AddMvc().AddRazorRuntimeCompilation(options => options.FileProviders.Add(fileProvider))
- .SetCompatibilityVersion(CompatibilityVersion.Latest);
services.TryAddTransient();
services.TryAddTransient();
diff --git a/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs b/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
index 6467eef..d32c4d3 100644
--- a/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
@@ -14,7 +14,7 @@ public class GeneratePdf : IGeneratePdf
public GeneratePdf(IRazorViewToStringRenderer engine, IWkhtmltopdfPathProvider pathProvider = null)
{
_engine = engine;
- _pathProvider = pathProvider ?? new LegacyPathProvider();
+ _pathProvider = pathProvider ?? LegacyPathProvider.Default;
_convertOptions = new ConvertOptions();
}
diff --git a/Wkhtmltopdf.NetCore/WkhtmlDriver.cs b/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
index 5975f0d..7beac04 100644
--- a/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
+++ b/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
@@ -15,7 +15,7 @@ public static class WkhtmlDriver
/// Switches that will be passed to wkhtmltopdf binary.
/// String containing HTML code that should be converted to PDF.
/// PDF as byte array.
- [Obsolete()]
+ [Obsolete]
public static byte[] Convert(string wkhtmlPath, string switches, string html)
{
string rotativaLocation;
From 3ba88d59dd8555b78c7a7318d67c5ffdb56c9411 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Wed, 9 Sep 2020 19:13:00 +0300
Subject: [PATCH 16/29] end of files.
---
Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs | 2 +-
.../Configuration/Interfaces/IWkhtmltopdfPathProvider.cs | 2 +-
Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
index a7415e7..14c5d7d 100644
--- a/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
@@ -18,4 +18,4 @@ public AbsolutePathProvider(string path)
public string GetPath() => _path;
}
-}
\ No newline at end of file
+}
diff --git a/Wkhtmltopdf.NetCore/Configuration/Interfaces/IWkhtmltopdfPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/Interfaces/IWkhtmltopdfPathProvider.cs
index 2641d4d..4e10b42 100644
--- a/Wkhtmltopdf.NetCore/Configuration/Interfaces/IWkhtmltopdfPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/Interfaces/IWkhtmltopdfPathProvider.cs
@@ -4,4 +4,4 @@ public interface IWkhtmltopdfPathProvider
{
string GetPath();
}
-}
\ No newline at end of file
+}
diff --git a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
index 96307ef..2d161ef 100644
--- a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
@@ -36,4 +36,4 @@ public string GetPath()
return rotativaLocation;
}
}
-}
\ No newline at end of file
+}
From e6c34b882481de28c079a171cc85ff2986b54d4a Mon Sep 17 00:00:00 2001
From: gurustron
Date: Wed, 9 Sep 2020 21:52:47 +0300
Subject: [PATCH 17/29] Path providers.
---
Wkhtmltopdf.NetCore.Test/SmokeTest.cs | 11 ++---
.../Configuration/LegacyPathProvider.cs | 38 ++++++++++------
.../RotativaPathAsPrefixPathProvider.cs | 43 +++++++++++++++++++
.../Implementation/GeneratePdf.cs | 2 +-
4 files changed, 74 insertions(+), 20 deletions(-)
create mode 100644 Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs
diff --git a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
index 1a8a141..601641b 100644
--- a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
+++ b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
@@ -25,14 +25,14 @@ public void CanConvert()
[Test]
public void CanConvertWithLegacyProvider()
{
- var generatePdf = new GeneratePdf(null, new LegacyPathProvider());
+ var generatePdf = new GeneratePdf(null, new RotativaPathAsPrefixPathProvider());
generatePdf.GetPDF("Hello World
This is html rendered text");
}
[Test]
public void CanConvertWithAbsoluteProvider()
{
- var path = new LegacyPathProvider().GetPath();
+ var path = new RotativaPathAsPrefixPathProvider().GetPath();
var generatePdf = new GeneratePdf(null, new AbsolutePathProvider(path));
generatePdf.GetPDF("Hello World
This is html rendered text");
}
@@ -58,9 +58,10 @@ public void LegacyResolutionWorks()
public void ResolutionWorks()
{
using var host = Host.CreateDefaultBuilder()
- .ConfigureWebHostDefaults(webBuilder => webBuilder
- .ConfigureServices(services =>
- services.AddMvc().AddWkhtmltopdf()))
+ .ConfigureWebHostDefaults(builder => {})
+ .ConfigureServices(services => services
+ .AddMvc()
+ .AddWkhtmltopdf())
.Build();
using var serviceScope = host.Services.CreateScope();
diff --git a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
index 2d161ef..a9fc7d9 100644
--- a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
@@ -4,36 +4,46 @@
namespace Wkhtmltopdf.NetCore
{
+ ///
+ /// Emulates legacy registration behavior.
+ ///
public class LegacyPathProvider : IWkhtmltopdfPathProvider
{
- internal static LegacyPathProvider Default { get; } = new LegacyPathProvider();
+ private readonly string _rotativaLocation;
- public string GetPath()
+ ///
+ /// Constructs path from 's base directory, ,
+ /// OS dependent folder and executable name.
+ ///
+ ///
+ public LegacyPathProvider(string wkhtmltopdfRelativePath = "Rotativa")
{
-#pragma warning disable 612
- var wkhtmlPath = WkhtmltopdfConfiguration.RotativaPath;
-#pragma warning restore 612
+ var wkhtmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, wkhtmltopdfRelativePath);
- string rotativaLocation;
+ if (!Directory.Exists(wkhtmlPath))
+ {
+ throw new Exception("Folder containing wkhtmltopdf not found, searched for " + wkhtmlPath);
+ }
+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
- rotativaLocation = Path.Combine(wkhtmlPath, "Windows", "wkhtmltopdf.exe");
+ _rotativaLocation = Path.Combine(wkhtmlPath, "Windows", "wkhtmltopdf.exe");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
- rotativaLocation = Path.Combine(wkhtmlPath, "Mac", "wkhtmltopdf");
+ _rotativaLocation = Path.Combine(wkhtmlPath, "Mac", "wkhtmltopdf");
}
else
{
- rotativaLocation = Path.Combine(wkhtmlPath, "Linux", "wkhtmltopdf");
+ _rotativaLocation = Path.Combine(wkhtmlPath, "Linux", "wkhtmltopdf");
}
- if (!File.Exists(rotativaLocation))
+ if (!File.Exists(_rotativaLocation))
{
- throw new Exception("wkhtmltopdf not found, searched for " + rotativaLocation);
+ throw new Exception("wkhtmltopdf not found, searched for " + _rotativaLocation);
}
-
- return rotativaLocation;
}
+
+ public string GetPath() => _rotativaLocation;
}
-}
+}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs
new file mode 100644
index 0000000..34554fc
--- /dev/null
+++ b/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs
@@ -0,0 +1,43 @@
+using System;
+using System.IO;
+using System.Runtime.InteropServices;
+
+namespace Wkhtmltopdf.NetCore
+{
+ ///
+ /// Legacy. Uses as prefix.
+ /// Appends OS depended folder and executable name.
+ ///
+ public class RotativaPathAsPrefixPathProvider : IWkhtmltopdfPathProvider
+ {
+ internal static RotativaPathAsPrefixPathProvider Default { get; } = new RotativaPathAsPrefixPathProvider();
+
+ public string GetPath()
+ {
+#pragma warning disable 612
+ var wkhtmlPath = WkhtmltopdfConfiguration.RotativaPath;
+#pragma warning restore 612
+
+ string rotativaLocation;
+ if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+ {
+ rotativaLocation = Path.Combine(wkhtmlPath, "Windows", "wkhtmltopdf.exe");
+ }
+ else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
+ {
+ rotativaLocation = Path.Combine(wkhtmlPath, "Mac", "wkhtmltopdf");
+ }
+ else
+ {
+ rotativaLocation = Path.Combine(wkhtmlPath, "Linux", "wkhtmltopdf");
+ }
+
+ if (!File.Exists(rotativaLocation))
+ {
+ throw new Exception("wkhtmltopdf not found, searched for " + rotativaLocation);
+ }
+
+ return rotativaLocation;
+ }
+ }
+}
diff --git a/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs b/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
index d32c4d3..eec38ef 100644
--- a/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
@@ -14,7 +14,7 @@ public class GeneratePdf : IGeneratePdf
public GeneratePdf(IRazorViewToStringRenderer engine, IWkhtmltopdfPathProvider pathProvider = null)
{
_engine = engine;
- _pathProvider = pathProvider ?? LegacyPathProvider.Default;
+ _pathProvider = pathProvider ?? RotativaPathAsPrefixPathProvider.Default;
_convertOptions = new ConvertOptions();
}
From a9bc218c3e5509a84223f2fddb88d69b4301fa1f Mon Sep 17 00:00:00 2001
From: gurustron
Date: Wed, 9 Sep 2020 21:53:48 +0300
Subject: [PATCH 18/29] Path providers.
---
Wkhtmltopdf.NetCore.Test/SmokeTest.cs | 4 ++--
.../Configuration/RotativaPathAsPrefixPathProvider.cs | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
index 601641b..5956696 100644
--- a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
+++ b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
@@ -25,14 +25,14 @@ public void CanConvert()
[Test]
public void CanConvertWithLegacyProvider()
{
- var generatePdf = new GeneratePdf(null, new RotativaPathAsPrefixPathProvider());
+ var generatePdf = new GeneratePdf(null, new LegacyPathProvider());
generatePdf.GetPDF("Hello World
This is html rendered text");
}
[Test]
public void CanConvertWithAbsoluteProvider()
{
- var path = new RotativaPathAsPrefixPathProvider().GetPath();
+ var path = new LegacyPathProvider().GetPath();
var generatePdf = new GeneratePdf(null, new AbsolutePathProvider(path));
generatePdf.GetPDF("Hello World
This is html rendered text");
}
diff --git a/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs
index 34554fc..9a3c304 100644
--- a/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs
@@ -8,7 +8,7 @@ namespace Wkhtmltopdf.NetCore
/// Legacy. Uses as prefix.
/// Appends OS depended folder and executable name.
///
- public class RotativaPathAsPrefixPathProvider : IWkhtmltopdfPathProvider
+ internal class RotativaPathAsPrefixPathProvider : IWkhtmltopdfPathProvider
{
internal static RotativaPathAsPrefixPathProvider Default { get; } = new RotativaPathAsPrefixPathProvider();
From 793e5c547b175b148d30db282bca79e7bfa4cef0 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Wed, 9 Sep 2020 21:54:30 +0300
Subject: [PATCH 19/29] end of files.
---
Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
index a9fc7d9..f4af89a 100644
--- a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
@@ -46,4 +46,4 @@ public LegacyPathProvider(string wkhtmltopdfRelativePath = "Rotativa")
public string GetPath() => _rotativaLocation;
}
-}
\ No newline at end of file
+}
From 91407f6addc0098634022e75ed7cb5564c14e429 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Fri, 11 Sep 2020 23:30:42 +0300
Subject: [PATCH 20/29] Xml comments.
---
.../Configuration/AbsolutePathProvider.cs | 9 ++--
.../Interfaces/IWkhtmltopdfPathProvider.cs | 6 ++-
.../Configuration/LegacyPathProvider.cs | 15 ++++--
.../RotativaPathAsPrefixPathProvider.cs | 10 ++--
.../Configuration/WkhtmltopdfConfiguration.cs | 51 ++++++++++++++-----
5 files changed, 66 insertions(+), 25 deletions(-)
diff --git a/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
index 14c5d7d..cac3713 100644
--- a/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
@@ -1,14 +1,14 @@
namespace Wkhtmltopdf.NetCore
{
///
- /// Provides absolute path to wkthmltopdf/wkthmltoimage.
+ /// Provides absolute path to wkthmltopdf/wkthmltoimage.
///
public class AbsolutePathProvider : IWkhtmltopdfPathProvider
{
private readonly string _path;
///
- /// Constructs .
+ /// Constructs .
///
/// Absolute path to wkthmltopdf/wkthmltoimage.
public AbsolutePathProvider(string path)
@@ -16,6 +16,9 @@ public AbsolutePathProvider(string path)
_path = path;
}
+ /**
+ *
+ */
public string GetPath() => _path;
}
-}
+}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore/Configuration/Interfaces/IWkhtmltopdfPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/Interfaces/IWkhtmltopdfPathProvider.cs
index 4e10b42..fbc50ff 100644
--- a/Wkhtmltopdf.NetCore/Configuration/Interfaces/IWkhtmltopdfPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/Interfaces/IWkhtmltopdfPathProvider.cs
@@ -2,6 +2,10 @@
{
public interface IWkhtmltopdfPathProvider
{
+ ///
+ /// Gets path to executable.
+ ///
+ /// Path to executable.
string GetPath();
}
-}
+}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
index f4af89a..8a969db 100644
--- a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
@@ -5,15 +5,17 @@
namespace Wkhtmltopdf.NetCore
{
///
- /// Emulates legacy registration behavior.
+ /// Emulates legacy registration behavior.
///
public class LegacyPathProvider : IWkhtmltopdfPathProvider
{
private readonly string _rotativaLocation;
///
- /// Constructs path from 's base directory, ,
- /// OS dependent folder and executable name.
+ /// Constructs path from 's base directory,
+ /// ,
+ ///
+ /// OS dependent folder and executable name.
///
///
public LegacyPathProvider(string wkhtmltopdfRelativePath = "Rotativa")
@@ -24,7 +26,7 @@ public LegacyPathProvider(string wkhtmltopdfRelativePath = "Rotativa")
{
throw new Exception("Folder containing wkhtmltopdf not found, searched for " + wkhtmlPath);
}
-
+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
_rotativaLocation = Path.Combine(wkhtmlPath, "Windows", "wkhtmltopdf.exe");
@@ -44,6 +46,9 @@ public LegacyPathProvider(string wkhtmltopdfRelativePath = "Rotativa")
}
}
+ /**
+ *
+ */
public string GetPath() => _rotativaLocation;
}
-}
+}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs
index 9a3c304..af4d3d7 100644
--- a/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs
@@ -5,13 +5,17 @@
namespace Wkhtmltopdf.NetCore
{
///
- /// Legacy. Uses as prefix.
- /// Appends OS depended folder and executable name.
+ /// Legacy. Uses as prefix.
+ ///
+ /// Appends OS depended folder and executable name.
///
internal class RotativaPathAsPrefixPathProvider : IWkhtmltopdfPathProvider
{
internal static RotativaPathAsPrefixPathProvider Default { get; } = new RotativaPathAsPrefixPathProvider();
+ /**
+ *
+ */
public string GetPath()
{
#pragma warning disable 612
@@ -40,4 +44,4 @@ public string GetPath()
return rotativaLocation;
}
}
-}
+}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs b/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
index 3a9580d..969d82d 100644
--- a/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
@@ -1,10 +1,10 @@
-using Microsoft.AspNetCore.Mvc;
+using System;
+using System.IO;
+using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
-using System;
-using System.IO;
namespace Wkhtmltopdf.NetCore
{
@@ -13,11 +13,13 @@ public static class WkhtmltopdfConfiguration
[Obsolete] public static string RotativaPath { get; set; }
///
- /// Setup Rotativa library
+ /// Setup Rotativa library
///
/// The IServiceCollection object
- /// Optional. Relative path to the directory containing wkhtmltopdf.
- /// Default is "Rotativa". Download at https://wkhtmltopdf.org/downloads.html
+ ///
+ /// Optional. Relative path to the directory containing wkhtmltopdf.
+ /// Default is "Rotativa". Download at https://wkhtmltopdf.org/downloads.html
+ ///
[Obsolete]
public static IServiceCollection AddWkhtmltopdf(this IServiceCollection services,
string wkhtmltopdfRelativePath = "Rotativa")
@@ -26,9 +28,7 @@ public static IServiceCollection AddWkhtmltopdf(this IServiceCollection services
RotativaPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, wkhtmltopdfRelativePath);
if (!Directory.Exists(RotativaPath))
- {
throw new Exception("Folder containing wkhtmltopdf not found, searched for " + RotativaPath);
- }
#pragma warning restore 612
var fileProvider = new UpdateableFileProvider();
@@ -39,19 +39,44 @@ public static IServiceCollection AddWkhtmltopdf(this IServiceCollection services
}
///
- /// Setup Rotativa library.
+ /// Setup Rotativa library.
+ ///
+ /// The .
+ ///
+ /// The of the .
+ ///
+ /// The .
+ public static IMvcBuilder AddWkhtmltopdf(
+ this IMvcBuilder builder,
+ ServiceLifetime lifetime = ServiceLifetime.Singleton
+ ) where T : class, IWkhtmltopdfPathProvider
+ {
+ return AddWkhtmltopdfInternal(builder, null, lifetime);
+ }
+
+ ///
+ /// Setup Rotativa library.
///
/// The .
/// A factory used for creating service instances.
///
- /// The of the .
+ /// The of the .
///
- /// The .
+ /// The .
public static IMvcBuilder AddWkhtmltopdf(
this IMvcBuilder builder,
- Func factory = null,
+ Func factory,
ServiceLifetime lifetime = ServiceLifetime.Singleton
) where T : class, IWkhtmltopdfPathProvider
+ {
+ return AddWkhtmltopdfInternal(builder, factory, lifetime);
+ }
+
+ private static IMvcBuilder AddWkhtmltopdfInternal(
+ IMvcBuilder builder,
+ Func factory,
+ ServiceLifetime lifetime)
+ where T : class, IWkhtmltopdfPathProvider
{
builder.Services.TryAdd(factory == null
? new ServiceDescriptor(typeof(IWkhtmltopdfPathProvider), typeof(T), lifetime)
@@ -73,4 +98,4 @@ private static IServiceCollection AddCore(IServiceCollection services, Updateabl
return services;
}
}
-}
+}
\ No newline at end of file
From b959402df672d016dce2fa4a2c0eb28e68958f31 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Fri, 11 Sep 2020 23:45:51 +0300
Subject: [PATCH 21/29] Add exception
---
Wkhtmltopdf.NetCore.Test/SmokeTest.cs | 14 +++-
.../Configuration/AbsolutePathProvider.cs | 6 +-
.../Configuration/LegacyPathProvider.cs | 4 +-
.../RotativaPathAsPrefixPathProvider.cs | 4 +-
.../Implementation/ConvertOptions.cs | 1 +
.../Interfaces/IConvertOptions.cs | 4 ++
Wkhtmltopdf.NetCore/WkhtmlDriver.cs | 66 ++++++++++---------
Wkhtmltopdf.NetCore/WkhtmlDriverException.cs | 12 ++++
8 files changed, 69 insertions(+), 42 deletions(-)
create mode 100644 Wkhtmltopdf.NetCore/WkhtmlDriverException.cs
diff --git a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
index 5956696..f21244e 100644
--- a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
+++ b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
@@ -58,7 +58,7 @@ public void LegacyResolutionWorks()
public void ResolutionWorks()
{
using var host = Host.CreateDefaultBuilder()
- .ConfigureWebHostDefaults(builder => {})
+ .ConfigureWebHostDefaults(builder => { })
.ConfigureServices(services => services
.AddMvc()
.AddWkhtmltopdf())
@@ -68,5 +68,15 @@ public void ResolutionWorks()
var generatePdf = serviceScope.ServiceProvider.GetService();
generatePdf.GetPDF("Hello World
This is html rendered text");
}
+
+ [Test]
+ public void ThrowsForMissingExecutable()
+ {
+ var path = "not_valid_path";
+ var generatePdf = new GeneratePdf(null, new AbsolutePathProvider(path));
+
+ var ex = Assert.Throws(() => generatePdf.GetPDF(""));
+ StringAssert.Contains(path, ex.Message);
+ }
}
-}
+}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
index cac3713..64d3eac 100644
--- a/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
@@ -11,14 +11,12 @@ public class AbsolutePathProvider : IWkhtmltopdfPathProvider
/// Constructs .
///
/// Absolute path to wkthmltopdf/wkthmltoimage.
- public AbsolutePathProvider(string path)
+ public AbsolutePathProvider(string path = "wkhtmltopdf")
{
_path = path;
}
- /**
- *
- */
+ /* */
public string GetPath() => _path;
}
}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
index 8a969db..26c43c5 100644
--- a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
@@ -46,9 +46,7 @@ public LegacyPathProvider(string wkhtmltopdfRelativePath = "Rotativa")
}
}
- /**
- *
- */
+ /* */
public string GetPath() => _rotativaLocation;
}
}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs
index af4d3d7..58b6271 100644
--- a/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs
@@ -13,9 +13,7 @@ internal class RotativaPathAsPrefixPathProvider : IWkhtmltopdfPathProvider
{
internal static RotativaPathAsPrefixPathProvider Default { get; } = new RotativaPathAsPrefixPathProvider();
- /**
- *
- */
+ /* */
public string GetPath()
{
#pragma warning disable 612
diff --git a/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs b/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
index 694c84c..43136de 100644
--- a/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
@@ -105,6 +105,7 @@ protected string GetContentType()
[OptionFlag("--replace")]
public Dictionary Replacements { get; set; }
+ /* */
public string GetConvertOptions()
{
var result = new StringBuilder();
diff --git a/Wkhtmltopdf.NetCore/Implementation/Interfaces/IConvertOptions.cs b/Wkhtmltopdf.NetCore/Implementation/Interfaces/IConvertOptions.cs
index 51ce2ce..5ff84e3 100644
--- a/Wkhtmltopdf.NetCore/Implementation/Interfaces/IConvertOptions.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/Interfaces/IConvertOptions.cs
@@ -2,6 +2,10 @@
{
public interface IConvertOptions
{
+ ///
+ /// Builds conversion options.
+ ///
+ /// Options for executable.
public string GetConvertOptions();
}
}
\ No newline at end of file
diff --git a/Wkhtmltopdf.NetCore/WkhtmlDriver.cs b/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
index 7beac04..248bb91 100644
--- a/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
+++ b/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
@@ -63,55 +63,61 @@ public static byte[] Convert(IWkhtmltopdfPathProvider pathProvider, string switc
html = SpecialCharsEncode(html);
}
- using (var proc = new Process())
+ var wkhtmlPath = pathProvider.GetPath();
+ using var proc = new Process
{
- proc.StartInfo = new ProcessStartInfo
+ StartInfo = new ProcessStartInfo
{
- FileName = pathProvider.GetPath(),
+ FileName = wkhtmlPath,
Arguments = switches,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
CreateNoWindow = true
- };
+ }
+ };
+ try
+ {
proc.Start();
+ }
+ catch (Exception e)
+ {
+ throw new WkhtmlDriverException($"Failed to start wkhtmltodpf at path {wkhtmlPath}.", e);
+ }
- // generate PDF from given HTML string, not from URL
- if (!string.IsNullOrEmpty(html))
+ // generate PDF from given HTML string, not from URL
+ if (!string.IsNullOrEmpty(html))
+ {
+ using (var sIn = proc.StandardInput)
{
- using (var sIn = proc.StandardInput)
- {
- sIn.WriteLine(html);
- }
+ sIn.WriteLine(html);
}
+ }
- using (var ms = new MemoryStream())
- {
- using (var sOut = proc.StandardOutput.BaseStream)
- {
- byte[] buffer = new byte[4096];
- int read;
+ using var ms = new MemoryStream();
+ using (var sOut = proc.StandardOutput.BaseStream)
+ {
+ byte[] buffer = new byte[4096];
+ int read;
- while ((read = sOut.Read(buffer, 0, buffer.Length)) > 0)
- {
- ms.Write(buffer, 0, read);
- }
- }
+ while ((read = sOut.Read(buffer, 0, buffer.Length)) > 0)
+ {
+ ms.Write(buffer, 0, read);
+ }
+ }
- string error = proc.StandardError.ReadToEnd();
+ string error = proc.StandardError.ReadToEnd();
- if (ms.Length == 0)
- {
- throw new Exception(error);
- }
+ if (ms.Length == 0)
+ {
+ throw new Exception(error);
+ }
- proc.WaitForExit();
+ proc.WaitForExit();
- return ms.ToArray();
- }
- }
+ return ms.ToArray();
}
///
diff --git a/Wkhtmltopdf.NetCore/WkhtmlDriverException.cs b/Wkhtmltopdf.NetCore/WkhtmlDriverException.cs
new file mode 100644
index 0000000..98305fe
--- /dev/null
+++ b/Wkhtmltopdf.NetCore/WkhtmlDriverException.cs
@@ -0,0 +1,12 @@
+using System;
+
+namespace Wkhtmltopdf.NetCore
+{
+ [Serializable]
+ public class WkhtmlDriverException : Exception
+ {
+ public WkhtmlDriverException(string message, Exception e) : base(message, e)
+ {
+ }
+ }
+}
\ No newline at end of file
From eb51e360f9f94eb4944374c0aa34ace2757bad5f Mon Sep 17 00:00:00 2001
From: gurustron
Date: Fri, 11 Sep 2020 23:46:47 +0300
Subject: [PATCH 22/29] small comment change.
---
Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
index 26c43c5..13e3a74 100644
--- a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
@@ -12,8 +12,7 @@ public class LegacyPathProvider : IWkhtmltopdfPathProvider
private readonly string _rotativaLocation;
///
- /// Constructs path from 's base directory,
- /// ,
+ /// Constructs path from 's base directory, ,
///
/// OS dependent folder and executable name.
///
From 30127a7f9854995bc92bc61ab8530c202612a932 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Fri, 11 Sep 2020 23:51:36 +0300
Subject: [PATCH 23/29] end of files.
---
Wkhtmltopdf.NetCore.Test/SmokeTest.cs | 2 +-
Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs | 2 +-
.../Configuration/Interfaces/IWkhtmltopdfPathProvider.cs | 2 +-
Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs | 2 +-
.../Configuration/RotativaPathAsPrefixPathProvider.cs | 2 +-
Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs | 2 +-
.../Implementation/Interfaces/IConvertOptions.cs | 2 +-
Wkhtmltopdf.NetCore/WkhtmlDriverException.cs | 2 +-
8 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
index f21244e..1106b30 100644
--- a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
+++ b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
@@ -79,4 +79,4 @@ public void ThrowsForMissingExecutable()
StringAssert.Contains(path, ex.Message);
}
}
-}
\ No newline at end of file
+}
diff --git a/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
index 64d3eac..09117e3 100644
--- a/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
@@ -19,4 +19,4 @@ public AbsolutePathProvider(string path = "wkhtmltopdf")
/* */
public string GetPath() => _path;
}
-}
\ No newline at end of file
+}
diff --git a/Wkhtmltopdf.NetCore/Configuration/Interfaces/IWkhtmltopdfPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/Interfaces/IWkhtmltopdfPathProvider.cs
index fbc50ff..6316f0c 100644
--- a/Wkhtmltopdf.NetCore/Configuration/Interfaces/IWkhtmltopdfPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/Interfaces/IWkhtmltopdfPathProvider.cs
@@ -8,4 +8,4 @@ public interface IWkhtmltopdfPathProvider
/// Path to executable.
string GetPath();
}
-}
\ No newline at end of file
+}
diff --git a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
index 13e3a74..d38b109 100644
--- a/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/LegacyPathProvider.cs
@@ -48,4 +48,4 @@ public LegacyPathProvider(string wkhtmltopdfRelativePath = "Rotativa")
/* */
public string GetPath() => _rotativaLocation;
}
-}
\ No newline at end of file
+}
diff --git a/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs
index 58b6271..cf54d72 100644
--- a/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/RotativaPathAsPrefixPathProvider.cs
@@ -42,4 +42,4 @@ public string GetPath()
return rotativaLocation;
}
}
-}
\ No newline at end of file
+}
diff --git a/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs b/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
index 969d82d..173eab0 100644
--- a/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
@@ -98,4 +98,4 @@ private static IServiceCollection AddCore(IServiceCollection services, Updateabl
return services;
}
}
-}
\ No newline at end of file
+}
diff --git a/Wkhtmltopdf.NetCore/Implementation/Interfaces/IConvertOptions.cs b/Wkhtmltopdf.NetCore/Implementation/Interfaces/IConvertOptions.cs
index 5ff84e3..7b2aa15 100644
--- a/Wkhtmltopdf.NetCore/Implementation/Interfaces/IConvertOptions.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/Interfaces/IConvertOptions.cs
@@ -8,4 +8,4 @@ public interface IConvertOptions
/// Options for executable.
public string GetConvertOptions();
}
-}
\ No newline at end of file
+}
diff --git a/Wkhtmltopdf.NetCore/WkhtmlDriverException.cs b/Wkhtmltopdf.NetCore/WkhtmlDriverException.cs
index 98305fe..a5b218f 100644
--- a/Wkhtmltopdf.NetCore/WkhtmlDriverException.cs
+++ b/Wkhtmltopdf.NetCore/WkhtmlDriverException.cs
@@ -9,4 +9,4 @@ public WkhtmlDriverException(string message, Exception e) : base(message, e)
{
}
}
-}
\ No newline at end of file
+}
From dd6ee51120ed119a0addd24848520b5294b656e3 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Fri, 11 Sep 2020 23:58:29 +0300
Subject: [PATCH 24/29] rename.
---
Wkhtmltopdf.NetCore.Test/SmokeTest.cs | 4 ++--
.../{AbsolutePathProvider.cs => FixedPathProvider.cs} | 8 ++++----
Wkhtmltopdf.NetCore/WkhtmlDriver.cs | 2 +-
3 files changed, 7 insertions(+), 7 deletions(-)
rename Wkhtmltopdf.NetCore/Configuration/{AbsolutePathProvider.cs => FixedPathProvider.cs} (55%)
diff --git a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
index 1106b30..00f8283 100644
--- a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
+++ b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
@@ -33,7 +33,7 @@ public void CanConvertWithLegacyProvider()
public void CanConvertWithAbsoluteProvider()
{
var path = new LegacyPathProvider().GetPath();
- var generatePdf = new GeneratePdf(null, new AbsolutePathProvider(path));
+ var generatePdf = new GeneratePdf(null, new FixedPathProvider(path));
generatePdf.GetPDF("Hello World
This is html rendered text");
}
@@ -73,7 +73,7 @@ public void ResolutionWorks()
public void ThrowsForMissingExecutable()
{
var path = "not_valid_path";
- var generatePdf = new GeneratePdf(null, new AbsolutePathProvider(path));
+ var generatePdf = new GeneratePdf(null, new FixedPathProvider(path));
var ex = Assert.Throws(() => generatePdf.GetPDF(""));
StringAssert.Contains(path, ex.Message);
diff --git a/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/FixedPathProvider.cs
similarity index 55%
rename from Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
rename to Wkhtmltopdf.NetCore/Configuration/FixedPathProvider.cs
index 09117e3..cef44dc 100644
--- a/Wkhtmltopdf.NetCore/Configuration/AbsolutePathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/FixedPathProvider.cs
@@ -3,15 +3,15 @@
///
/// Provides absolute path to wkthmltopdf/wkthmltoimage.
///
- public class AbsolutePathProvider : IWkhtmltopdfPathProvider
+ public class FixedPathProvider : IWkhtmltopdfPathProvider
{
private readonly string _path;
///
- /// Constructs .
+ /// Constructs . Uses provided path unchanged.
///
- /// Absolute path to wkthmltopdf/wkthmltoimage.
- public AbsolutePathProvider(string path = "wkhtmltopdf")
+ /// Path to wkthmltopdf/wkthmltoimage.
+ public FixedPathProvider(string path = "wkhtmltopdf")
{
_path = path;
}
diff --git a/Wkhtmltopdf.NetCore/WkhtmlDriver.cs b/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
index 248bb91..b4fb2f9 100644
--- a/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
+++ b/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
@@ -38,7 +38,7 @@ public static byte[] Convert(string wkhtmlPath, string switches, string html)
throw new Exception("wkhtmltopdf not found, searched for " + rotativaLocation);
}
- return Convert(new AbsolutePathProvider(rotativaLocation), switches, html);
+ return Convert(new FixedPathProvider(rotativaLocation), switches, html);
}
///
From e51c9f39a2a77d6811d958511507931a8de0109f Mon Sep 17 00:00:00 2001
From: gurustron
Date: Fri, 11 Sep 2020 23:59:56 +0300
Subject: [PATCH 25/29] rename.
---
Wkhtmltopdf.NetCore.Test/SmokeTest.cs | 4 ++--
.../{FixedPathProvider.cs => FullPathProvider.cs} | 6 +++---
Wkhtmltopdf.NetCore/WkhtmlDriver.cs | 2 +-
3 files changed, 6 insertions(+), 6 deletions(-)
rename Wkhtmltopdf.NetCore/Configuration/{FixedPathProvider.cs => FullPathProvider.cs} (66%)
diff --git a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
index 00f8283..d4b58b3 100644
--- a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
+++ b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
@@ -33,7 +33,7 @@ public void CanConvertWithLegacyProvider()
public void CanConvertWithAbsoluteProvider()
{
var path = new LegacyPathProvider().GetPath();
- var generatePdf = new GeneratePdf(null, new FixedPathProvider(path));
+ var generatePdf = new GeneratePdf(null, new FullPathProvider(path));
generatePdf.GetPDF("Hello World
This is html rendered text");
}
@@ -73,7 +73,7 @@ public void ResolutionWorks()
public void ThrowsForMissingExecutable()
{
var path = "not_valid_path";
- var generatePdf = new GeneratePdf(null, new FixedPathProvider(path));
+ var generatePdf = new GeneratePdf(null, new FullPathProvider(path));
var ex = Assert.Throws(() => generatePdf.GetPDF(""));
StringAssert.Contains(path, ex.Message);
diff --git a/Wkhtmltopdf.NetCore/Configuration/FixedPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/FullPathProvider.cs
similarity index 66%
rename from Wkhtmltopdf.NetCore/Configuration/FixedPathProvider.cs
rename to Wkhtmltopdf.NetCore/Configuration/FullPathProvider.cs
index cef44dc..427fc67 100644
--- a/Wkhtmltopdf.NetCore/Configuration/FixedPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/FullPathProvider.cs
@@ -3,15 +3,15 @@
///
/// Provides absolute path to wkthmltopdf/wkthmltoimage.
///
- public class FixedPathProvider : IWkhtmltopdfPathProvider
+ public class FullPathProvider : IWkhtmltopdfPathProvider
{
private readonly string _path;
///
- /// Constructs . Uses provided path unchanged.
+ /// Constructs . Uses provided path as is.
///
/// Path to wkthmltopdf/wkthmltoimage.
- public FixedPathProvider(string path = "wkhtmltopdf")
+ public FullPathProvider(string path = "wkhtmltopdf")
{
_path = path;
}
diff --git a/Wkhtmltopdf.NetCore/WkhtmlDriver.cs b/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
index b4fb2f9..1d33e9a 100644
--- a/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
+++ b/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
@@ -38,7 +38,7 @@ public static byte[] Convert(string wkhtmlPath, string switches, string html)
throw new Exception("wkhtmltopdf not found, searched for " + rotativaLocation);
}
- return Convert(new FixedPathProvider(rotativaLocation), switches, html);
+ return Convert(new FullPathProvider(rotativaLocation), switches, html);
}
///
From 23586f418304fa20b03cbe5dd2f35514a3d95792 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Sat, 12 Sep 2020 00:19:35 +0300
Subject: [PATCH 26/29] Improve test
---
Wkhtmltopdf.NetCore.Test/SmokeTest.cs | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
index d4b58b3..bb13ae7 100644
--- a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
+++ b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
@@ -8,13 +8,21 @@ namespace Wkhtmltopdf.NetCore.Test
public class SmokeTest
{
[SetUp]
- public void Setup()
+ public void SetRotativaPath()
{
#pragma warning disable 612
WkhtmltopdfConfiguration.RotativaPath = AppDomain.CurrentDomain.BaseDirectory + "Rotativa";
#pragma warning restore 612
}
+ [TearDown]
+ public void ClearRotativaPath()
+ {
+#pragma warning disable 612
+ WkhtmltopdfConfiguration.RotativaPath = null;
+#pragma warning restore 612
+ }
+
[Test]
public void CanConvert()
{
@@ -40,6 +48,7 @@ public void CanConvertWithAbsoluteProvider()
[Test]
public void LegacyResolutionWorks()
{
+ ClearRotativaPath();
using var host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(webBuilder =>
{
@@ -57,6 +66,7 @@ public void LegacyResolutionWorks()
[Test]
public void ResolutionWorks()
{
+ ClearRotativaPath();
using var host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => { })
.ConfigureServices(services => services
From fd717e01d366dadc86da15b141ba177e06395b73 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Sat, 12 Sep 2020 00:37:46 +0300
Subject: [PATCH 27/29] Introduced IWkhtmlDriver.
---
Wkhtmltopdf.NetCore.Test/SmokeTest.cs | 6 +++---
.../Configuration/WkhtmltopdfConfiguration.cs | 4 ++--
.../Implementation/GeneratePdf.cs | 18 +++++++++++-------
.../Implementation/Interfaces/IWkhtmlDriver.cs | 15 +++++++++++++++
.../{ => Implementation}/WkhtmlDriver.cs | 15 +++++++++++++--
5 files changed, 44 insertions(+), 14 deletions(-)
create mode 100644 Wkhtmltopdf.NetCore/Implementation/Interfaces/IWkhtmlDriver.cs
rename Wkhtmltopdf.NetCore/{ => Implementation}/WkhtmlDriver.cs (88%)
diff --git a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
index bb13ae7..367c889 100644
--- a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
+++ b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
@@ -33,7 +33,7 @@ public void CanConvert()
[Test]
public void CanConvertWithLegacyProvider()
{
- var generatePdf = new GeneratePdf(null, new LegacyPathProvider());
+ var generatePdf = new GeneratePdf(null, new WkhtmlDriver(new LegacyPathProvider()));
generatePdf.GetPDF("Hello World
This is html rendered text");
}
@@ -41,7 +41,7 @@ public void CanConvertWithLegacyProvider()
public void CanConvertWithAbsoluteProvider()
{
var path = new LegacyPathProvider().GetPath();
- var generatePdf = new GeneratePdf(null, new FullPathProvider(path));
+ var generatePdf = new GeneratePdf(null, new WkhtmlDriver(new FullPathProvider(path)));
generatePdf.GetPDF("Hello World
This is html rendered text");
}
@@ -83,7 +83,7 @@ public void ResolutionWorks()
public void ThrowsForMissingExecutable()
{
var path = "not_valid_path";
- var generatePdf = new GeneratePdf(null, new FullPathProvider(path));
+ var generatePdf = new GeneratePdf(null, new WkhtmlDriver(new FullPathProvider(path)));
var ex = Assert.Throws(() => generatePdf.GetPDF(""));
StringAssert.Contains(path, ex.Message);
diff --git a/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs b/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
index 173eab0..7700eca 100644
--- a/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/WkhtmltopdfConfiguration.cs
@@ -82,8 +82,7 @@ private static IMvcBuilder AddWkhtmltopdfInternal(
? new ServiceDescriptor(typeof(IWkhtmltopdfPathProvider), typeof(T), lifetime)
: new ServiceDescriptor(typeof(IWkhtmltopdfPathProvider), factory, lifetime));
- var fileProvider = new UpdateableFileProvider();
- AddCore(builder.Services, fileProvider);
+ AddCore(builder.Services, new UpdateableFileProvider());
return builder;
}
@@ -93,6 +92,7 @@ private static IServiceCollection AddCore(IServiceCollection services, Updateabl
services.TryAddTransient();
services.TryAddSingleton();
services.TryAddTransient();
+ services.TryAddTransient();
services.TryAddTransient();
return services;
diff --git a/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs b/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
index eec38ef..cb4d79e 100644
--- a/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/GeneratePdf.cs
@@ -8,13 +8,20 @@ namespace Wkhtmltopdf.NetCore
public class GeneratePdf : IGeneratePdf
{
private readonly IRazorViewToStringRenderer _engine;
- private readonly IWkhtmltopdfPathProvider _pathProvider;
+ private readonly IWkhtmlDriver _wkhtmlDriver;
private IConvertOptions _convertOptions;
- public GeneratePdf(IRazorViewToStringRenderer engine, IWkhtmltopdfPathProvider pathProvider = null)
+ ///
+ /// Initializes new instance of .
+ ///
+ /// .
+ /// .
+ public GeneratePdf(
+ IRazorViewToStringRenderer engine,
+ IWkhtmlDriver wkhtmlDriver = null)
{
_engine = engine;
- _pathProvider = pathProvider ?? RotativaPathAsPrefixPathProvider.Default;
+ _wkhtmlDriver = wkhtmlDriver ?? new WkhtmlDriver();
_convertOptions = new ConvertOptions();
}
@@ -23,10 +30,7 @@ public void SetConvertOptions(IConvertOptions convertOptions)
_convertOptions = convertOptions;
}
- public byte[] GetPDF(string html) => WkhtmlDriver.Convert(
- _pathProvider,
- _convertOptions.GetConvertOptions(),
- html);
+ public byte[] GetPDF(string html) => _wkhtmlDriver.Convert(_convertOptions, html);
public async Task GetByteArray(string view, T model)
{
diff --git a/Wkhtmltopdf.NetCore/Implementation/Interfaces/IWkhtmlDriver.cs b/Wkhtmltopdf.NetCore/Implementation/Interfaces/IWkhtmlDriver.cs
new file mode 100644
index 0000000..e62329c
--- /dev/null
+++ b/Wkhtmltopdf.NetCore/Implementation/Interfaces/IWkhtmlDriver.cs
@@ -0,0 +1,15 @@
+using Wkhtmltopdf.NetCore.Interfaces;
+
+namespace Wkhtmltopdf.NetCore
+{
+ public interface IWkhtmlDriver
+ {
+ ///
+ /// Converts given URL or HTML string to PDF.
+ ///
+ /// that will be passed to wkhtmltopdf binary.
+ /// String containing HTML code that should be converted to PDF.
+ /// PDF as byte array.
+ byte[] Convert(IConvertOptions options, string html);
+ }
+}
diff --git a/Wkhtmltopdf.NetCore/WkhtmlDriver.cs b/Wkhtmltopdf.NetCore/Implementation/WkhtmlDriver.cs
similarity index 88%
rename from Wkhtmltopdf.NetCore/WkhtmlDriver.cs
rename to Wkhtmltopdf.NetCore/Implementation/WkhtmlDriver.cs
index 1d33e9a..abbaf79 100644
--- a/Wkhtmltopdf.NetCore/WkhtmlDriver.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/WkhtmlDriver.cs
@@ -3,11 +3,22 @@
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
+using Wkhtmltopdf.NetCore.Interfaces;
namespace Wkhtmltopdf.NetCore
{
- public static class WkhtmlDriver
+ public class WkhtmlDriver : IWkhtmlDriver
{
+ private readonly IWkhtmltopdfPathProvider _pathProvider;
+
+ public WkhtmlDriver(IWkhtmltopdfPathProvider pathProvider = null)
+ {
+ _pathProvider = pathProvider ?? RotativaPathAsPrefixPathProvider.Default;
+ }
+
+ /* */
+ public byte[] Convert(IConvertOptions options, string html) => Convert(_pathProvider, options.GetConvertOptions(), html);
+
///
/// Converts given URL or HTML string to PDF.
///
@@ -48,7 +59,7 @@ public static byte[] Convert(string wkhtmlPath, string switches, string html)
/// Switches that will be passed to wkhtmltopdf binary.
/// String containing HTML code that should be converted to PDF.
/// PDF as byte array.
- public static byte[] Convert(IWkhtmltopdfPathProvider pathProvider, string switches, string html)
+ private static byte[] Convert(IWkhtmltopdfPathProvider pathProvider, string switches, string html)
{
// switches:
// "-q" - silent output, only errors - no progress messages
From 4024f60b0f87f82615405ae18adb8138da4a2fa4 Mon Sep 17 00:00:00 2001
From: gurustron
Date: Sat, 12 Sep 2020 11:04:41 +0300
Subject: [PATCH 28/29] rename to ExactPathProvider
---
Wkhtmltopdf.NetCore.Test/SmokeTest.cs | 7 +++++--
.../{FullPathProvider.cs => ExactPathProvider.cs} | 8 ++++----
Wkhtmltopdf.NetCore/Implementation/WkhtmlDriver.cs | 2 +-
3 files changed, 10 insertions(+), 7 deletions(-)
rename Wkhtmltopdf.NetCore/Configuration/{FullPathProvider.cs => ExactPathProvider.cs} (54%)
diff --git a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
index 367c889..c75c45e 100644
--- a/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
+++ b/Wkhtmltopdf.NetCore.Test/SmokeTest.cs
@@ -5,6 +5,9 @@
namespace Wkhtmltopdf.NetCore.Test
{
+ ///
+ /// Some smoke tests including legacy API.
+ ///
public class SmokeTest
{
[SetUp]
@@ -41,7 +44,7 @@ public void CanConvertWithLegacyProvider()
public void CanConvertWithAbsoluteProvider()
{
var path = new LegacyPathProvider().GetPath();
- var generatePdf = new GeneratePdf(null, new WkhtmlDriver(new FullPathProvider(path)));
+ var generatePdf = new GeneratePdf(null, new WkhtmlDriver(new ExactPathProvider(path)));
generatePdf.GetPDF("Hello World
This is html rendered text");
}
@@ -83,7 +86,7 @@ public void ResolutionWorks()
public void ThrowsForMissingExecutable()
{
var path = "not_valid_path";
- var generatePdf = new GeneratePdf(null, new WkhtmlDriver(new FullPathProvider(path)));
+ var generatePdf = new GeneratePdf(null, new WkhtmlDriver(new ExactPathProvider(path)));
var ex = Assert.Throws(() => generatePdf.GetPDF(""));
StringAssert.Contains(path, ex.Message);
diff --git a/Wkhtmltopdf.NetCore/Configuration/FullPathProvider.cs b/Wkhtmltopdf.NetCore/Configuration/ExactPathProvider.cs
similarity index 54%
rename from Wkhtmltopdf.NetCore/Configuration/FullPathProvider.cs
rename to Wkhtmltopdf.NetCore/Configuration/ExactPathProvider.cs
index 427fc67..b658c10 100644
--- a/Wkhtmltopdf.NetCore/Configuration/FullPathProvider.cs
+++ b/Wkhtmltopdf.NetCore/Configuration/ExactPathProvider.cs
@@ -1,17 +1,17 @@
namespace Wkhtmltopdf.NetCore
{
///
- /// Provides absolute path to wkthmltopdf/wkthmltoimage.
+ /// Provides exact specified path to wkthmltopdf/wkthmltoimage.
///
- public class FullPathProvider : IWkhtmltopdfPathProvider
+ public class ExactPathProvider : IWkhtmltopdfPathProvider
{
private readonly string _path;
///
- /// Constructs . Uses provided path as is.
+ /// Constructs new instance of . Uses provided path as is.
///
/// Path to wkthmltopdf/wkthmltoimage.
- public FullPathProvider(string path = "wkhtmltopdf")
+ public ExactPathProvider(string path = "wkhtmltopdf")
{
_path = path;
}
diff --git a/Wkhtmltopdf.NetCore/Implementation/WkhtmlDriver.cs b/Wkhtmltopdf.NetCore/Implementation/WkhtmlDriver.cs
index abbaf79..48f6854 100644
--- a/Wkhtmltopdf.NetCore/Implementation/WkhtmlDriver.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/WkhtmlDriver.cs
@@ -49,7 +49,7 @@ public static byte[] Convert(string wkhtmlPath, string switches, string html)
throw new Exception("wkhtmltopdf not found, searched for " + rotativaLocation);
}
- return Convert(new FullPathProvider(rotativaLocation), switches, html);
+ return Convert(new ExactPathProvider(rotativaLocation), switches, html);
}
///
From 4b03c51617cae5b1121a2c072cd943b4b64ce384 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C5=82awomir=20Bry=C5=9B?=
Date: Fri, 25 Jun 2021 08:35:31 +0200
Subject: [PATCH 29/29] Enable local file access for wkhtmltopdf 0.12.6
---
Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs b/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
index 43136de..39fd3ab 100644
--- a/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
+++ b/Wkhtmltopdf.NetCore/Implementation/ConvertOptions.cs
@@ -98,6 +98,12 @@ protected string GetContentType()
[OptionFlag("--footer-spacing")]
public int? FooterSpacing { get; set; }
+ ///
+ /// Enables access to local files.
+ ///
+ [OptionFlag("--enable-local-file-access")]
+ public bool EnableLocalFileAccess { get; set; }
+
///
/// Sets the variables to replace in the header and footer html
///