Use C# and HttpClient to generate PDFs from ASP.NET Core applications, services, workers, or console apps.
Post JSON with the built-in .NET HTTP APIs, validate the response, and save or return the generated PDF as binary data.
The API accepts a JSON request body. Send either a public URL or inline HTML in the required html parameter. For normal synchronous conversions, the response body is the generated PDF and should be saved as binary data.
Replace <your-api-key> or the environment variable with your html2pdf.app API key, then run the example from a trusted backend environment.
Send a public URL to the API, check the HTTP response, and save the returned bytes to document.pdf.
using System.Net.Http.Json;
const string endpoint = "https://api.html2pdf.app/v1/generate";
var apiKey = Environment.GetEnvironmentVariable("HTML2PDF_API_KEY")
?? throw new InvalidOperationException("HTML2PDF_API_KEY is not set.");
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", apiKey);
using var response = await client.PostAsJsonAsync(endpoint, new
{
html = "https://www.example.com",
});
if (!response.IsSuccessStatusCode)
{
var error = await response.Content.ReadAsStringAsync();
throw new HttpRequestException("PDF generation failed: " + error);
}
var pdf = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync("document.pdf", pdf);These examples cover the next common production flows: generating from inline HTML with options and queuing a callback job.
Generate an invoice with layout options and return it directly from a minimal API endpoint.
using System.Net.Http.Json;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpClient();
var app = builder.Build();
app.MapGet("/invoice.pdf", async (
IHttpClientFactory clientFactory,
IConfiguration configuration) =>
{
var apiKey = configuration["HTML2PDF_API_KEY"]
?? throw new InvalidOperationException("HTML2PDF_API_KEY is not set.");
var client = clientFactory.CreateClient();
client.DefaultRequestHeaders.Add("X-API-Key", apiKey);
using var response = await client.PostAsJsonAsync("https://api.html2pdf.app/v1/generate", new
{
html = "<h1>Invoice</h1><p>Total: $240.00</p>",
format = "A4",
media = "print",
marginTop = 40,
marginRight = 32,
marginBottom = 40,
marginLeft = 32,
filename = "invoice.pdf",
});
IResult result;
if (!response.IsSuccessStatusCode)
{
var error = await response.Content.ReadAsStringAsync();
result = Results.Problem("PDF generation failed: " + error);
}
else
{
var pdf = await response.Content.ReadAsByteArrayAsync();
result = Results.File(pdf, "application/pdf", "invoice.pdf");
}
return result;
});
app.Run();Provide callback details when the generated PDF should be delivered to a webhook instead of returned immediately.
using System.Net.Http.Json;
const string endpoint = "https://api.html2pdf.app/v1/generate";
var apiKey = Environment.GetEnvironmentVariable("HTML2PDF_API_KEY")
?? throw new InvalidOperationException("HTML2PDF_API_KEY is not set.");
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", apiKey);
using var response = await client.PostAsJsonAsync(endpoint, new
{
html = "https://www.example.com/report",
callBackUrl = "https://your-app.com/webhooks/html2pdf",
state = "report-1042",
});
if (!response.IsSuccessStatusCode)
{
var error = await response.Content.ReadAsStringAsync();
throw new HttpRequestException("Unable to queue PDF conversion: " + error);
}Most integrations start with html and then add options as the document design becomes more specific.
html is required and accepts a public URL or raw HTML.format, landscape, width, and height control page size.marginTop, marginRight, marginBottom, and marginLeft control whitespace around the rendered page.media selects screen or print CSS rendering.callBackUrl switches the request to asynchronous generation and sends the completed PDF to your webhook.See the full API parameter reference for every supported field.
If the generated document is blank or missing styles, first confirm that the source URL is public and that required CSS, fonts, and images are reachable by the rendering service.