Skip to content

Commit 1e192dc

Browse files
committed
Merge develop into feature/screenshot-routes
Resolve conflicts in FacetBase.cs (keep both screenshot and chromium type serialization cases), GotenbergSharpClient.cs (keep both screenshot and PDF engine methods), and README.md (keep screenshot sections).
2 parents 817ba64 + d45780d commit 1e192dc

File tree

61 files changed

+4536
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+4536
-1
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,26 @@ public async Task<Stream> DoOfficeMerge(string sourceDirectory)
223223
return await _sharpClient.MergeOfficeDocsAsync(request);
224224
}
225225
```
226+
227+
### LibreOffice Conversion Options
228+
*Fine-tune LibreOffice conversions with image compression, bookmarks, and native watermarks:*
229+
230+
```csharp
231+
public async Task<Stream> ConvertWithOptions(string sourceDirectory)
232+
{
233+
var builder = new MergeOfficeBuilder()
234+
.WithAsyncAssets(async a => a.AddItems(await GetDocsAsync(sourceDirectory)))
235+
.SetLibreOfficeOptions(o => o
236+
.SetQuality(85)
237+
.SetReduceImageResolution()
238+
.SetMaxImageResolution(300)
239+
.SetExportBookmarks()
240+
.SetNativeWatermarkText("DRAFT"));
241+
242+
var request = await builder.BuildAsync();
243+
return await _sharpClient.MergeOfficeDocsAsync(request);
244+
}
245+
```
226246
### Markdown to PDF
227247
*Markdown to PDF conversion with embedded assets:*
228248

@@ -462,6 +482,23 @@ public async Task<Stream> ConvertToAccessiblePdfA(string pdfPath)
462482
}
463483
```
464484

485+
### PDF Encryption
486+
*Password-protect PDFs with user and owner passwords:*
487+
488+
```csharp
489+
public async Task<Stream> CreateEncryptedPdf()
490+
{
491+
var builder = new HtmlRequestBuilder()
492+
.AddDocument(doc => doc.SetBody("<html><body><h1>Confidential</h1></body></html>"))
493+
.SetPdfOutputOptions(o => o
494+
.SetEncryption(userPassword: "reader123", ownerPassword: "admin456"))
495+
.WithPageProperties(pp => pp.UseChromeDefaults());
496+
497+
var request = builder.Build();
498+
return await _sharpClient.HtmlToPdfAsync(request);
499+
}
500+
```
501+
465502
### Flatten PDFs
466503
*Flatten PDF forms and annotations (v2.8+):*
467504

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+
var handler = new HttpClientHandler();
25+
HttpMessageHandler effectiveHandler = handler;
26+
if (!string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword))
27+
effectiveHandler = new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler };
28+
29+
using var httpClient = new HttpClient(effectiveHandler, disposeHandler: true)
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+
}
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>

