|
| 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