Skip to content

Commit dd1e9b0

Browse files
authored
Merge pull request #66 from ChangemakerStudios/feature/convert-linqpad-to-console
Convert LinqPad examples to .NET 8 console apps with basic auth middleware
2 parents 130257d + 4b93802 commit dd1e9b0

Some content is hidden

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

54 files changed

+1099
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,4 @@ __pycache__/
289289
*.xsd.cs
290290

291291
settings.local.json
292+
examples/**/output

GotenbergSharpApiClient.sln

Lines changed: 142 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,32 @@ VisualStudioVersion = 17.0.32112.339
55
MinimumVisualStudioVersion = 15.0.26124.0
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gotenberg.Sharp.Api.Client", "src\Gotenberg.Sharp.Api.Client\Gotenberg.Sharp.Api.Client.csproj", "{75F783A4-9392-412F-9DFE-00EE89527C10}"
77
EndProject
8-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{FE638D0D-6A76-4BF4-AF06-D8AAB9726723}"
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GotenbergSharpClient.Tests", "test\GotenbergSharpClient.Tests\GotenbergSharpClient.Tests.csproj", "{EEAF9CA2-7962-176A-E851-BF81D8DE31F0}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
911
ProjectSection(SolutionItems) = preProject
10-
.editorconfig = .editorconfig
12+
examples\appsettings.json = examples\appsettings.json
13+
examples\Directory.Build.props = examples\Directory.Build.props
14+
examples\README.md = examples\README.md
1115
EndProjectSection
1216
EndProject
13-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GotenbergSharpClient.Tests", "test\GotenbergSharpClient.Tests\GotenbergSharpClient.Tests.csproj", "{EEAF9CA2-7962-176A-E851-BF81D8DE31F0}"
17+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DIExample", "examples\DIExample\DIExample.csproj", "{C022147F-DA63-5B3C-9349-FEB9675362DF}"
18+
EndProject
19+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HtmlConvert", "examples\HtmlConvert\HtmlConvert.csproj", "{6113FC36-F04E-B6CA-5C64-EA6156640C94}"
20+
EndProject
21+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HtmlWithMarkdown", "examples\HtmlWithMarkdown\HtmlWithMarkdown.csproj", "{893AD0F6-7E7E-8550-56D7-BBCB77933BD3}"
22+
EndProject
23+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OfficeMerge", "examples\OfficeMerge\OfficeMerge.csproj", "{2A1FC5B6-EB87-C86A-71BB-42784913627C}"
24+
EndProject
25+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PdfConvert", "examples\PdfConvert\PdfConvert.csproj", "{BB512649-8BE2-38B1-49D1-E8CA8701BA1A}"
26+
EndProject
27+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PdfMerge", "examples\PdfMerge\PdfMerge.csproj", "{DDD4236E-F481-4DE9-306C-66E77A8D2CB9}"
28+
EndProject
29+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UrlConvert", "examples\UrlConvert\UrlConvert.csproj", "{9C18F2D1-E9E9-613A-DE10-4263A2EA8A17}"
30+
EndProject
31+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UrlsToMergedPdf", "examples\UrlsToMergedPdf\UrlsToMergedPdf.csproj", "{A4D0B498-A934-2F11-2AA7-18D1D1C9E881}"
32+
EndProject
33+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Webhook", "examples\Webhook\Webhook.csproj", "{01C9F662-6378-3FC4-B629-9FD37A38033D}"
1434
EndProject
1535
Global
1636
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -46,10 +66,129 @@ Global
4666
{EEAF9CA2-7962-176A-E851-BF81D8DE31F0}.Release|x64.Build.0 = Release|Any CPU
4767
{EEAF9CA2-7962-176A-E851-BF81D8DE31F0}.Release|x86.ActiveCfg = Release|Any CPU
4868
{EEAF9CA2-7962-176A-E851-BF81D8DE31F0}.Release|x86.Build.0 = Release|Any CPU
69+
{C022147F-DA63-5B3C-9349-FEB9675362DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
70+
{C022147F-DA63-5B3C-9349-FEB9675362DF}.Debug|Any CPU.Build.0 = Debug|Any CPU
71+
{C022147F-DA63-5B3C-9349-FEB9675362DF}.Debug|x64.ActiveCfg = Debug|Any CPU
72+
{C022147F-DA63-5B3C-9349-FEB9675362DF}.Debug|x64.Build.0 = Debug|Any CPU
73+
{C022147F-DA63-5B3C-9349-FEB9675362DF}.Debug|x86.ActiveCfg = Debug|Any CPU
74+
{C022147F-DA63-5B3C-9349-FEB9675362DF}.Debug|x86.Build.0 = Debug|Any CPU
75+
{C022147F-DA63-5B3C-9349-FEB9675362DF}.Release|Any CPU.ActiveCfg = Release|Any CPU
76+
{C022147F-DA63-5B3C-9349-FEB9675362DF}.Release|Any CPU.Build.0 = Release|Any CPU
77+
{C022147F-DA63-5B3C-9349-FEB9675362DF}.Release|x64.ActiveCfg = Release|Any CPU
78+
{C022147F-DA63-5B3C-9349-FEB9675362DF}.Release|x64.Build.0 = Release|Any CPU
79+
{C022147F-DA63-5B3C-9349-FEB9675362DF}.Release|x86.ActiveCfg = Release|Any CPU
80+
{C022147F-DA63-5B3C-9349-FEB9675362DF}.Release|x86.Build.0 = Release|Any CPU
81+
{6113FC36-F04E-B6CA-5C64-EA6156640C94}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
82+
{6113FC36-F04E-B6CA-5C64-EA6156640C94}.Debug|Any CPU.Build.0 = Debug|Any CPU
83+
{6113FC36-F04E-B6CA-5C64-EA6156640C94}.Debug|x64.ActiveCfg = Debug|Any CPU
84+
{6113FC36-F04E-B6CA-5C64-EA6156640C94}.Debug|x64.Build.0 = Debug|Any CPU
85+
{6113FC36-F04E-B6CA-5C64-EA6156640C94}.Debug|x86.ActiveCfg = Debug|Any CPU
86+
{6113FC36-F04E-B6CA-5C64-EA6156640C94}.Debug|x86.Build.0 = Debug|Any CPU
87+
{6113FC36-F04E-B6CA-5C64-EA6156640C94}.Release|Any CPU.ActiveCfg = Release|Any CPU
88+
{6113FC36-F04E-B6CA-5C64-EA6156640C94}.Release|Any CPU.Build.0 = Release|Any CPU
89+
{6113FC36-F04E-B6CA-5C64-EA6156640C94}.Release|x64.ActiveCfg = Release|Any CPU
90+
{6113FC36-F04E-B6CA-5C64-EA6156640C94}.Release|x64.Build.0 = Release|Any CPU
91+
{6113FC36-F04E-B6CA-5C64-EA6156640C94}.Release|x86.ActiveCfg = Release|Any CPU
92+
{6113FC36-F04E-B6CA-5C64-EA6156640C94}.Release|x86.Build.0 = Release|Any CPU
93+
{893AD0F6-7E7E-8550-56D7-BBCB77933BD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
94+
{893AD0F6-7E7E-8550-56D7-BBCB77933BD3}.Debug|Any CPU.Build.0 = Debug|Any CPU
95+
{893AD0F6-7E7E-8550-56D7-BBCB77933BD3}.Debug|x64.ActiveCfg = Debug|Any CPU
96+
{893AD0F6-7E7E-8550-56D7-BBCB77933BD3}.Debug|x64.Build.0 = Debug|Any CPU
97+
{893AD0F6-7E7E-8550-56D7-BBCB77933BD3}.Debug|x86.ActiveCfg = Debug|Any CPU
98+
{893AD0F6-7E7E-8550-56D7-BBCB77933BD3}.Debug|x86.Build.0 = Debug|Any CPU
99+
{893AD0F6-7E7E-8550-56D7-BBCB77933BD3}.Release|Any CPU.ActiveCfg = Release|Any CPU
100+
{893AD0F6-7E7E-8550-56D7-BBCB77933BD3}.Release|Any CPU.Build.0 = Release|Any CPU
101+
{893AD0F6-7E7E-8550-56D7-BBCB77933BD3}.Release|x64.ActiveCfg = Release|Any CPU
102+
{893AD0F6-7E7E-8550-56D7-BBCB77933BD3}.Release|x64.Build.0 = Release|Any CPU
103+
{893AD0F6-7E7E-8550-56D7-BBCB77933BD3}.Release|x86.ActiveCfg = Release|Any CPU
104+
{893AD0F6-7E7E-8550-56D7-BBCB77933BD3}.Release|x86.Build.0 = Release|Any CPU
105+
{2A1FC5B6-EB87-C86A-71BB-42784913627C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
106+
{2A1FC5B6-EB87-C86A-71BB-42784913627C}.Debug|Any CPU.Build.0 = Debug|Any CPU
107+
{2A1FC5B6-EB87-C86A-71BB-42784913627C}.Debug|x64.ActiveCfg = Debug|Any CPU
108+
{2A1FC5B6-EB87-C86A-71BB-42784913627C}.Debug|x64.Build.0 = Debug|Any CPU
109+
{2A1FC5B6-EB87-C86A-71BB-42784913627C}.Debug|x86.ActiveCfg = Debug|Any CPU
110+
{2A1FC5B6-EB87-C86A-71BB-42784913627C}.Debug|x86.Build.0 = Debug|Any CPU
111+
{2A1FC5B6-EB87-C86A-71BB-42784913627C}.Release|Any CPU.ActiveCfg = Release|Any CPU
112+
{2A1FC5B6-EB87-C86A-71BB-42784913627C}.Release|Any CPU.Build.0 = Release|Any CPU
113+
{2A1FC5B6-EB87-C86A-71BB-42784913627C}.Release|x64.ActiveCfg = Release|Any CPU
114+
{2A1FC5B6-EB87-C86A-71BB-42784913627C}.Release|x64.Build.0 = Release|Any CPU
115+
{2A1FC5B6-EB87-C86A-71BB-42784913627C}.Release|x86.ActiveCfg = Release|Any CPU
116+
{2A1FC5B6-EB87-C86A-71BB-42784913627C}.Release|x86.Build.0 = Release|Any CPU
117+
{BB512649-8BE2-38B1-49D1-E8CA8701BA1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
118+
{BB512649-8BE2-38B1-49D1-E8CA8701BA1A}.Debug|Any CPU.Build.0 = Debug|Any CPU
119+
{BB512649-8BE2-38B1-49D1-E8CA8701BA1A}.Debug|x64.ActiveCfg = Debug|Any CPU
120+
{BB512649-8BE2-38B1-49D1-E8CA8701BA1A}.Debug|x64.Build.0 = Debug|Any CPU
121+
{BB512649-8BE2-38B1-49D1-E8CA8701BA1A}.Debug|x86.ActiveCfg = Debug|Any CPU
122+
{BB512649-8BE2-38B1-49D1-E8CA8701BA1A}.Debug|x86.Build.0 = Debug|Any CPU
123+
{BB512649-8BE2-38B1-49D1-E8CA8701BA1A}.Release|Any CPU.ActiveCfg = Release|Any CPU
124+
{BB512649-8BE2-38B1-49D1-E8CA8701BA1A}.Release|Any CPU.Build.0 = Release|Any CPU
125+
{BB512649-8BE2-38B1-49D1-E8CA8701BA1A}.Release|x64.ActiveCfg = Release|Any CPU
126+
{BB512649-8BE2-38B1-49D1-E8CA8701BA1A}.Release|x64.Build.0 = Release|Any CPU
127+
{BB512649-8BE2-38B1-49D1-E8CA8701BA1A}.Release|x86.ActiveCfg = Release|Any CPU
128+
{BB512649-8BE2-38B1-49D1-E8CA8701BA1A}.Release|x86.Build.0 = Release|Any CPU
129+
{DDD4236E-F481-4DE9-306C-66E77A8D2CB9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
130+
{DDD4236E-F481-4DE9-306C-66E77A8D2CB9}.Debug|Any CPU.Build.0 = Debug|Any CPU
131+
{DDD4236E-F481-4DE9-306C-66E77A8D2CB9}.Debug|x64.ActiveCfg = Debug|Any CPU
132+
{DDD4236E-F481-4DE9-306C-66E77A8D2CB9}.Debug|x64.Build.0 = Debug|Any CPU
133+
{DDD4236E-F481-4DE9-306C-66E77A8D2CB9}.Debug|x86.ActiveCfg = Debug|Any CPU
134+
{DDD4236E-F481-4DE9-306C-66E77A8D2CB9}.Debug|x86.Build.0 = Debug|Any CPU
135+
{DDD4236E-F481-4DE9-306C-66E77A8D2CB9}.Release|Any CPU.ActiveCfg = Release|Any CPU
136+
{DDD4236E-F481-4DE9-306C-66E77A8D2CB9}.Release|Any CPU.Build.0 = Release|Any CPU
137+
{DDD4236E-F481-4DE9-306C-66E77A8D2CB9}.Release|x64.ActiveCfg = Release|Any CPU
138+
{DDD4236E-F481-4DE9-306C-66E77A8D2CB9}.Release|x64.Build.0 = Release|Any CPU
139+
{DDD4236E-F481-4DE9-306C-66E77A8D2CB9}.Release|x86.ActiveCfg = Release|Any CPU
140+
{DDD4236E-F481-4DE9-306C-66E77A8D2CB9}.Release|x86.Build.0 = Release|Any CPU
141+
{9C18F2D1-E9E9-613A-DE10-4263A2EA8A17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
142+
{9C18F2D1-E9E9-613A-DE10-4263A2EA8A17}.Debug|Any CPU.Build.0 = Debug|Any CPU
143+
{9C18F2D1-E9E9-613A-DE10-4263A2EA8A17}.Debug|x64.ActiveCfg = Debug|Any CPU
144+
{9C18F2D1-E9E9-613A-DE10-4263A2EA8A17}.Debug|x64.Build.0 = Debug|Any CPU
145+
{9C18F2D1-E9E9-613A-DE10-4263A2EA8A17}.Debug|x86.ActiveCfg = Debug|Any CPU
146+
{9C18F2D1-E9E9-613A-DE10-4263A2EA8A17}.Debug|x86.Build.0 = Debug|Any CPU
147+
{9C18F2D1-E9E9-613A-DE10-4263A2EA8A17}.Release|Any CPU.ActiveCfg = Release|Any CPU
148+
{9C18F2D1-E9E9-613A-DE10-4263A2EA8A17}.Release|Any CPU.Build.0 = Release|Any CPU
149+
{9C18F2D1-E9E9-613A-DE10-4263A2EA8A17}.Release|x64.ActiveCfg = Release|Any CPU
150+
{9C18F2D1-E9E9-613A-DE10-4263A2EA8A17}.Release|x64.Build.0 = Release|Any CPU
151+
{9C18F2D1-E9E9-613A-DE10-4263A2EA8A17}.Release|x86.ActiveCfg = Release|Any CPU
152+
{9C18F2D1-E9E9-613A-DE10-4263A2EA8A17}.Release|x86.Build.0 = Release|Any CPU
153+
{A4D0B498-A934-2F11-2AA7-18D1D1C9E881}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
154+
{A4D0B498-A934-2F11-2AA7-18D1D1C9E881}.Debug|Any CPU.Build.0 = Debug|Any CPU
155+
{A4D0B498-A934-2F11-2AA7-18D1D1C9E881}.Debug|x64.ActiveCfg = Debug|Any CPU
156+
{A4D0B498-A934-2F11-2AA7-18D1D1C9E881}.Debug|x64.Build.0 = Debug|Any CPU
157+
{A4D0B498-A934-2F11-2AA7-18D1D1C9E881}.Debug|x86.ActiveCfg = Debug|Any CPU
158+
{A4D0B498-A934-2F11-2AA7-18D1D1C9E881}.Debug|x86.Build.0 = Debug|Any CPU
159+
{A4D0B498-A934-2F11-2AA7-18D1D1C9E881}.Release|Any CPU.ActiveCfg = Release|Any CPU
160+
{A4D0B498-A934-2F11-2AA7-18D1D1C9E881}.Release|Any CPU.Build.0 = Release|Any CPU
161+
{A4D0B498-A934-2F11-2AA7-18D1D1C9E881}.Release|x64.ActiveCfg = Release|Any CPU
162+
{A4D0B498-A934-2F11-2AA7-18D1D1C9E881}.Release|x64.Build.0 = Release|Any CPU
163+
{A4D0B498-A934-2F11-2AA7-18D1D1C9E881}.Release|x86.ActiveCfg = Release|Any CPU
164+
{A4D0B498-A934-2F11-2AA7-18D1D1C9E881}.Release|x86.Build.0 = Release|Any CPU
165+
{01C9F662-6378-3FC4-B629-9FD37A38033D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
166+
{01C9F662-6378-3FC4-B629-9FD37A38033D}.Debug|Any CPU.Build.0 = Debug|Any CPU
167+
{01C9F662-6378-3FC4-B629-9FD37A38033D}.Debug|x64.ActiveCfg = Debug|Any CPU
168+
{01C9F662-6378-3FC4-B629-9FD37A38033D}.Debug|x64.Build.0 = Debug|Any CPU
169+
{01C9F662-6378-3FC4-B629-9FD37A38033D}.Debug|x86.ActiveCfg = Debug|Any CPU
170+
{01C9F662-6378-3FC4-B629-9FD37A38033D}.Debug|x86.Build.0 = Debug|Any CPU
171+
{01C9F662-6378-3FC4-B629-9FD37A38033D}.Release|Any CPU.ActiveCfg = Release|Any CPU
172+
{01C9F662-6378-3FC4-B629-9FD37A38033D}.Release|Any CPU.Build.0 = Release|Any CPU
173+
{01C9F662-6378-3FC4-B629-9FD37A38033D}.Release|x64.ActiveCfg = Release|Any CPU
174+
{01C9F662-6378-3FC4-B629-9FD37A38033D}.Release|x64.Build.0 = Release|Any CPU
175+
{01C9F662-6378-3FC4-B629-9FD37A38033D}.Release|x86.ActiveCfg = Release|Any CPU
176+
{01C9F662-6378-3FC4-B629-9FD37A38033D}.Release|x86.Build.0 = Release|Any CPU
49177
EndGlobalSection
50178
GlobalSection(SolutionProperties) = preSolution
51179
HideSolutionNode = FALSE
52180
EndGlobalSection
181+
GlobalSection(NestedProjects) = preSolution
182+
{C022147F-DA63-5B3C-9349-FEB9675362DF} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
183+
{6113FC36-F04E-B6CA-5C64-EA6156640C94} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
184+
{893AD0F6-7E7E-8550-56D7-BBCB77933BD3} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
185+
{2A1FC5B6-EB87-C86A-71BB-42784913627C} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
186+
{BB512649-8BE2-38B1-49D1-E8CA8701BA1A} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
187+
{DDD4236E-F481-4DE9-306C-66E77A8D2CB9} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
188+
{9C18F2D1-E9E9-613A-DE10-4263A2EA8A17} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
189+
{A4D0B498-A934-2F11-2AA7-18D1D1C9E881} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
190+
{01C9F662-6378-3FC4-B629-9FD37A38033D} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
191+
EndGlobalSection
53192
GlobalSection(ExtensibilityGlobals) = postSolution
54193
SolutionGuid = {E732CE09-693A-4EB7-BC1C-34E0D4E2E19F}
55194
EndGlobalSection

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void ConfigureServices(IServiceCollection services)
9393

9494
```
9595
# Using GotenbergSharpClient
96-
*See the [linqPad folder](linqpad/)* for complete examples.
96+
*See the [examples folder](examples/)* for complete working examples as console applications.
9797

9898
## Required Using Statements
9999
```csharp
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/DIExample/Program.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using Gotenberg.Sharp.API.Client;
2+
using Gotenberg.Sharp.API.Client.Domain.Builders;
3+
using Gotenberg.Sharp.API.Client.Domain.Builders.Faceted;
4+
using Gotenberg.Sharp.API.Client.Domain.Requests;
5+
using Gotenberg.Sharp.API.Client.Domain.Settings;
6+
using Gotenberg.Sharp.API.Client.Extensions;
7+
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.Extensions.Logging;
11+
12+
// Builds a simple DI container with logging enabled.
13+
// Client retrieved through the service provider is configured with options defined in appsettings.json
14+
// Watch the polly-retry policy in action:
15+
// Turn off gotenberg, run this script and let it fail/retry two or three times.
16+
// Turn gotenberg back on & the request will successfully complete.
17+
// Example builds a 1 page PDF from the specified TargetUrl
18+
19+
const string TargetUrl = "https://www.cnn.com";
20+
var saveToPath = args.Length > 0 ? args[0] : Path.Combine(Directory.GetCurrentDirectory(), "output");
21+
Directory.CreateDirectory(saveToPath);
22+
23+
var services = BuildServiceCollection();
24+
var sp = services.BuildServiceProvider();
25+
26+
var sharpClient = sp.GetRequiredService<GotenbergSharpClient>();
27+
var request = await CreateUrlRequest();
28+
var response = await sharpClient.UrlToPdfAsync(request);
29+
30+
var resultPath = Path.Combine(saveToPath, $"GotenbergFromUrl-{DateTime.Now:yyyyMMddHHmmss}.pdf");
31+
32+
await using (var destinationStream = File.Create(resultPath))
33+
{
34+
await response.CopyToAsync(destinationStream, CancellationToken.None);
35+
}
36+
37+
Console.WriteLine($"PDF created: {resultPath}");
38+
39+
IServiceCollection BuildServiceCollection()
40+
{
41+
var config = new ConfigurationBuilder()
42+
.SetBasePath(AppContext.BaseDirectory)
43+
.AddJsonFile("appsettings.json")
44+
.Build();
45+
46+
return new ServiceCollection()
47+
.AddOptions<GotenbergSharpClientOptions>()
48+
.Bind(config.GetSection(nameof(GotenbergSharpClient))).Services
49+
.AddGotenbergSharpClient()
50+
.Services.AddLogging(s => s.AddSimpleConsole(ops =>
51+
{
52+
ops.IncludeScopes = true;
53+
ops.SingleLine = false;
54+
ops.TimestampFormat = "hh:mm:ss ";
55+
}));
56+
}
57+
58+
Task<UrlRequest> CreateUrlRequest()
59+
{
60+
var builder = new UrlRequestBuilder()
61+
.SetUrl(TargetUrl)
62+
.ConfigureRequest(b => b.SetPageRanges("1-2"))
63+
.WithPageProperties(b =>
64+
{
65+
b.SetPaperSize(PaperSizes.A4)
66+
.SetMargins(Margins.None);
67+
});
68+
69+
return builder.BuildAsync();
70+
}

examples/Directory.Build.props

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project>
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\src\Gotenberg.Sharp.Api.Client\Gotenberg.Sharp.Api.Client.csproj" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
15+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
16+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
17+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
18+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
19+
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
20+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<None Include="..\appsettings.json" Link="appsettings.json">
25+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
26+
</None>
27+
<None Include="..\resources\**\*.*" Link="resources\%(RecursiveDir)%(Filename)%(Extension)">
28+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
29+
</None>
30+
</ItemGroup>
31+
</Project>
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/HtmlConvert/Program.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 resourcePath = Path.Combine(AppContext.BaseDirectory, "resources", "Html", "ConvertExample");
20+
var path = await CreateFromHtml(destinationDirectory, resourcePath, options);
21+
22+
Console.WriteLine($"PDF created: {path}");
23+
24+
static async Task<string> CreateFromHtml(string destinationDirectory, string resourcePath, GotenbergSharpClientOptions options)
25+
{
26+
using var handler = new HttpClientHandler();
27+
using var authHandler = !string.IsNullOrWhiteSpace(options.BasicAuthUsername) && !string.IsNullOrWhiteSpace(options.BasicAuthPassword)
28+
? new BasicAuthHandler(options.BasicAuthUsername, options.BasicAuthPassword) { InnerHandler = handler }
29+
: null;
30+
31+
using var httpClient = new HttpClient(authHandler ?? (HttpMessageHandler)handler)
32+
{
33+
BaseAddress = options.ServiceUrl,
34+
Timeout = options.TimeOut
35+
};
36+
37+
var sharpClient = new GotenbergSharpClient(httpClient);
38+
39+
var builder = new HtmlRequestBuilder()
40+
.AddAsyncDocument(async doc =>
41+
doc.SetBody(await GetHtmlFile(resourcePath, "body.html"))
42+
.SetFooter(await GetHtmlFile(resourcePath, "footer.html"))
43+
).WithPageProperties(dims => dims.UseChromeDefaults())
44+
.WithAsyncAssets(async assets =>
45+
assets.AddItem("ear-on-beach.jpg", await GetImageBytes(resourcePath))
46+
)
47+
.SetConversionBehaviors(b =>
48+
b.AddAdditionalHeaders("hello", "from-earth")
49+
)
50+
.ConfigureRequest(b => b.SetPageRanges("1"));
51+
52+
var request = await builder.BuildAsync();
53+
54+
var resultPath = Path.Combine(destinationDirectory, $"GotenbergFromHtml-{DateTime.Now:yyyyMMddHHmmss}.pdf");
55+
var response = await sharpClient.HtmlToPdfAsync(request);
56+
57+
await using var destinationStream = File.Create(resultPath);
58+
await response.CopyToAsync(destinationStream, CancellationToken.None);
59+
60+
return resultPath;
61+
}
62+
63+
static Task<byte[]> GetImageBytes(string resourcePath)
64+
{
65+
return File.ReadAllBytesAsync(Path.Combine(resourcePath, "ear-on-beach.jpg"));
66+
}
67+
68+
static Task<byte[]> GetHtmlFile(string resourcePath, string fileName)
69+
{
70+
return File.ReadAllBytesAsync(Path.Combine(resourcePath, fileName));
71+
}
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)