Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
- OS:
- CPU Architecture:
- .net Version:
- .net Trimming:
- .net Trimming:
- Linux: Output of `cat /proc/version`
1 change: 1 addition & 0 deletions Codeuctivity.HtmlRenderer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
.github\dependabot.yml = .github\dependabot.yml
.github\workflows\dotnet.yml = .github\workflows\dotnet.yml
.github\ISSUE_TEMPLATE.md = .github\ISSUE_TEMPLATE.md
README.md = README.md
EndProjectSection
EndProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
<PackageReference Include="PuppeteerSharp" Version="8.0.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.48.0.56517">
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.49.0.57237">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
51 changes: 42 additions & 9 deletions Codeuctivity.HtmlRenderer/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,36 @@ namespace Codeuctivity.HtmlRenderer
/// </summary>
public class Renderer : IAsyncDisposable, IDisposable
{
/// <summary>
/// Ctor
/// </summary>
/// <param name="customChromiumArgs"></param>
public Renderer(string? customChromiumArgs)
{
if (customChromiumArgs == null)
{
LaunchOptions = SystemSpecificConfig();
}
else
{
LaunchOptions = new LaunchOptions() { Args = new[] { customChromiumArgs } };
}
}

/// <summary>
/// Ctor
/// </summary>
/// <param name="launchOptions"></param>
public Renderer(string? launchOptions = null)
public Renderer(LaunchOptions? launchOptions = null)
{
LaunchOptions = launchOptions;
if (launchOptions == null)
{
LaunchOptions = SystemSpecificConfig();
}
else
{
LaunchOptions = launchOptions;
}
}

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

private string? LaunchOptions { get; }
private LaunchOptions LaunchOptions { get; }

/// <summary>
/// Call CreateAsync before using ConvertHtmlTo*
Expand All @@ -45,9 +68,20 @@ public static Task<Renderer> CreateAsync()
/// Call CreateAsync before using ConvertHtmlTo*, accepts custom BrowserFetcher and custom chromium launch options
/// </summary>
/// <param name="browserFetcher"></param>
/// <param name="launchOptions">Adds launch options to chromium</param>
/// <param name="chromiumArguments">Adds custom arguments to chromium</param>
/// <returns></returns>
public static Task<Renderer> CreateAsync(BrowserFetcher browserFetcher, string? launchOptions = null)
public static Task<Renderer> CreateAsync(BrowserFetcher browserFetcher, string chromiumArguments)
{
return CreateAsync(browserFetcher, new LaunchOptions() { Args = new[] { chromiumArguments } });
}

/// <summary>
/// Call CreateAsync before using ConvertHtmlTo*, accepts custom BrowserFetcher and custom chromium launch options
/// </summary>
/// <param name="browserFetcher"></param>
/// <param name="launchOptions">Adds launch options to puppeteer</param>
/// <returns></returns>
public static Task<Renderer> CreateAsync(BrowserFetcher browserFetcher, LaunchOptions? launchOptions = null)
{
var html2Pdf = new Renderer(launchOptions);
return html2Pdf.InitializeAsync(browserFetcher);
Expand All @@ -59,18 +93,17 @@ private async Task<Renderer> InitializeAsync(BrowserFetcher browserFetcher)
BrowserFetcher.DownloadProgressChanged += DownloadProgressChanged;

_ = await BrowserFetcher.DownloadAsync(BrowserFetcher.DefaultChromiumRevision ?? string.Empty).ConfigureAwait(false);
Browser = await Puppeteer.LaunchAsync(SystemSpecificConfig()).ConfigureAwait(false);
Browser = await Puppeteer.LaunchAsync(LaunchOptions).ConfigureAwait(false);
return this;
}

private LaunchOptions SystemSpecificConfig()
{
if (string.IsNullOrEmpty(LaunchOptions) && (IsRunningOnWslOrAzure() || IsRunningOnAzureLinux()))
if (IsRunningOnWslOrAzure() || IsRunningOnAzureLinux())
{
return new LaunchOptions { Headless = true, Args = new string[] { "--no-sandbox" } };
}

return new LaunchOptions { Headless = true };
return new LaunchOptions();
}

private static bool IsRunningOnAzureLinux()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.48.0.56517">
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.49.0.57237">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion Codeuctivity.HtmlRendererCliTests/RendererCliTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void PublishedSelfContainedBinaryShouldWork()
};

