|
| 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 | +} |
0 commit comments