Skip to content

Commit d0a1e3e

Browse files
committed
Fixed ignored custom launch values
1 parent 973b680 commit d0a1e3e

9 files changed

+84
-16
lines changed

.github/ISSUE_TEMPLATE.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
- OS:
1717
- CPU Architecture:
1818
- .net Version:
19-
- .net Trimming:
19+
- .net Trimming:
20+
- Linux: Output of `cat /proc/version`

Codeuctivity.HtmlRenderer.sln

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
88
.editorconfig = .editorconfig
99
.github\dependabot.yml = .github\dependabot.yml
1010
.github\workflows\dotnet.yml = .github\workflows\dotnet.yml
11+
.github\ISSUE_TEMPLATE.md = .github\ISSUE_TEMPLATE.md
1112
README.md = README.md
1213
EndProjectSection
1314
EndProject

Codeuctivity.HtmlRenderer/Codeuctivity.HtmlRenderer.csproj

+1-1
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.48.0.56517">
43+
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.49.0.57237">
4444
<PrivateAssets>all</PrivateAssets>
4545
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4646
</PackageReference>

Codeuctivity.HtmlRenderer/Renderer.cs

+42-9
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,36 @@ namespace Codeuctivity.HtmlRenderer
1212
/// </summary>
1313
public class Renderer : IAsyncDisposable, IDisposable
1414
{
15+
/// <summary>
16+
/// Ctor
17+
/// </summary>
18+
/// <param name="customChromeiumArgs"></param>
19+
public Renderer(string? customChromeiumArgs)
20+
{
21+
if (customChromeiumArgs == null)
22+
{
23+
LaunchOptions = SystemSpecificConfig();
24+
}
25+
else
26+
{
27+
LaunchOptions = new LaunchOptions() { Args = new[] { customChromeiumArgs } };
28+
}
29+
}
30+
1531
/// <summary>
1632
/// Ctor
1733
/// </summary>
1834
/// <param name="launchOptions"></param>
19-
public Renderer(string? launchOptions = null)
35+
public Renderer(LaunchOptions? launchOptions = null)
2036
{
21-
LaunchOptions = launchOptions;
37+
if (launchOptions == null)
38+
{
39+
LaunchOptions = SystemSpecificConfig();
40+
}
41+
else
42+
{
43+
LaunchOptions = launchOptions;
44+
}
2245
}
2346

2447
private IBrowser Browser { get; set; } = default!;
@@ -29,7 +52,7 @@ public Renderer(string? launchOptions = null)
2952
/// </summary>
3053
public BrowserFetcher BrowserFetcher { get; private set; } = default!;
3154

32-
private string? LaunchOptions { get; }
55+
private LaunchOptions LaunchOptions { get; }
3356

3457
/// <summary>
3558
/// Call CreateAsync before using ConvertHtmlTo*
@@ -45,9 +68,20 @@ public static Task<Renderer> CreateAsync()
4568
/// Call CreateAsync before using ConvertHtmlTo*, accepts custom BrowserFetcher and custom chromium launch options
4669
/// </summary>
4770
/// <param name="browserFetcher"></param>
48-
/// <param name="launchOptions">Adds launch options to chromium</param>
71+
/// <param name="chromeiumArguments">Adds custom arguments to chromium</param>
4972
/// <returns></returns>
50-
public static Task<Renderer> CreateAsync(BrowserFetcher browserFetcher, string? launchOptions = null)
73+
public static Task<Renderer> CreateAsync(BrowserFetcher browserFetcher, string chromeiumArguments)
74+
{
75+
return CreateAsync(browserFetcher, new LaunchOptions() { Args = new[] { chromeiumArguments } });
76+
}
77+
78+
/// <summary>
79+
/// Call CreateAsync before using ConvertHtmlTo*, accepts custom BrowserFetcher and custom chromium launch options
80+
/// </summary>
81+
/// <param name="browserFetcher"></param>
82+
/// <param name="launchOptions">Adds launch options to puppeteer</param>
83+
/// <returns></returns>
84+
public static Task<Renderer> CreateAsync(BrowserFetcher browserFetcher, LaunchOptions? launchOptions = null)
5185
{
5286
var html2Pdf = new Renderer(launchOptions);
5387
return html2Pdf.InitializeAsync(browserFetcher);
@@ -59,18 +93,17 @@ private async Task<Renderer> InitializeAsync(BrowserFetcher browserFetcher)
5993
BrowserFetcher.DownloadProgressChanged += DownloadProgressChanged;
6094

6195
_ = await BrowserFetcher.DownloadAsync(BrowserFetcher.DefaultChromiumRevision ?? string.Empty).ConfigureAwait(false);
62-
Browser = await Puppeteer.LaunchAsync(SystemSpecificConfig()).ConfigureAwait(false);
96+
Browser = await Puppeteer.LaunchAsync(LaunchOptions).ConfigureAwait(false);
6397
return this;
6498
}
6599

66100
private LaunchOptions SystemSpecificConfig()
67101
{
68-
if (string.IsNullOrEmpty(LaunchOptions) && (IsRunningOnWslOrAzure() || IsRunningOnAzureLinux()))
102+
if (IsRunningOnWslOrAzure() || IsRunningOnAzureLinux())
69103
{
70104
return new LaunchOptions { Headless = true, Args = new string[] { "--no-sandbox" } };
71105
}
72-
73-
return new LaunchOptions { Headless = true };
106+
return new LaunchOptions();
74107
}
75108

76109
private static bool IsRunningOnAzureLinux()

Codeuctivity.HtmlRendererCliTests/Codeuctivity.HtmlRendererCliTests.csproj

+1-1
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.48.0.56517">
11+
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.49.0.57237">
1212
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1313
<PrivateAssets>all</PrivateAssets>
1414
</PackageReference>

Codeuctivity.HtmlRendererCliTests/RendererCliTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void PublishedSelfContainedBinaryShouldWork()
5656
};
5757

