Skip to content

Commit 27956cf

Browse files
committed
Add ChromiumFeatures example and README section
Add console example demonstrating waitForSelector, emulated media features, failOnHttpStatusCodes, and ignoreResourceHttpStatusDomains. Add corresponding README section with concise usage example.
1 parent 18ed243 commit 27956cf

3 files changed

Lines changed: 104 additions & 0 deletions

File tree

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,27 @@ public async Task<Stream> FastConversion()
492492
}
493493
```
494494

495+
### Wait For Selector & Emulated Media Features
496+
*Wait for a DOM element and emulate CSS media features like dark mode:*
497+
498+
```csharp
499+
public async Task<Stream> CreateWithChromiumFeatures()
500+
{
501+
var builder = new HtmlRequestBuilder()
502+
.AddDocument(doc => doc.SetBody("<html><body><div id='app'>Ready</div></body></html>"))
503+
.SetConversionBehaviors(b => b
504+
.SetWaitForSelector("#app")
505+
.AddEmulatedMediaFeature("prefers-color-scheme", "dark")
506+
.SetFailOnHttpStatusCodes(499, 599)
507+
.FailOnResourceLoadingFailed()
508+
.AddIgnoreResourceHttpStatusDomains("cdn.example.com"))
509+
.WithPageProperties(pp => pp.UseChromeDefaults());
510+
511+
var request = builder.Build();
512+
return await _sharpClient.HtmlToPdfAsync(request);
513+
}
514+
```
515+
495516
### Custom Page Properties
496517
*Fine-tune page dimensions and properties:*
497518

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
</Project>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)