|
| 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 | + using var handler = new HttpClientHandler(); |
| 26 | + using var authHandler = !string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword) |
| 27 | + ? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler } |
| 28 | + : null; |
| 29 | + |
| 30 | + using var httpClient = new HttpClient(authHandler ?? (HttpMessageHandler)handler) |
| 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 | +} |
0 commit comments