Class CobaltPdfServiceExtensions
- Namespace
- CobaltPdf
- Assembly
- CobaltPdf.dll
Extension methods for registering CobaltPdf with the ASP.NET Core dependency injection container.
public static class CobaltPdfServiceExtensions
- Inheritance
-
CobaltPdfServiceExtensions
- Inherited Members
Methods
AddCobaltPdf(IServiceCollection, Action<PoolOptions>?)
Registers CobaltPdf with the dependency injection container.
public static IServiceCollection AddCobaltPdf(this IServiceCollection services, Action<PoolOptions>? configure = null)
Parameters
servicesIServiceCollectionThe service collection.
configureAction<PoolOptions>Optional action to configure the browser pool.
Returns
Examples
// Program.cs
builder.Services.AddCobaltPdf(o => {
o.MinSize = 2;
o.MaxSize = Environment.ProcessorCount;
o.ExtraArgs.Add("--no-sandbox");
});
// In a service or controller
public class ReportService(CobaltEngine renderer)
{
public async Task<byte[]> GenerateAsync(string url)
{
var pdf = await renderer.RenderUrlAsPdfAsync(url);
return pdf.BinaryData;
}
}
Remarks
This registers CobaltEngine as a singleton — one shared instance for the lifetime of the application, backed by the global browser pool.
Call this once in Program.cs / Startup.cs before app.Run(),
then inject CobaltEngine wherever you need to generate PDFs.
Also register a shutdown hook so the browser pool is cleaned up gracefully:
var lifetime = app.Services.GetRequiredService<IHostApplicationLifetime>();
lifetime.ApplicationStopping.Register(() =>
CobaltEngine.ShutdownAsync().GetAwaiter().GetResult());