Skip to content

Commit 8fb7ec4

Browse files
authored
Added overloads of ConvertHtmlToPng and ConvertHtmlToPdf
- Exposed PdfOptions in ConvertHtmlToPdf overload - Exposed ScreenshotOptions in ConvertHtmlToPng overload
2 parents 6300442 + f3efb4b commit 8fb7ec4

11 files changed

+440
-11
lines changed

Codeuctivity.HtmlRenderer/Codeuctivity.HtmlRenderer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<ItemGroup>
4141
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
4242
<PackageReference Include="PuppeteerSharp" Version="8.0.0" />
43-
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.49.0.57237">
43+
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.50.0.58025">
4444
<PrivateAssets>all</PrivateAssets>
4545
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4646
</PackageReference>

Codeuctivity.HtmlRenderer/Renderer.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,19 @@ private static bool IsRunningOnWslOrAzure()
137137
/// </summary>
138138
/// <param name="sourceHtmlFilePath"></param>
139139
/// <param name="destinationPdfFilePath"></param>
140-
public async Task ConvertHtmlToPdf(string sourceHtmlFilePath, string destinationPdfFilePath)
140+
public Task ConvertHtmlToPdf(string sourceHtmlFilePath, string destinationPdfFilePath)
141+
{
142+
PdfOptions pdfOptions = new PdfOptions();
143+
return ConvertHtmlToPdf(sourceHtmlFilePath, destinationPdfFilePath, pdfOptions);
144+
}
145+
146+
/// <summary>
147+
/// Converts a HTML file to a PDF
148+
/// </summary>
149+
/// <param name="sourceHtmlFilePath"></param>
150+
/// <param name="destinationPdfFilePath"></param>
151+
/// <param name="pdfOptions"></param>
152+
public async Task ConvertHtmlToPdf(string sourceHtmlFilePath, string destinationPdfFilePath, PdfOptions pdfOptions)
141153
{
142154
if (!File.Exists(sourceHtmlFilePath))
143155
{
@@ -149,15 +161,26 @@ public async Task ConvertHtmlToPdf(string sourceHtmlFilePath, string destination
149161
await page.GoToAsync($"file://{absolutePath}").ConfigureAwait(false);
150162
// Wait for fonts to be loaded. Omitting this might result in no text rendered in PDF.
151163
await page.EvaluateExpressionHandleAsync("document.fonts.ready");
152-
await page.PdfAsync(destinationPdfFilePath).ConfigureAwait(false);
164+
await page.PdfAsync(destinationPdfFilePath, pdfOptions).ConfigureAwait(false);
165+
}
166+
167+
/// <summary>
168+
/// Converts a HTML file to a PNG
169+
/// </summary>
170+
/// <param name="sourceHtmlFilePath"></param>
171+
/// <param name="destinationPngFilePath"></param>
172+
public Task ConvertHtmlToPng(string sourceHtmlFilePath, string destinationPngFilePath)
173+
{
174+
return ConvertHtmlToPng(sourceHtmlFilePath, destinationPngFilePath, new ScreenshotOptions { FullPage = true });
153175
}
154176

155177
/// <summary>
156178
/// Converts a HTML file to a PNG
157179
/// </summary>
158180
/// <param name="sourceHtmlFilePath"></param>
159181
/// <param name="destinationPngFilePath"></param>
160-
public async Task ConvertHtmlToPng(string sourceHtmlFilePath, string destinationPngFilePath)
182+
/// <param name="screenshotOptions"></param>
183+
public async Task ConvertHtmlToPng(string sourceHtmlFilePath, string destinationPngFilePath, ScreenshotOptions screenshotOptions)
161184
{
162185
if (!File.Exists(sourceHtmlFilePath))
163186
{
@@ -169,7 +192,7 @@ public async Task ConvertHtmlToPng(string sourceHtmlFilePath, string destination
169192
await page.GoToAsync($"file://{absolutePath}").ConfigureAwait(false);
170193
// Wait for fonts to be loaded. Omitting this might result in no text the screenshot.
171194
await page.EvaluateExpressionHandleAsync("document.fonts.ready");
172-
await page.ScreenshotAsync(destinationPngFilePath, new ScreenshotOptions { FullPage = true }).ConfigureAwait(false);
195+
await page.ScreenshotAsync(destinationPngFilePath, screenshotOptions).ConfigureAwait(false);
173196
}
174197

175198
private void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)

Codeuctivity.HtmlRendererCliTests/Codeuctivity.HtmlRendererCliTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<ItemGroup>
1010
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
11-
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.49.0.57237">
11+
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.50.0.58025">
1212
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1313
<PrivateAssets>all</PrivateAssets>
1414
</PackageReference>

Codeuctivity.HtmlRendererTests/Codeuctivity.HtmlRendererTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<ItemGroup>
1919
<PackageReference Include="Codeuctivity.ImageSharpCompare" Version="2.0.76" />
2020
<PackageReference Include="Codeuctivity.PdfjsSharp" Version="1.2.70" />
21-
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.49.0.57237">
21+
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.50.0.58025">
2222
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2323
<PrivateAssets>all</PrivateAssets>
2424
</PackageReference>
20.2 KB
Loading
19.2 KB
Loading
23.6 KB
Loading
28.3 KB
Loading

Codeuctivity.HtmlRendererTests/RendererTests.cs

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Codeuctivity.HtmlRenderer;
22
using Codeuctivity.HtmlRendererTests.Infrastructure;
33
using Codeuctivity.PdfjsSharp;
4+
using Jering.Javascript.NodeJS;
45
using PuppeteerSharp;
56
using System;
67
using System.IO;
@@ -11,8 +12,17 @@
1112

1213
namespace Codeuctivity.HtmlRendererTests
1314
{
14-
public class RendererTests
15+
public class RendererTests : IDisposable
1516
{
17+
private bool disposedValue;
18+
19+
public RendererTests()
20+
{
21+
Rasterize = new Rasterizer();
22+
}
23+
24+
public Rasterizer Rasterize { get; private set; }
25+
1626
[Theory]
1727
[InlineData("BasicTextFormated.html")]
1828
public async Task ShouldConvertHtmlToPdf(string testFileName)
@@ -32,11 +42,9 @@ public async Task ShouldConvertHtmlToPdf(string testFileName)
3242

3343
var actualImagePathDirectory = Path.Combine(Path.GetTempPath(), testFileName);
3444

35-
using var rasterize = new Rasterizer();
36-
3745
if (!IsRunningOnWslOrAzureOrMacos())
3846
{
39-
var actualImages = await rasterize.ConvertToPngAsync(actualFilePath, actualImagePathDirectory);
47+
var actualImages = await Rasterize.ConvertToPngAsync(actualFilePath, actualImagePathDirectory);
4048
Assert.Single(actualImages);
4149
DocumentAsserter.AssertImageIsEqual(actualImages.Single(), expectReferenceFilePath, 2000);
4250
}
@@ -45,6 +53,46 @@ public async Task ShouldConvertHtmlToPdf(string testFileName)
4553
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
4654
}
4755

56+
[Theory]
57+
[InlineData("BasicTextFormatedInlineBackground.html", false, 6000)]
58+
[InlineData("BasicTextFormatedInlineBackground.html", true, 6000)]
59+
public async Task ShouldConvertHtmlToPdfWithOptions(string testFileName, bool printBackground, int allowedPixelDiff)
60+
{
61+
var sourceHtmlFilePath = $"../../../TestInput/{testFileName}";
62+
var actualFilePath = Path.Combine(Path.GetTempPath(), $"ActualConvertHtmlToPdf{testFileName}.{printBackground}.pdf");
63+
var expectReferenceFilePath = $"../../../ExpectedTestOutcome/ExpectedFromHtmlConvertHtmlToPdf{testFileName}.{printBackground}.png";
64+
65+
if (File.Exists(actualFilePath))
66+
{
67+
File.Delete(actualFilePath);
68+
}
69+
70+
await using (var chromiumRenderer = await Renderer.CreateAsync())
71+
{
72+
await chromiumRenderer.ConvertHtmlToPdf(sourceHtmlFilePath, actualFilePath, new PdfOptions() { PrintBackground = printBackground });
73+
74+
var actualImagePathDirectory = Path.Combine(Path.GetTempPath(), testFileName);
75+
76+
if (!IsRunningOnWslOrAzureOrMacos())
77+
{
78+
try
79+
{
80+
var actualImages = await Rasterize.ConvertToPngAsync(actualFilePath, actualImagePathDirectory);
81+
Assert.Single(actualImages);
82+
// File.Copy(actualImages.Single(), expectReferenceFilePath, true);
83+
DocumentAsserter.AssertImageIsEqual(actualImages.Single(), expectReferenceFilePath, allowedPixelDiff);
84+
}
85+
catch (InvocationException ex)
86+
{
87+
// Working around issue in Jering.Javascript.NodeJS, silencing false positiv failing
88+
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Windows), ex.Message);
89+
}
90+
}
91+
File.Delete(actualFilePath);
92+
}
93+
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
94+
}
95+
4896
private static bool IsRunningOnWslOrAzureOrMacos()
4997
{
5098
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
@@ -88,6 +136,36 @@ public async Task ShouldConvertHtmlToPng(string testFileName)
88136
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
89137
}
90138

139+
[Theory]
140+
[InlineData("BasicTextFormatedInlineBackground.html", false, 11000)]
141+
[InlineData("BasicTextFormatedInlineBackground.html", true, 9500)]
142+
public async Task ShouldConvertHtmlToPngScreenshotOptions(string testFileName, bool omitBackground, int allowedPixelDiff)
143+
{
144+
var sourceHtmlFilePath = $"../../../TestInput/{testFileName}";
145+
var actualFilePath = Path.Combine(Path.GetTempPath(), $"ActualConvertHtmlToPng{testFileName}.{omitBackground}.png");
146+
var expectReferenceFilePath = $"../../../ExpectedTestOutcome/ExpectedConvertHtmlToPng{testFileName}.{omitBackground}.png";
147+
148+
if (File.Exists(actualFilePath))
149+
{
150+
File.Delete(actualFilePath);
151+
}
152+
153+
await using (var chromiumRenderer = await Renderer.CreateAsync())
154+
{
155+
ScreenshotOptions screenshotOptions = new ScreenshotOptions
156+
{
157+
OmitBackground = omitBackground
158+
};
159+
160+
await chromiumRenderer.ConvertHtmlToPng(sourceHtmlFilePath, actualFilePath, screenshotOptions);
161+
// File.Copy(actualFilePath, expectReferenceFilePath);
162+
DocumentAsserter.AssertImageIsEqual(actualFilePath, expectReferenceFilePath, allowedPixelDiff);
163+
}
164+
165+
File.Delete(actualFilePath);
166+
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
167+
}
168+
91169
[Fact]
92170
public async Task ShouldDisposeGracefull()
93171
{
@@ -124,5 +202,24 @@ public async Task ShouldConvertHtmlToPngNoSandbox(string testFileName)
124202
File.Delete(actualFilePath);
125203
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
126204
}
205+
206+
protected virtual void Dispose(bool disposing)
207+
{
208+
if (!disposedValue)
209+
{
210+
if (disposing)
211+
{
212+
Rasterize?.Dispose();
213+
}
214+
215+
disposedValue = true;
216+
}
217+
}
218+
219+
public void Dispose()
220+
{
221+
Dispose(disposing: true);
222+
GC.SuppressFinalize(this);
223+
}
127224
}
128225
}

0 commit comments

Comments
 (0)