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
4 changes: 2 additions & 2 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: .NET build and test
env:
CURRENT_VERSION: 2.1.${{ github.run_number }}
CURRENT_VERSION: 2.2.${{ github.run_number }}
LAST_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}

on:
Expand Down Expand Up @@ -58,7 +58,7 @@ jobs:
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Publish FolderProfileWindows
run: dotnet publish ./Codeuctivity.HtmlRendererCli --configuration Release -f net7.0 -p:PublishProfile=FolderProfileWindows
run: dotnet publish ./Codeuctivity.HtmlRendererCli --configuration Release -f net6.0 -p:PublishProfile=FolderProfileWindows
- name: Publish FolderProfilePortable
run: dotnet publish ./Codeuctivity.HtmlRendererCli --configuration Release -f net6.0 -p:PublishProfile=FolderProfilePortable
- name: Zip FolderProfilePortable
Expand Down
4 changes: 2 additions & 2 deletions Codeuctivity.HtmlRenderer/Codeuctivity.HtmlRenderer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@

<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.50.0.58025">
<PackageReference Include="PuppeteerSharp" Version="9.0.1" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.51.0.59060">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
6 changes: 3 additions & 3 deletions Codeuctivity.HtmlRenderer/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Renderer(LaunchOptions? launchOptions = null)
/// <summary>
/// Browser fetcher - used to get chromium bins
/// </summary>
public BrowserFetcher BrowserFetcher { get; private set; } = default!;
public BrowserFetcher BrowserFetcher { get; private set; }

private LaunchOptions LaunchOptions { get; }

Expand Down Expand Up @@ -91,8 +91,8 @@ private async Task<Renderer> InitializeAsync(BrowserFetcher browserFetcher)
{
BrowserFetcher = browserFetcher;
BrowserFetcher.DownloadProgressChanged += DownloadProgressChanged;

_ = await BrowserFetcher.DownloadAsync(BrowserFetcher.DefaultChromiumRevision ?? string.Empty).ConfigureAwait(false);
var revisionInfo = await BrowserFetcher.DownloadAsync(BrowserFetcher.DefaultChromiumRevision ?? string.Empty).ConfigureAwait(false);
LaunchOptions.ExecutablePath = revisionInfo.ExecutablePath;
Browser = await Puppeteer.LaunchAsync(LaunchOptions).ConfigureAwait(false);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<TargetFrameworks>net6.0</TargetFrameworks>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<RootNamespace>Codeuctivity.HtmlRendererCli</RootNamespace>
<DebugSymbols>true</DebugSymbols>
<DebugType>embedded</DebugType>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
4 changes: 3 additions & 1 deletion Codeuctivity.HtmlRendererCli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public static async Task<int> Main(string[] args)
}

Console.WriteLine($"Converting {inputPathDocx} to {outputPathHtml} using PuppeteerSharp.Renderer {Version}");
var browserFetcher = new BrowserFetcher();
BrowserFetcherOptions options = new BrowserFetcherOptions();
options.Path = Path.GetTempPath();
var browserFetcher = new BrowserFetcher(options);
Console.WriteLine($"Fetching chromium from web, to {browserFetcher.DownloadsFolder} .... ");
browserFetcher.DownloadProgressChanged += BrowserFetcher_DownloadProgressChanged;
await using var chromiumRenderer = await Renderer.CreateAsync(browserFetcher, string.Empty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net7.0\publish\FolderProfileWindows\</PublishDir>
<PublishDir>bin\Release\net6.0\publish\FolderProfileWindows\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
<PublishReadyToRun>false</PublishReadyToRun>
<PublishTrimmed>false</PublishTrimmed>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.50.0.58025">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.51.0.59060">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
8 changes: 5 additions & 3 deletions Codeuctivity.HtmlRendererCliTests/RendererCliTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public void PublishedSelfContainedBinaryShouldWork()
};

process.Start();
var outputResult = GetStreamOutput(process.StandardOutput);
var errorResult = GetStreamOutput(process.StandardError);
var isExited = process.WaitForExit(80000);

if (!isExited)
Expand All @@ -66,13 +68,13 @@ public void PublishedSelfContainedBinaryShouldWork()

Assert.True(isExited);

Assert.True(File.Exists(actualFilePath));
Assert.True(File.Exists(actualFilePath), $"StdOut: {outputResult}\nStdErr: {errorResult}\n");
}

