Skip to content

Commit e395f44

Browse files
authored
Merge pull request #41 from Codeuctivity/40-printbackground-option-not-available
40 printbackground option not available
2 parents e1a198e + 69e8d3a commit e395f44

11 files changed

+403
-7
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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,40 @@ public async Task ShouldConvertHtmlToPdf(string testFileName)
4545
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
4646
}
4747

48+
[Theory]
49+
[InlineData("BasicTextFormatedInlineBackground.html", false)]
50+
[InlineData("BasicTextFormatedInlineBackground.html", true)]
51+
public async Task ShouldConvertHtmlToPdfWithOptions(string testFileName, bool printBackground)
52+
{
53+
var sourceHtmlFilePath = $"../../../TestInput/{testFileName}";
54+
var actualFilePath = Path.Combine(Path.GetTempPath(), $"ActualConvertHtmlToPdf{testFileName}.{printBackground}.pdf");
55+
var expectReferenceFilePath = $"../../../ExpectedTestOutcome/ExpectedFromHtmlConvertHtmlToPdf{testFileName}.{printBackground}.png";
56+
57+
if (File.Exists(actualFilePath))
58+
{
59+
File.Delete(actualFilePath);
60+
}
61+
62+
await using (var chromiumRenderer = await Renderer.CreateAsync())
63+
{
64+
await chromiumRenderer.ConvertHtmlToPdf(sourceHtmlFilePath, actualFilePath, new PdfOptions() { PrintBackground = printBackground });
65+
66+
var actualImagePathDirectory = Path.Combine(Path.GetTempPath(), testFileName);
67+
68+
using var rasterize = new Rasterizer();
69+
70+
if (!IsRunningOnWslOrAzureOrMacos())
71+
{
72+
var actualImages = await rasterize.ConvertToPngAsync(actualFilePath, actualImagePathDirectory);
73+
Assert.Single(actualImages);
74+
// File.Copy(actualImages.Single(), expectReferenceFilePath, true);
75+
DocumentAsserter.AssertImageIsEqual(actualImages.Single(), expectReferenceFilePath, 2000);
76+
}
77+
File.Delete(actualFilePath);
78+
}
79+
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
80+
}
81+
4882
private static bool IsRunningOnWslOrAzureOrMacos()
4983
{
5084
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
@@ -88,6 +122,36 @@ public async Task ShouldConvertHtmlToPng(string testFileName)
88122
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
89123
}
90124

125+
[Theory]
126+
[InlineData("BasicTextFormatedInlineBackground.html", false, 11000)]
127+
[InlineData("BasicTextFormatedInlineBackground.html", true, 9500)]
128+
public async Task ShouldConvertHtmlToPngScreenshotOptions(string testFileName, bool omitBackground, int allowedPixelDiff)
129+
{
130+
var sourceHtmlFilePath = $"../../../TestInput/{testFileName}";
131+
var actualFilePath = Path.Combine(Path.GetTempPath(), $"ActualConvertHtmlToPng{testFileName}.{omitBackground}.png");
132+
var expectReferenceFilePath = $"../../../ExpectedTestOutcome/ExpectedConvertHtmlToPng{testFileName}.{omitBackground}.png";
133+
134+
if (File.Exists(actualFilePath))
135+
{
136+
File.Delete(actualFilePath);
137+
}
138+
139+
await using (var chromiumRenderer = await Renderer.CreateAsync())
140+
{
141+
ScreenshotOptions screenshotOptions = new ScreenshotOptions
142+
{
143+
OmitBackground = omitBackground
144+
};
145+
146+
await chromiumRenderer.ConvertHtmlToPng(sourceHtmlFilePath, actualFilePath, screenshotOptions);
147+
// File.Copy(actualFilePath, expectReferenceFilePath);
148+
DocumentAsserter.AssertImageIsEqual(actualFilePath, expectReferenceFilePath, allowedPixelDiff);
149+
}
150+
151+
File.Delete(actualFilePath);
152+
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
153+
}
154+
91155
[Fact]
92156
public async Task ShouldDisposeGracefull()
93157
{

0 commit comments

Comments
 (0)