Skip to content

Getting Started

Working Example

DIExample — Full DI setup with logging and a retry

Prerequisites

  • Docker for running Gotenberg 7, 8 or 9. The client reads the version of the service and sends each request in the form that version takes; see Gotenberg 9
  • .NET 8.0+, or any runtime that supports .NET Standard 2.0 or 2.1 (the package also targets .NET 9 and 10)

Running Gotenberg

Docker Run

docker pull gotenberg/gotenberg:9
docker run --name gotenberg --rm -p 3000:3000 gotenberg/gotenberg:9 gotenberg --api-timeout=1800s --log-level=debug

Docker Compose (with Basic Auth)

docker-compose -f docker/docker-compose-basic-auth.yml up -d

Pre-configured with test credentials: testuser / testpass.

Install the NuGet Package

dotnet add package Gotenberg.Sharp.Api.Client

Configure Services

Basic (appsettings.json)

{
  "GotenbergSharpClient": {
    "ServiceUrl": "http://localhost:3000",
    "HealthCheckUrl": "http://localhost:3000/health"
  }
}
public void ConfigureServices(IServiceCollection services)
{
    services.AddOptions<GotenbergSharpClientOptions>()
        .Bind(Configuration.GetSection("GotenbergSharpClient"));
    services.AddGotenbergSharpClient();
}

With Basic Authentication

{
  "GotenbergSharpClient": {
    "ServiceUrl": "http://localhost:3000",
    "BasicAuthUsername": "your-username",
    "BasicAuthPassword": "your-password"
  }
}

Programmatic Configuration

services.AddOptions<GotenbergSharpClientOptions>()
    .Configure(options =>
    {
        options.ServiceUrl = new Uri("http://localhost:3000");
        options.TimeOut = TimeSpan.FromMinutes(5);
        options.BasicAuthUsername = "username";
        options.BasicAuthPassword = "password";
    });
services.AddGotenbergSharpClient();

Hybrid (appsettings + overrides)

services.AddOptions<GotenbergSharpClientOptions>()
    .Bind(Configuration.GetSection("GotenbergSharpClient"))
    .PostConfigure(options =>
    {
        options.BasicAuthUsername = Environment.GetEnvironmentVariable("GOTENBERG_USER");
        options.BasicAuthPassword = Environment.GetEnvironmentVariable("GOTENBERG_PASS");
    });
services.AddGotenbergSharpClient();

Retries

The client sends each request once. To retry failed requests, add a resilience handler to the builder that AddGotenbergSharpClient() returns; Retries and Timeouts shows how, and which failures are worth retrying.

Options Validation

Settings that could never work throw an OptionsValidationException when the options are first read:

  • ServiceUrl must be an absolute http or https URL
  • TimeOut must be positive, or Timeout.InfiniteTimeSpan
  • RetryPolicy.Enabled must not be true: the client has no built-in retry since 4.0 (see Retries and Timeouts)

Add ValidateOnStart() to fail at application start-up instead of on the first conversion:

services.AddOptions<GotenbergSharpClientOptions>()
    .Bind(Configuration.GetSection("GotenbergSharpClient"))
    .ValidateOnStart();
services.AddGotenbergSharpClient();

Required Using Statements

using Gotenberg.Sharp.API.Client;
using Gotenberg.Sharp.API.Client.Application.Builders;
using Gotenberg.Sharp.API.Client.Domain.Settings;
using Gotenberg.Sharp.API.Client.Extensions;

Your First PDF

public async Task<Stream> CreatePdf([FromServices] GotenbergSharpClient sharpClient)
{
    var builder = new HtmlRequestBuilder()
        .AddDocument(doc => doc.SetBody("<html><body><h1>Hello PDF!</h1></body></html>"))
        .WithPageProperties(pp => pp.UseChromeDefaults());

    return await sharpClient.HtmlToPdfAsync(builder);
}