5858
process.Start();
59-
var isExited = process.WaitForExit(60000);
59+
var isExited = process.WaitForExit(80000);
6060

6161
if (!isExited)
6262
{

Codeuctivity.HtmlRendererTests/Codeuctivity.HtmlRendererTests.csproj

+1-1
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.48.0.56517">
21+
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.49.0.57237">
2222
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2323
<PrivateAssets>all</PrivateAssets>
2424
</PackageReference>

Codeuctivity.HtmlRendererTests/RendererTests.cs

+29-2
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 PuppeteerSharp;
45
using System;
56
using System.IO;
67
using System.Linq;
@@ -39,13 +40,14 @@ public async Task ShouldConvertHtmlToPdf(string testFileName)
3940
Assert.Single(actualImages);
4041
DocumentAsserter.AssertImageIsEqual(actualImages.Single(), expectReferenceFilePath, 2000);
4142
}
43+
File.Delete(actualFilePath);
4244
}
4345
await ChromiumProcessDisposedAsserter.AssertNoChromeProcessIsRunning();
4446
}
4547

4648
private static bool IsRunningOnWslOrAzureOrMacos()
4749
{
48-
if(RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
50+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
4951
{
5052
return true;
5153
}
@@ -79,9 +81,10 @@ public async Task ShouldConvertHtmlToPng(string testFileName)
7981
{
8082
await chromiumRenderer.ConvertHtmlToPng(sourceHtmlFilePath, actualFilePath);
8183

82-
DocumentAsserter.AssertImageIsEqual(actualFilePath, expectReferenceFilePath, 7200);
84+
DocumentAsserter.AssertImageIsEqual(actualFilePath, expectReferenceFilePath, 9000);
8385
}
8486

87+
File.Delete(actualFilePath);
8588
await ChromiumProcessDisposedAsserter.AssertNoChromeProcessIsRunning();
8689
}
8790

@@ -97,5 +100,29 @@ public async Task ShouldDisposeGracefull()
97100
var afterDisposeChromiumTasks = ChromiumProcessDisposedAsserter.CountChromiumTasks();
98101
Assert.Equal(afterDisposeChromiumTasks, initialChromiumTasks);
99102
}
103+
104+
[Theory]
105+
[InlineData("BasicTextFormated.html")]
106+
public async Task ShouldConvertHtmlToPngNoSandbox(string testFileName)
107+
{
108+
var sourceHtmlFilePath = $"../../../TestInput/{testFileName}";
109+
var actualFilePath = Path.Combine(Path.GetTempPath(), $"ActualConvertHtmlToPng{testFileName}.png");
110+
var expectReferenceFilePath = $"../../../ExpectedTestOutcome/ExpectedConvertHtmlToPng{testFileName}.png";
111+
112+
if (File.Exists(actualFilePath))
113+
{
114+
File.Delete(actualFilePath);
115+
}
116+
117+
using (var chromiumRenderer = await Renderer.CreateAsync(new BrowserFetcher(), "--no-sandbox"))
118+
{
119+
await chromiumRenderer.ConvertHtmlToPng(sourceHtmlFilePath, actualFilePath);
120+
121+
DocumentAsserter.AssertImageIsEqual(actualFilePath, expectReferenceFilePath, 9000);
122+
}
123+
124+
File.Delete(actualFilePath);
125+
await ChromiumProcessDisposedAsserter.AssertNoChromeProcessIsRunning();
126+
}
100127
}
101128
}

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,9 @@ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
3131
exit
3232
nvm install 18
3333
```
34+
35+
... and if that fails you can either find out which dependency is missing on your system or you take a shortcut
36+
37+
```bash
38+
sudo apt install -y chromium-browser
39+
```

0 commit comments

Comments
 (0)