Skip to content

Commit c29f290

Browse files
authored
Merge pull request #62 from Partiolainen/part/no-disk
ConvertHtmlStringToPngData method: take a screenshot without saving files
2 parents cf5db99 + a3da142 commit c29f290

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

Codeuctivity.HtmlRenderer/Renderer.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,29 @@ public async Task ConvertHtmlToPng(string sourceHtmlFilePath, string destination
195195
await page.ScreenshotAsync(destinationPngFilePath, screenshotOptions).ConfigureAwait(false);
196196
}
197197

198+
/// <summary>
199+
/// Converts a HTML string to a PNG buffer
200+
/// </summary>
201+
/// <param name="sourceHtmlData"></param>
202+
public Task<byte[]> ConvertHtmlStringToPngData(string sourceHtmlData)
203+
{
204+
return ConvertHtmlStringToPngData(sourceHtmlData, new ScreenshotOptions { FullPage = true });
205+
}
206+
207+
/// <summary>
208+
/// Converts a HTML string to a PNG buffer
209+
/// </summary>
210+
/// <param name="sourceHtmlData"></param>
211+
/// <param name="screenshotOptions"></param>
212+
public async Task<byte[]> ConvertHtmlStringToPngData(string sourceHtmlData, ScreenshotOptions screenshotOptions)
213+
{
214+
await using var page = await Browser.NewPageAsync().ConfigureAwait(false);
215+
await page.SetContentAsync(sourceHtmlData).ConfigureAwait(false);
216+
// Wait for fonts to be loaded. Omitting this might result in no text the screenshot.
217+
await page.EvaluateExpressionHandleAsync("document.fonts.ready");
218+
return await page.ScreenshotDataAsync(screenshotOptions).ConfigureAwait(false);
219+
}
220+
198221
private void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
199222
{
200223
if (LastProgressValue != e.ProgressPercentage)

Codeuctivity.HtmlRendererTests/RendererTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,38 @@ public async Task ShouldConvertHtmlToPngScreenshotOptions(string testFileName, b
166166
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
167167
}
168168

169+
[Theory]
170+
[InlineData("BasicTextFormatedInlineBackground.html", false, 15000)]
171+
[InlineData("BasicTextFormatedInlineBackground.html", true, 9500)]
172+
public async Task ShouldConvertHtmlToPngBufferOptions(string testFileName, bool omitBackground, int allowedPixelDiff)
173+
{
174+
var sourceHtmlFilePath = $"../../../TestInput/{testFileName}";
175+
var actualFilePath = Path.Combine(Path.GetTempPath(), $"ActualConvertHtmlToPng{testFileName}.{omitBackground}.png");
176+
var expectReferenceFilePath = $"../../../ExpectedTestOutcome/ExpectedConvertHtmlToPng{testFileName}.{omitBackground}.png";
177+
178+
if (File.Exists(actualFilePath))
179+
{
180+
File.Delete(actualFilePath);
181+
}
182+
183+
await using (var chromiumRenderer = await Renderer.CreateAsync())
184+
{
185+
ScreenshotOptions screenshotOptions = new ScreenshotOptions
186+
{
187+
OmitBackground = omitBackground
188+
};
189+
190+
var fileContent = await File.ReadAllTextAsync(sourceHtmlFilePath);
191+
var pngData = await chromiumRenderer.ConvertHtmlStringToPngData(fileContent, screenshotOptions);
192+
// File.Copy(actualFilePath, expectReferenceFilePath, true);
193+
await File.WriteAllBytesAsync(actualFilePath, pngData);
194+
DocumentAsserter.AssertImageIsEqual(actualFilePath, expectReferenceFilePath, allowedPixelDiff);
195+
}
196+
197+
File.Delete(actualFilePath);
198+
await ChromiumProcessDisposedAsserter.AssertNoChromiumProcessIsRunning();
199+
}
200+
169201
[Fact]
170202
public async Task ShouldDisposeGracefull()
171203
{

0 commit comments

Comments
 (0)