private static string DotnetPublishFolderProfileWindows(string projectName)
{
var absolutePath = Path.GetFullPath("../../../../" + projectName);
var expectedBinaryPath = Path.Combine(absolutePath, $"bin/Release/net7.0/publish/FolderProfileWindows/{projectName}.exe");
var expectedBinaryPath = Path.Combine(absolutePath, $"bin/Release/net6.0/publish/FolderProfileWindows/{projectName}.exe");

if (File.Exists(expectedBinaryPath))
{
Expand All @@ -84,7 +86,7 @@ private static string DotnetPublishFolderProfileWindows(string projectName)
StartInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = $"publish {absolutePath} --configuration Release -f net7.0 -p:PublishProfile=FolderProfileWindows",
Arguments = $"publish {absolutePath} --configuration Release -f net6.0 -p:PublishProfile=FolderProfileWindows",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

<ItemGroup>
<PackageReference Include="Codeuctivity.ImageSharpCompare" Version="2.0.76" />
<PackageReference Include="Codeuctivity.PdfjsSharp" Version="1.2.70" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.50.0.58025">
<PackageReference Include="Codeuctivity.PdfjsSharp" Version="1.2.95" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.51.0.59060">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
20 changes: 10 additions & 10 deletions Codeuctivity.HtmlRendererTests/RendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,21 @@ public async Task ShouldConvertHtmlToPdf(string testFileName)

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

if (!IsRunningOnWslOrAzureOrMacos())
if (!IsRunningOnAzureOrMacos())
{
var actualImages = await Rasterize.ConvertToPngAsync(actualFilePath, actualImagePathDirectory);
Assert.Single(actualImages);
DocumentAsserter.AssertImageIsEqual(actualImages.Single(), expectReferenceFilePath, 2000);
// File.Copy(actualImages.Single(), expectReferenceFilePath, true);
DocumentAsserter.AssertImageIsEqual(actualImages.Single(), expectReferenceFilePath, 8000);
}
File.Delete(actualFilePath);
}
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
}

[Theory]
[InlineData("BasicTextFormatedInlineBackground.html", false, 6000)]
[InlineData("BasicTextFormatedInlineBackground.html", true, 6000)]
[InlineData("BasicTextFormatedInlineBackground.html", false, 9000)]
[InlineData("BasicTextFormatedInlineBackground.html", true, 9000)]
public async Task ShouldConvertHtmlToPdfWithOptions(string testFileName, bool printBackground, int allowedPixelDiff)
{
var sourceHtmlFilePath = $"../../../TestInput/{testFileName}";
Expand All @@ -73,7 +74,7 @@ public async Task ShouldConvertHtmlToPdfWithOptions(string testFileName, bool pr

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

if (!IsRunningOnWslOrAzureOrMacos())
if (!IsRunningOnAzureOrMacos())
{
try
{
Expand All @@ -93,7 +94,7 @@ public async Task ShouldConvertHtmlToPdfWithOptions(string testFileName, bool pr
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
}

private static bool IsRunningOnWslOrAzureOrMacos()
private static bool IsRunningOnAzureOrMacos()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
{
Expand All @@ -107,9 +108,8 @@ private static bool IsRunningOnWslOrAzureOrMacos()

var version = File.ReadAllText("/proc/version");
var IsAzure = version.IndexOf("azure", StringComparison.OrdinalIgnoreCase) >= 0;
var IsWsl = version.IndexOf("Microsoft", StringComparison.OrdinalIgnoreCase) >= 0;

return IsWsl || IsAzure;
return IsAzure;
}

[Theory]
Expand Down Expand Up @@ -137,7 +137,7 @@ public async Task ShouldConvertHtmlToPng(string testFileName)
}

[Theory]
[InlineData("BasicTextFormatedInlineBackground.html", false, 11000)]
[InlineData("BasicTextFormatedInlineBackground.html", false, 15000)]
[InlineData("BasicTextFormatedInlineBackground.html", true, 9500)]
public async Task ShouldConvertHtmlToPngScreenshotOptions(string testFileName, bool omitBackground, int allowedPixelDiff)
{
Expand All @@ -158,7 +158,7 @@ public async Task ShouldConvertHtmlToPngScreenshotOptions(string testFileName, b
};

await chromiumRenderer.ConvertHtmlToPng(sourceHtmlFilePath, actualFilePath, screenshotOptions);
// File.Copy(actualFilePath, expectReferenceFilePath);
// File.Copy(actualFilePath, expectReferenceFilePath, true);
DocumentAsserter.AssertImageIsEqual(actualFilePath, expectReferenceFilePath, allowedPixelDiff);
}

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Renders HTML to PDF or PNGs

- Based on PuppeteerSharp
- Give the cli version a try - [Codeuctivity.HtmlRendererCli.exe](https://github.com/Codeuctivity/PuppeteerSharp.Renderer/releases)
- Give the CLI version a try - [Codeuctivity.HtmlRendererCli.exe](https://github.com/Codeuctivity/PuppeteerSharp.Renderer/releases)

## Render HTML to PDF

Expand All @@ -23,10 +23,10 @@ await chromiumRenderer.ConvertHtmlToPng(actualFilePath, pathRasterizedHtml);

## Development

### Linux
### Linux / WSL

```bash
sudo apt install libgbm-dev libatk-bridge2.0-0
sudo apt install libgbm-dev libatk-bridge2.0-0 libnss3 libcups2 libxkbcommon0 libxcomposite1 libxdamage1 libxfixes3 libxrandr2 libpango-1.0-0 libcairo2 libasound2
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
exit
nvm install 18
Expand Down