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
23 changes: 23 additions & 0 deletions Codeuctivity.HtmlRenderer/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,29 @@ public async Task ConvertHtmlToPng(string sourceHtmlFilePath, string destination
await page.ScreenshotAsync(destinationPngFilePath, screenshotOptions).ConfigureAwait(false);
}

/// <summary>
/// Converts a HTML string to a PNG buffer
/// </summary>
/// <param name="sourceHtmlData"></param>
public Task<byte[]> ConvertHtmlStringToPngData(string sourceHtmlData)
{
return ConvertHtmlStringToPngData(sourceHtmlData, new ScreenshotOptions { FullPage = true });
}

/// <summary>
/// Converts a HTML string to a PNG buffer
/// </summary>
/// <param name="sourceHtmlData"></param>
/// <param name="screenshotOptions"></param>
public async Task<byte[]> ConvertHtmlStringToPngData(string sourceHtmlData, ScreenshotOptions screenshotOptions)
{
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");
return await page.ScreenshotDataAsync(screenshotOptions).ConfigureAwait(false);
}

private void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
if (LastProgressValue != e.ProgressPercentage)
Expand Down
32 changes: 32 additions & 0 deletions Codeuctivity.HtmlRendererTests/RendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,38 @@ public async Task ShouldConvertHtmlToPngScreenshotOptions(string testFileName, b
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
}

[Theory]
[InlineData("BasicTextFormatedInlineBackground.html", false, 15000)]
[InlineData("BasicTextFormatedInlineBackground.html", true, 9500)]
public async Task ShouldConvertHtmlToPngBufferOptions(string testFileName, bool omitBackground, int allowedPixelDiff)
{
var sourceHtmlFilePath = $"../../../TestInput/{testFileName}";
var actualFilePath = Path.Combine(Path.GetTempPath(), $"ActualConvertHtmlToPng{testFileName}.{omitBackground}.png");
var expectReferenceFilePath = $"../../../ExpectedTestOutcome/ExpectedConvertHtmlToPng{testFileName}.{omitBackground}.png";

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

await using (var chromiumRenderer = await Renderer.CreateAsync())
{
ScreenshotOptions screenshotOptions = new ScreenshotOptions
{
OmitBackground = omitBackground
};

var fileContent = await File.ReadAllTextAsync(sourceHtmlFilePath);
var pngData = await chromiumRenderer.ConvertHtmlStringToPngData(fileContent, screenshotOptions);
// File.Copy(actualFilePath, expectReferenceFilePath, true);
await File.WriteAllBytesAsync(actualFilePath, pngData);
DocumentAsserter.AssertImageIsEqual(actualFilePath, expectReferenceFilePath, allowedPixelDiff);
}

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

[Fact]
public async Task ShouldDisposeGracefull()
{
Expand Down