process.Start();
var isExited = process.WaitForExit(60000);
var isExited = process.WaitForExit(80000);

if (!isExited)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<ItemGroup>
<PackageReference Include="Codeuctivity.ImageSharpCompare" Version="2.0.76" />
<PackageReference Include="Codeuctivity.PdfjsSharp" Version="1.2.70" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.48.0.56517">
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.49.0.57237">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Codeuctivity.HtmlRendererTests.Infrastructure
{
public static class ChromiumProcessDisposedAsserter
{
public static async Task AssertNoChromeProcessIsRunning()
public static async Task AssertNoChromiumProcessIsRunning()
{
for (var i = 0; i < 20 && CountChromiumTasks() > 0; i++)
{
Expand Down
35 changes: 31 additions & 4 deletions Codeuctivity.HtmlRendererTests/RendererTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Codeuctivity.HtmlRenderer;
using Codeuctivity.HtmlRendererTests.Infrastructure;
using Codeuctivity.PdfjsSharp;
using PuppeteerSharp;
using System;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -39,13 +40,14 @@ public async Task ShouldConvertHtmlToPdf(string testFileName)
Assert.Single(actualImages);
DocumentAsserter.AssertImageIsEqual(actualImages.Single(), expectReferenceFilePath, 2000);
}
File.Delete(actualFilePath);
}
await ChromiumProcessDisposedAsserter.AssertNoChromeProcessIsRunning();
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
}

private static bool IsRunningOnWslOrAzureOrMacos()
{
if(RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
{
return true;
}
Expand Down Expand Up @@ -79,10 +81,11 @@ public async Task ShouldConvertHtmlToPng(string testFileName)
{
await chromiumRenderer.ConvertHtmlToPng(sourceHtmlFilePath, actualFilePath);

DocumentAsserter.AssertImageIsEqual(actualFilePath, expectReferenceFilePath, 7200);
DocumentAsserter.AssertImageIsEqual(actualFilePath, expectReferenceFilePath, 9000);
}

await ChromiumProcessDisposedAsserter.AssertNoChromeProcessIsRunning();
File.Delete(actualFilePath);
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
}

[Fact]
Expand All @@ -97,5 +100,29 @@ public async Task ShouldDisposeGracefull()
var afterDisposeChromiumTasks = ChromiumProcessDisposedAsserter.CountChromiumTasks();
Assert.Equal(afterDisposeChromiumTasks, initialChromiumTasks);
}

[Theory]
[InlineData("BasicTextFormated.html")]
public async Task ShouldConvertHtmlToPngNoSandbox(string testFileName)
{
var sourceHtmlFilePath = $"../../../TestInput/{testFileName}";
var actualFilePath = Path.Combine(Path.GetTempPath(), $"ActualConvertHtmlToPng{testFileName}.png");
var expectReferenceFilePath = $"../../../ExpectedTestOutcome/ExpectedConvertHtmlToPng{testFileName}.png";

if (File.Exists(actualFilePath))
{
File.Delete(actualFilePath);
}

using (var chromiumRenderer = await Renderer.CreateAsync(new BrowserFetcher(), "--no-sandbox"))
{
await chromiumRenderer.ConvertHtmlToPng(sourceHtmlFilePath, actualFilePath);

DocumentAsserter.AssertImageIsEqual(actualFilePath, expectReferenceFilePath, 9000);
}

File.Delete(actualFilePath);
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
}
}
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
exit
nvm install 18
```

... and if that fails you can either find out which dependency is missing on your system or you take a shortcut

```bash
sudo apt install -y chromium-browser
```