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
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
- uses: actions/setup-node@v3.7.0
- uses: actions/setup-node@v3.8.1
with:
node-version: 18
- name: Restore dependencies
Expand Down
6 changes: 4 additions & 2 deletions Codeuctivity.HtmlRenderer/Codeuctivity.HtmlRenderer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
<AssemblyName>Codeuctivity.HtmlRenderer</AssemblyName>
<RootNamespace>Codeuctivity.HtmlRenderer</RootNamespace>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<AnalysisLevel>latest</AnalysisLevel>
<ContinuousIntegrationBuild Condition="'$(GITHUB_ACTIONS)' == 'true'">true</ContinuousIntegrationBuild>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AssemblyOriginatorKeyFile>Codeuctivity.HtmlRenderer.snk</AssemblyOriginatorKeyFile>
Expand All @@ -39,8 +41,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
<PackageReference Include="PuppeteerSharp" Version="10.1.2" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.55.0.65544">
<PackageReference Include="PuppeteerSharp" Version="11.0.3" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.8.0.76515">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
43 changes: 26 additions & 17 deletions Codeuctivity.HtmlRenderer/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Renderer(string? customChromiumArgs)
{
if (customChromiumArgs == null)
{
LaunchOptions = SystemSpecificConfig();
LaunchOptions = Renderer.SystemSpecificConfig();
}
else
{
Expand All @@ -36,7 +36,7 @@ public Renderer(LaunchOptions? launchOptions = null)
{
if (launchOptions == null)
{
LaunchOptions = SystemSpecificConfig();
LaunchOptions = Renderer.SystemSpecificConfig();
}
else
{
Expand Down Expand Up @@ -89,15 +89,26 @@ public static Task<Renderer> CreateAsync(BrowserFetcher browserFetcher, LaunchOp

private async Task<Renderer> InitializeAsync(BrowserFetcher browserFetcher)
{
BrowserFetcher = browserFetcher;
BrowserFetcher.DownloadProgressChanged += DownloadProgressChanged;
var revisionInfo = await BrowserFetcher.DownloadAsync(BrowserFetcher.DefaultChromiumRevision ?? string.Empty).ConfigureAwait(false);
LaunchOptions.ExecutablePath = revisionInfo.ExecutablePath;
Browser = await Puppeteer.LaunchAsync(LaunchOptions).ConfigureAwait(false);
// for macsome reason the download progress is not called on macos
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
using var browserFetcher1 = new BrowserFetcher();
await browserFetcher1.DownloadAsync();
Browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });
}
else
{
BrowserFetcher = browserFetcher;
BrowserFetcher.DownloadProgressChanged += DownloadProgressChanged;
var revisionInfo = await BrowserFetcher.DownloadAsync(PuppeteerSharp.BrowserData.Chrome.DefaultBuildId).ConfigureAwait(false);
LaunchOptions.ExecutablePath = revisionInfo.GetExecutablePath();
Browser = await Puppeteer.LaunchAsync(LaunchOptions).ConfigureAwait(false);
}

return this;
}

private LaunchOptions SystemSpecificConfig()
private static LaunchOptions SystemSpecificConfig()
{
if (IsRunningOnWslOrAzure() || IsRunningOnAzureLinux())
{
Expand Down Expand Up @@ -126,8 +137,8 @@ private static bool IsRunningOnWslOrAzure()
}

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

return IsWsl || IsAzure;
}
Expand Down Expand Up @@ -157,10 +168,10 @@ public async Task ConvertHtmlToPdf(string sourceHtmlFilePath, string destination
}

var absolutePath = Path.GetFullPath(sourceHtmlFilePath);
await using var page = (await Browser.NewPageAsync().ConfigureAwait(false));
await using var page = await Browser.NewPageAsync().ConfigureAwait(false);
await page.GoToAsync($"file://{absolutePath}").ConfigureAwait(false);
// Wait for fonts to be loaded. Omitting this might result in no text rendered in PDF.
await page.EvaluateExpressionHandleAsync("document.fonts.ready");
await page.EvaluateExpressionHandleAsync("document.fonts.ready").ConfigureAwait(false);
await page.PdfAsync(destinationPdfFilePath, pdfOptions).ConfigureAwait(false);
}

Expand Down Expand Up @@ -188,10 +199,10 @@ public async Task ConvertHtmlToPng(string sourceHtmlFilePath, string destination
}

var absolutePath = Path.GetFullPath(sourceHtmlFilePath);
await using var page = (await Browser.NewPageAsync().ConfigureAwait(false));
await using var page = await Browser.NewPageAsync().ConfigureAwait(false);
await page.GoToAsync($"file://{absolutePath}").ConfigureAwait(false);
// Wait for fonts to be loaded. Omitting this might result in no text the screenshot.
await page.EvaluateExpressionHandleAsync("document.fonts.ready");
await page.EvaluateExpressionHandleAsync("document.fonts.ready").ConfigureAwait(false);
await page.ScreenshotAsync(destinationPngFilePath, screenshotOptions).ConfigureAwait(false);
}

