.NET C# PDF Generation SDK
The official cloudlayerio-dotnet package provides C# PDF generation for .NET 6+ applications. Convert HTML to PDF, capture URLs as documents, and render templates — available on NuGet with full IntelliSense and typed request/response models.
- NuGet Package: cloudlayerio-dotnet
- Source Code: github.com/cloudlayerio/cloudlayerio-dotnet
Installation
Visual Studio
Using the Package Manager Console:
Install-Package cloudlayerio-dotnet
Or search for cloudlayerio-dotnet in the NuGet Package Manager.
JetBrains Rider
See JetBrains NuGet documentation.
.NET CLI
dotnet add package cloudlayerio-dotnet
Setup
Get your API key by creating a free account at cloudlayer.io. Your account comes with free API credits for testing.
Initialize the manager with your API key:
var manager = new CloudlayerioManager("<YOUR-API-KEY>");
URL to PDF
var rsp = await manager.UrlToPdf(new UrlToPdf
{
Url = "https://example.com",
Async = false
});
// Save the JSON response to the filesystem
await rsp.SaveToFileSystem("C:\\output\\example.json");
// Access the asset URL (available when async is false)
var url = rsp.Response.AssetUrl;
URL to Image
var rsp = await manager.UrlToImage(new UrlToImage
{
Url = "https://example.com",
Async = false,
AutoScroll = true,
ViewPort = new ViewPort
{
Width = 1440,
Height = 900,
DeviceScaleFactor = 2
}
});
var url = rsp.Response.AssetUrl;
HTML to PDF
// HTML must be base64-encoded
var html = Convert.ToBase64String(
Encoding.UTF8.GetBytes("<html><body><h1>Hello!</h1></body></html>")
);
var rsp = await manager.HtmlToPdf(new HtmlToPdf
{
Html = html
});
await rsp.SaveToFileSystem("C:\\output\\hello.json");
Template to PDF
var rsp = await manager.TemplateToPdf(new TemplateToPdf
{
TemplateId = "professional-invoice",
Data = new Dictionary<string, object>
{
["company_name"] = "Acme Inc.",
["invoice_no"] = "INV-001",
["locale"] = "en-US",
["currency"] = "USD",
["items"] = new[]
{
new Dictionary<string, object>
{
["title"] = "Web Design",
["quantity"] = 10,
["unit_price"] = 150.00,
["amount"] = null
}
}
}
});
await rsp.SaveToFileSystem("C:\\output\\invoice.json");
Synchronous vs Asynchronous
By default, requests are processed asynchronously. The response is delivered to your configured webhook. To get the result immediately in the response, set Async = false:
var rsp = await manager.UrlToPdf(new UrlToPdf
{
Url = "https://example.com",
Async = false
});
// AssetUrl is populated in the synchronous response
var url = rsp.Response.AssetUrl;
Note: For long-running requests (large documents, batch processing), use the default async mode with webhooks to avoid connection timeouts.
Advanced Options
The SDK provides typed classes for all API options, with full IntelliSense support.
Custom Margins
var rsp = await manager.UrlToPdf(new UrlToPdf
{
Url = "https://example.com",
Margin = new Margin
{
Top = new LayoutDimension(UnitTypes.Pixels, 100),
Bottom = new LayoutDimension(UnitTypes.Pixels, 100),
Left = new LayoutDimension(UnitTypes.Pixels, 50),
Right = new LayoutDimension(UnitTypes.Pixels, 50)
}
});
Header and Footer Templates
var rsp = await manager.UrlToPdf(new UrlToPdf
{
Url = "https://example.com",
Margin = new Margin
{
Top = new LayoutDimension(UnitTypes.Pixels, 120),
Bottom = new LayoutDimension(UnitTypes.Pixels, 80)
},
HeaderTemplate = new HeaderFooterTemplate
{
Method = "extract",
Selector = ".page-header",
Style = new Dictionary<string, string>
{
["width"] = "100%",
["text-align"] = "center"
}
},
FooterTemplate = new HeaderFooterTemplate
{
Selector = ".page-footer",
Style = new Dictionary<string, string>
{
["width"] = "100%",
["font-size"] = "10px",
["text-align"] = "center"
}
}
});
Viewport Configuration
var rsp = await manager.UrlToImage(new UrlToImage
{
Url = "https://example.com",
ViewPort = new ViewPort
{
Width = 1920,
Height = 1080,
DeviceScaleFactor = 2,
IsMobile = false,
IsLandscape = true
}
});
Response Format
As of v2, the API returns a JSON response rather than binary content. The SaveToFileSystem helper saves this JSON response to disk. The response object is fully typed with properties including:
| Property | Type | Description |
|---|---|---|
AssetUrl | string | URL to the generated asset (populated for sync calls) |
Status | string | Job status (success, error, etc.) |
JobId | string | Unique identifier for the generation job |
For the full set of response properties and request options, see the source code.