Skip to content

Commit 9b26a91

Browse files
committed
Merge develop into feature/chromium-missing-fields
Resolve conflicts in README.md (keep chromium features section alongside develop's watermark/split/standalone sections) and FacetBase.cs (keep both chromium and cross-cutting type serialization cases).
2 parents 7fc7265 + 3f225a9 commit 9b26a91

47 files changed

Lines changed: 3238 additions & 1 deletion

Some content is hidden

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

README.md

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

@@ -461,6 +481,23 @@ public async Task<Stream> ConvertToAccessiblePdfA(string pdfPath)
461481
}
462482
```
463483

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

@@ -513,6 +550,7 @@ public async Task<Stream> CreateWithChromiumFeatures()
513550
}
514551
```
515552

553+
516554
### Custom Page Properties
517555
*Fine-tune page dimensions and properties:*
518556

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+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 path = await CreateWatermarkedAndRotatedPdf(destinationDirectory, options);
21+
Console.WriteLine($"Watermarked & rotated PDF created: {path}");
22+
23+
static async Task<string> CreateWatermarkedAndRotatedPdf(string destinationDirectory, GotenbergSharpClientOptions options)
24+
{
25+
var handler = new HttpClientHandler();
26+
HttpMessageHandler effectiveHandler = handler;
27+
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+
// Demonstrates watermark, stamp, rotation, and split options
40+
var builder = new HtmlRequestBuilder()
41+
.AddDocument(doc => doc.SetBody(@"
42+
<html><body>
43+
<h1>Cross-Cutting Features Demo</h1>
44+
<p>This PDF has a text watermark and is rotated 90 degrees.</p>
45+
<p>Page 2 content here...</p>
46+
</body></html>"))
47+
// Add a text watermark behind the content
48+
.SetWatermarkOptions(w => w.SetTextWatermark("CONFIDENTIAL"))
49+
// Rotate all pages 90 degrees
50+
.SetRotationOptions(r => r.SetAngle(RotationAngle.Degrees90))
51+
.WithPageProperties(pp => pp.UseChromeDefaults());
52+
53+
var request = builder.Build();
54+
var response = await sharpClient.HtmlToPdfAsync(request);
55+
56+
var resultPath = Path.Combine(destinationDirectory, $"WatermarkRotate-{DateTime.Now:yyyyMMddHHmmss}.pdf");
57+
58+
await using var destinationStream = File.Create(resultPath);
59+
await response.CopyToAsync(destinationStream, CancellationToken.None);
60+
61+
return resultPath;
62+
}
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>

0 commit comments

Comments
 (0)