|
| 1 | +using Gotenberg.Sharp.API.Client; |
| 2 | +using Gotenberg.Sharp.API.Client.Domain.Builders; |
| 3 | +using Gotenberg.Sharp.API.Client.Domain.Settings; |
| 4 | +using Gotenberg.Sharp.API.Client.Infrastructure.Pipeline; |
| 5 | + |
| 6 | +using Microsoft.Extensions.Configuration; |
| 7 | + |
| 8 | +var config = new ConfigurationBuilder() |
| 9 | + .SetBasePath(AppContext.BaseDirectory) |
| 10 | + .AddJsonFile("appsettings.json") |
| 11 | + .Build(); |
| 12 | + |
| 13 | +var options = new GotenbergSharpClientOptions(); |
| 14 | +config.GetSection(nameof(GotenbergSharpClient)).Bind(options); |
| 15 | + |
| 16 | +var destinationDirectory = args.Length > 0 ? args[0] : Path.Combine(Directory.GetCurrentDirectory(), "output"); |
| 17 | +Directory.CreateDirectory(destinationDirectory); |
| 18 | + |
| 19 | +var path = await CreateWithChromiumFeatures(destinationDirectory, options); |
| 20 | +Console.WriteLine($"PDF created: {path}"); |
| 21 | + |
| 22 | +static async Task<string> CreateWithChromiumFeatures(string destinationDirectory, GotenbergSharpClientOptions options) |
| 23 | +{ |
| 24 | + using var handler = new HttpClientHandler(); |
| 25 | + using var authHandler = !string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword) |
| 26 | + ? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler } |
| 27 | + : null; |
| 28 | + |
| 29 | + using var httpClient = new HttpClient(authHandler ?? (HttpMessageHandler)handler) |
| 30 | + { |
| 31 | + BaseAddress = options.ServiceUrl, |
| 32 | + Timeout = options.TimeOut |
| 33 | + }; |
| 34 | + |
| 35 | + var sharpClient = new GotenbergSharpClient(httpClient); |
| 36 | + |
| 37 | + // Demonstrates waitForSelector, emulated media features, and error handling options |
| 38 | + var builder = new HtmlRequestBuilder() |
| 39 | + .AddDocument(doc => doc.SetBody(@" |
| 40 | + <html> |
| 41 | + <head> |
| 42 | + <style> |
| 43 | + @media (prefers-color-scheme: dark) { |
| 44 | + body { background: #1a1a2e; color: #eee; } |
| 45 | + } |
| 46 | + </style> |
| 47 | + </head> |
| 48 | + <body> |
| 49 | + <div id='content'> |
| 50 | + <h1>Chromium Feature Demo</h1> |
| 51 | + <p>This PDF was generated with dark mode emulation, waitForSelector, |
| 52 | + and strict error handling.</p> |
| 53 | + </div> |
| 54 | + </body> |
| 55 | + </html>")) |
| 56 | + .SetConversionBehaviors(b => b |
| 57 | + // Wait for the #content element before converting |
| 58 | + .SetWaitForSelector("#content") |
| 59 | + // Emulate dark mode |
| 60 | + .AddEmulatedMediaFeature("prefers-color-scheme", "dark") |
| 61 | + // Fail if the main page returns 4xx or 5xx |
| 62 | + .SetFailOnHttpStatusCodes(499, 599) |
| 63 | + // Fail if any resource fails to load |
| 64 | + .FailOnResourceLoadingFailed() |
| 65 | + // Fail on any console exceptions |
| 66 | + .FailOnConsoleExceptions() |
| 67 | + // Ignore CDN domains for status code checks |
| 68 | + .AddIgnoreResourceHttpStatusDomains("cdn.example.com") |
| 69 | + ) |
| 70 | + .WithPageProperties(pp => pp.UseChromeDefaults()); |
| 71 | + |
| 72 | + var request = builder.Build(); |
| 73 | + var response = await sharpClient.HtmlToPdfAsync(request); |
| 74 | + |
| 75 | + var resultPath = Path.Combine(destinationDirectory, $"ChromiumFeatures-{DateTime.Now:yyyyMMddHHmmss}.pdf"); |
| 76 | + |
| 77 | + await using var destinationStream = File.Create(resultPath); |
| 78 | + await response.CopyToAsync(destinationStream, CancellationToken.None); |
| 79 | + |
| 80 | + return resultPath; |
| 81 | +} |
0 commit comments