-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathProgram.cs
More file actions
85 lines (68 loc) · 3.24 KB
/
Program.cs
File metadata and controls
85 lines (68 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using Gotenberg.Sharp.API.Client;
using Gotenberg.Sharp.API.Client.Domain.Builders;
using Gotenberg.Sharp.API.Client.Domain.ValueObjects;
using Gotenberg.Sharp.API.Client.Domain.Settings;
using Gotenberg.Sharp.API.Client.Infrastructure.Pipeline;
using Microsoft.Extensions.Configuration;
var config = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build();
var options = new GotenbergSharpClientOptions();
config.GetSection(nameof(GotenbergSharpClient)).Bind(options);
var destinationDirectory = args.Length > 0 ? args[0] : Path.Combine(Directory.GetCurrentDirectory(), "output");
Directory.CreateDirectory(destinationDirectory);
var sharpClient = CreateClient(options);
// Screenshot from HTML
var htmlPath = await ScreenshotFromHtml(destinationDirectory, sharpClient);
Console.WriteLine($"HTML screenshot: {htmlPath}");
// Screenshot from URL
var urlPath = await ScreenshotFromUrl(destinationDirectory, sharpClient);
Console.WriteLine($"URL screenshot: {urlPath}");
static async Task<string> ScreenshotFromHtml(string destinationDirectory, GotenbergSharpClient sharpClient)
{
var builder = new ScreenshotHtmlRequestBuilder()
.AddDocument(doc => doc.SetBody(@"
<html>
<body style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 40px;'>
<h1 style='color: white; font-family: sans-serif;'>Screenshot Demo</h1>
<p style='color: white;'>Captured with Gotenberg + GotenbergSharpApiClient</p>
</body>
</html>"))
.WithScreenshotProperties(p => p
.SetSize(1280, 720)
.SetFormat(ScreenshotFormat.Png));
await using var response = await sharpClient.ScreenshotHtmlAsync(builder);
var resultPath = Path.Combine(destinationDirectory, $"ScreenshotHtml-{DateTime.Now:yyyyMMddHHmmss}.png");
await using var file = File.Create(resultPath);
await response.CopyToAsync(file);
return resultPath;
}
static async Task<string> ScreenshotFromUrl(string destinationDirectory, GotenbergSharpClient sharpClient)
{
var builder = new ScreenshotUrlRequestBuilder()
.SetUrl("https://example.com")
.WithScreenshotProperties(p => p
.SetSize(1024, 768)
.SetFormat(ScreenshotFormat.Jpeg)
.SetQuality(90)
.SetClip());
await using var response = await sharpClient.ScreenshotUrlAsync(builder);
var resultPath = Path.Combine(destinationDirectory, $"ScreenshotUrl-{DateTime.Now:yyyyMMddHHmmss}.jpg");
await using var file = File.Create(resultPath);
await response.CopyToAsync(file);
return resultPath;
}
static GotenbergSharpClient CreateClient(GotenbergSharpClientOptions options)
{
var handler = new HttpClientHandler();
HttpMessageHandler effectiveHandler = handler;
if (!string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword))
effectiveHandler = new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler };
var httpClient = new HttpClient(effectiveHandler)
{
BaseAddress = options.ServiceUrl,
Timeout = options.TimeOut
};
return new GotenbergSharpClient(httpClient);
}