Expand All @@ -214,7 +225,7 @@ public async Task<byte[]> ConvertHtmlStringToPngData(string sourceHtmlData, Scre
await using var page = await Browser.NewPageAsync().ConfigureAwait(false);
await page.SetContentAsync(sourceHtmlData).ConfigureAwait(false);
// Wait for fonts to be loaded. Omitting this might result in no text the screenshot.
await page.EvaluateExpressionHandleAsync("document.fonts.ready");
await page.EvaluateExpressionHandleAsync("document.fonts.ready").ConfigureAwait(false);
return await page.ScreenshotDataAsync(screenshotOptions).ConfigureAwait(false);
}

Expand Down Expand Up @@ -243,9 +254,7 @@ public async ValueTask DisposeAsync()
await DisposeAsyncCore().ConfigureAwait(false);

Dispose(disposing: false);
#pragma warning disable CA1816 // Dispose methods should call SuppressFinalize
GC.SuppressFinalize(this);
#pragma warning restore CA1816 // Dispose methods should call SuppressFinalize
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0</TargetFrameworks>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<AnalysisLevel>latest</AnalysisLevel>
<RootNamespace>Codeuctivity.HtmlRendererCli</RootNamespace>
<DebugSymbols>true</DebugSymbols>
<DebugType>embedded</DebugType>
Expand Down
22 changes: 10 additions & 12 deletions Codeuctivity.HtmlRendererCli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@