examples/EncryptPdf/Program.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 CreateEncryptedPdf(destinationDirectory, options);
20+
Console.WriteLine($"Encrypted PDF created: {path}");
21+
22+
static async Task<string> CreateEncryptedPdf(string destinationDirectory, GotenbergSharpClientOptions options)
23+
{
24+
var handler = new HttpClientHandler();
25+
HttpMessageHandler effectiveHandler = handler;
26+
if (string.IsNullOrWhiteSpace(options.BasicAuthUsername) != string.IsNullOrWhiteSpace(options.BasicAuthPassword))
27+
throw new InvalidOperationException("Both BasicAuthUsername and BasicAuthPassword must be provided, or neither.");
28+
if (!string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword))
29+
effectiveHandler = new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler };
30+
31+
using var httpClient = new HttpClient(effectiveHandler, disposeHandler: true)
32+
{
33+
BaseAddress = options.ServiceUrl,
34+
Timeout = options.TimeOut
35+
};
36+
37+
var sharpClient = new GotenbergSharpClient(httpClient);
38+
39+
// Create a password-protected PDF
40+
var builder = new HtmlRequestBuilder()
41+
.AddDocument(doc => doc.SetBody(@"
42+
<html><body>
43+
<h1>Confidential Report</h1>
44+
<p>This document is password protected.</p>
45+
</body></html>"))
46+
.SetPdfOutputOptions(o => o
47+
.SetEncryption(
48+
userPassword: "reader123", // Required to open the PDF
49+
ownerPassword: "admin456")) // Required to change permissions
50+
.WithPageProperties(pp => pp.UseChromeDefaults());
51+
52+
var request = builder.Build();
53+
var response = await sharpClient.HtmlToPdfAsync(request);
54+
55+
var resultPath = Path.Combine(destinationDirectory, $"Encrypted-{DateTime.Now:yyyyMMddHHmmss}.pdf");
56+
57+
await using var destinationStream = File.Create(resultPath);
58+
await response.CopyToAsync(destinationStream, CancellationToken.None);
59+
60+
return resultPath;
61+
}
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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 sourceDirectory = args.Length > 0 ? args[0] : Path.Combine(AppContext.BaseDirectory, "resources", "OfficeDocs");
17+
var destinationDirectory = args.Length > 1 ? args[1] : Path.Combine(Directory.GetCurrentDirectory(), "output");
18+
Directory.CreateDirectory(destinationDirectory);
19+
20+
var path = await ConvertWithLibreOfficeOptions(sourceDirectory, destinationDirectory, options);
21+
Console.WriteLine($"PDF with LibreOffice options created: {path}");
22+
23+
static async Task<string> ConvertWithLibreOfficeOptions(string sourceDirectory, string destinationDirectory, GotenbergSharpClientOptions options)
24+
{
25+
var handler = new HttpClientHandler();
26+
HttpMessageHandler effectiveHandler = handler;
27+
if (!string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword))
28+
effectiveHandler = new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler };
29+
30+
using var httpClient = new HttpClient(effectiveHandler, disposeHandler: true)
31+
{
32+
BaseAddress = options.ServiceUrl,
33+
Timeout = options.TimeOut
34+
};
35+
36+
var client = new GotenbergSharpClient(httpClient);
37+
38+
// Demonstrates LibreOffice-specific conversion options
39+
var builder = new MergeOfficeBuilder()
40+
.WithAsyncAssets(async b => b.AddItems(await GetDocsAsync(sourceDirectory)))
41+
.SetLibreOfficeOptions(o => o
42+
// Image compression
43+
.SetQuality(85)
44+
.SetReduceImageResolution()
45+
.SetMaxImageResolution(300)
46+
// Export options
47+
.SetExportBookmarks()
48+
.SetExportFormFields(false)
49+
.SetUpdateIndexes()
50+
// Native watermark
51+
.SetNativeWatermarkText("DRAFT")
52+
.SetNativeWatermarkFontName("Arial")
53+
)
54+
.SetPdfOutputOptions(o => o.SetPdfFormat(PdfFormat.A2b));
55+
56+
var response = await client.MergeOfficeDocsAsync(builder).ConfigureAwait(false);
57+
58+
var resultPath = Path.Combine(destinationDirectory, $"LibreOfficeOptions-{DateTime.Now:yyyyMMddHHmmss}.pdf");
59+
60+
await using var destinationStream = File.Create(resultPath);
61+
await response.CopyToAsync(destinationStream, CancellationToken.None);
62+
63+
return resultPath;
64+
}
65+
66+
static async Task<IEnumerable<KeyValuePair<string, byte[]>>> GetDocsAsync(string sourceDirectory)
67+
{
68+
var paths = Directory.GetFiles(sourceDirectory, "*.*", SearchOption.TopDirectoryOnly);
69+
var names = paths.Select(p => new FileInfo(p).Name);
70+
var tasks = paths.Select(f => File.ReadAllBytesAsync(f));
71+
var docs = await Task.WhenAll(tasks);
72+
73+
return names.Select((name, index) => KeyValuePair.Create(name, docs[index])).Take(10);
74+
}
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: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
using Newtonsoft.Json.Linq;
9+
10+
var config = new ConfigurationBuilder()
11+
.SetBasePath(AppContext.BaseDirectory)
12+
.AddJsonFile("appsettings.json")
13+
.Build();
14+
15+
var options = new GotenbergSharpClientOptions();
16+
config.GetSection(nameof(GotenbergSharpClient)).Bind(options);
17+
18+
var destinationDirectory = args.Length > 0 ? args[0] : Path.Combine(Directory.GetCurrentDirectory(), "output");
19+
Directory.CreateDirectory(destinationDirectory);
20+
21+
var sharpClient = CreateClient(options);
22+
23+
// First generate a test PDF
24+
Console.WriteLine("Generating test PDF...");
25+
var pdfBytes = await GenerateTestPdf(sharpClient);
26+
27+
// Flatten
28+
Console.WriteLine("Flattening PDF...");
29+
using var flattenResult = await sharpClient.ExecutePdfEngineAsync(
30+
PdfEngineBuilders.Flatten().WithPdfs(a => a.AddItem("test.pdf", pdfBytes)));
31+
await SaveStream(flattenResult, destinationDirectory, "Flattened.pdf");
32+
33+
// Rotate 90 degrees
34+
Console.WriteLine("Rotating PDF 90 degrees...");
35+
using var rotateResult = await sharpClient.ExecutePdfEngineAsync(
36+
PdfEngineBuilders.Rotate(90).WithPdfs(a => a.AddItem("test.pdf", pdfBytes)));
37+
await SaveStream(rotateResult, destinationDirectory, "Rotated.pdf");
38+
39+
// Encrypt
40+
Console.WriteLine("Encrypting PDF...");
41+
using var encryptResult = await sharpClient.ExecutePdfEngineAsync(
42+
PdfEngineBuilders.Encrypt("reader123", "admin456").WithPdfs(a => a.AddItem("test.pdf", pdfBytes)));
43+
await SaveStream(encryptResult, destinationDirectory, "Encrypted.pdf");
44+
45+
// Write metadata
46+
Console.WriteLine("Writing metadata...");
47+
using var writeResult = await sharpClient.ExecutePdfEngineAsync(
48+
PdfEngineBuilders.WriteMetadata(new Dictionary<string, object>
49+
{
50+
{ "Author", "GotenbergSharpApiClient" },
51+
{ "Title", "PDF Engine Demo" }
52+
}).WithPdfs(a => a.AddItem("test.pdf", pdfBytes)));
53+
await SaveStream(writeResult, destinationDirectory, "WithMetadata.pdf");
54+
55+
// Read metadata
56+
Console.WriteLine("Reading metadata...");
57+
var metadataJson = await sharpClient.ReadPdfMetadataAsync(
58+
PdfEngineBuilders.ReadMetadata().WithPdfs(a => a.AddItem("test.pdf", pdfBytes)));
59+
var parsed = JObject.Parse(metadataJson);
60+
Console.WriteLine($"Metadata: {parsed.ToString(Newtonsoft.Json.Formatting.Indented)}");
61+
62+
Console.WriteLine($"\nAll output saved to: {destinationDirectory}");
63+
64+
static async Task<byte[]> GenerateTestPdf(GotenbergSharpClient client)
65+
{
66+
var builder = new HtmlRequestBuilder()
67+
.AddDocument(doc => doc.SetBody(@"
68+
<html><body>
69+
<h1>PDF Engine Operations Demo</h1>
70+
<p>This PDF is used to demonstrate standalone PDF engine operations.</p>
71+
<form><input type='text' name='field1' value='Form field (will be flattened)'/></form>
72+
</body></html>"));
73+
74+
using var stream = await client.HtmlToPdfAsync(builder);
75+
using var ms = new MemoryStream();
76+
await stream.CopyToAsync(ms);
77+
return ms.ToArray();
78+
}
79+
80+
static async Task SaveStream(Stream stream, string directory, string filename)
81+
{
82+
var path = Path.Combine(directory, $"{Path.GetFileNameWithoutExtension(filename)}-{DateTime.Now:yyyyMMddHHmmss}{Path.GetExtension(filename)}");
83+
await using var file = File.Create(path);
84+
await stream.CopyToAsync(file);
85+
Console.WriteLine($" Saved: {path}");
86+
}
87+
88+
static GotenbergSharpClient CreateClient(GotenbergSharpClientOptions options)
89+
{
90+
var handler = new HttpClientHandler();
91+
HttpMessageHandler effectiveHandler = handler;
92+
93+
if (!string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword))
94+
effectiveHandler = new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler };
95+
96+
var httpClient = new HttpClient(effectiveHandler)
97+
{
98+
BaseAddress = options.ServiceUrl,
99+
Timeout = options.TimeOut
100+
};
101+
102+
return new GotenbergSharpClient(httpClient);
103+
}

0 commit comments

Comments
 (0)