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