namespace Codeuctivity.HtmlRendererCli
{
public class Program
public static class Program
{
public static readonly Assembly Reference = typeof(Renderer).Assembly;
public static readonly Version Version = Reference.GetName().Version;
private static int _lastProgressValue;

public static async Task<int> Main(string[] args)
{
if (args.Length != 2)
if (args?.Length != 2)
{
Console.WriteLine("Usage: PuppeteerSharp.RendererCli <sourceHtmlFilePath> <destinatioPdfFilePath>");
return 1;
}

var inputPathDocx = args[0];
var inputPathDocX = args[0];
var outputPathHtml = args[1];

if (!File.Exists(inputPathDocx))
if (!File.Exists(inputPathDocX))
{
Console.WriteLine($"Could not find source {inputPathDocx}.");
Console.WriteLine($"Could not find source {inputPathDocX}.");
return 1;
}

Expand All @@ -36,14 +36,12 @@ public static async Task<int> Main(string[] args)
return 1;
}

Console.WriteLine($"Converting {inputPathDocx} to {outputPathHtml} using PuppeteerSharp.Renderer {Version}");
BrowserFetcherOptions options = new BrowserFetcherOptions();
options.Path = Path.GetTempPath();
var browserFetcher = new BrowserFetcher(options);
Console.WriteLine($"Fetching chromium from web, to {browserFetcher.DownloadsFolder} .... ");
Console.WriteLine($"Converting {inputPathDocX} to {outputPathHtml} using PuppeteerSharp.Renderer {Version}");
using var browserFetcher = new BrowserFetcher();
Console.WriteLine($"Fetching chromium from web, to {browserFetcher.CacheDir} .... ");
browserFetcher.DownloadProgressChanged += BrowserFetcher_DownloadProgressChanged;
await using var chromiumRenderer = await Renderer.CreateAsync(browserFetcher, string.Empty);
await chromiumRenderer.ConvertHtmlToPdf(inputPathDocx, outputPathHtml);
await using var chromiumRenderer = await Renderer.CreateAsync(browserFetcher);
await chromiumRenderer.ConvertHtmlToPdf(inputPathDocX, outputPathHtml);
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.55.0.65544">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.8.0.76515">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<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 @@ -3,9 +3,9 @@

namespace Codeuctivity.HtmlRendererCliTests
{
internal class FactRunableOnWindowsAttribute : FactAttribute
internal class FactRunnableOnWindowsAttribute : FactAttribute
{
public FactRunableOnWindowsAttribute()
public FactRunnableOnWindowsAttribute()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Expand Down
10 changes: 5 additions & 5 deletions Codeuctivity.HtmlRendererCliTests/RendererCliTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void VersionShouldBeProcessed()
[Fact]
public async Task ConversionShouldWork()
{
var testFileName = "BasicTextFormated.html";
var testFileName = "BasicTextFormatted.html";
var sourceHtmlFilePath = $"../../../TestInput/{testFileName}";
var actualFilePath = Path.Combine(Path.GetTempPath(), $"Actual{testFileName}.pdf");

Expand All @@ -28,10 +28,10 @@ public async Task ConversionShouldWork()
Assert.True(File.Exists(actualFilePath));
}

[FactRunableOnWindows]
[FactRunnableOnWindows]
public void PublishedSelfContainedBinaryShouldWork()
{
var testFileName = "BasicTextFormated.html";
var testFileName = "BasicTextFormatted.html";
var sourceHtmlFilePath = Path.GetFullPath($"../../../TestInput/{testFileName}");
var actualFilePath = Path.Combine(Path.GetTempPath(), $"Actual{testFileName}.pdf");

Expand All @@ -40,13 +40,13 @@ public void PublishedSelfContainedBinaryShouldWork()
File.Delete(actualFilePath);
}

var acutualWindowsBinary = DotnetPublishFolderProfileWindows("Codeuctivity.HtmlRendererCli");
var actualWindowsBinary = DotnetPublishFolderProfileWindows("Codeuctivity.HtmlRendererCli");

using var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = acutualWindowsBinary,
FileName = actualWindowsBinary,
Arguments = $" {sourceHtmlFilePath} {actualFilePath}",
RedirectStandardOutput = true,
RedirectStandardError = true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup>

<ItemGroup>
<Compile Remove="SourceTestFiles\**" />
<EmbeddedResource Remove="SourceTestFiles\**" />
<None Remove="SourceTestFiles\**" />
</ItemGroup>
<ItemGroup>
<Compile Remove="SourceTestFiles\**" />
<EmbeddedResource Remove="SourceTestFiles\**" />
<None Remove="SourceTestFiles\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Codeuctivity.ImageSharpCompare" Version="2.0.155" />
<PackageReference Include="Codeuctivity.PdfjsSharp" Version="1.2.95" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.55.0.65544">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Codeuctivity.ImageSharpCompare" Version="2.0.182" />
<PackageReference Include="Codeuctivity.PdfjsSharp" Version="1.2.95" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.8.0.76515">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Codeuctivity.HtmlRenderer\Codeuctivity.HtmlRenderer.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Codeuctivity.HtmlRenderer\Codeuctivity.HtmlRenderer.csproj" />
</ItemGroup>
</Project>
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ private static void CopyToTestOutput(string testOutputFile)
}

if (!Directory.Exists(TestOutputFirectory))
{
Directory.CreateDirectory(TestOutputFirectory);
}

File.Copy(testOutputFile, Path.Combine(TestOutputFirectory, Path.GetFileName(testOutputFile)), true);
}
Expand Down
Loading