Generate a PDF in .NET (C#)
The simplest way to generate a PDF in .NET (C#) is to call the Docweave API with HttpClient — POST your HTML to /api/v1/pdf and write the returned bytes to disk, with no headless browser to install or run yourself.
Generate a PDF in C# with HttpClient
POST your HTML to /api/v1/pdf with an Authorization: Bearer header, then write the response body to a file with File.WriteAllBytesAsync:
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
var html = "<h1>Hello from .NET</h1><p>Generated via Docweave.</p>";
var payload = new
{
source = new { type = "html", html },
options = new { format = "A4" },
idempotencyKey = "dotnet-quickstart-001"
};
using var client = new HttpClient();
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", "dw_live_your_key");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/pdf"));
var json = JsonSerializer.Serialize(payload);
using var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync("https://docweave.dev/api/v1/pdf", content);
response.EnsureSuccessStatusCode();
var pdfBytes = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync("output.pdf", pdfBytes);
Console.WriteLine($"Saved {pdfBytes.Length} bytes to output.pdf");…or from a .NET AI agent
The same orchestration is available over MCP. Run npx @docweave/mcp and any agent — including one built on .NET — gets a generate_pdf tool, no HTTP boilerplate required:
// From a .NET-based AI agent over MCP — no HttpClient plumbing:
generate_pdf({
source: {
type: "html",
html: "<h1>Hello from .NET</h1><p>Generated via Docweave.</p>"
},
outputPath: "/tmp/output.pdf"
})FAQ
How do I generate a PDF in .NET (C#) without a headless browser like PuppeteerSharp?
Call the Docweave API with HttpClient: POST HTML (or a URL) to /api/v1/pdf and write the returned bytes to disk. Docweave runs Chromium server-side, so there's no browser binary, PuppeteerSharp package, or Docker image to manage inside your .NET app.
Can I convert an existing Razor view or HTML string to a PDF in C#?
Yes — render your Razor view (or any HTML) to a string as you normally would, then send that string as source.html in the POST body. Docweave renders it with Chromium and returns the PDF bytes directly as the response body.
How do I avoid double-generating the same PDF on a retry in .NET?
Pass an idempotencyKey (for example an order or document ID) in the request body. Docweave dedupes on that key, so a retried HttpClient call after a timeout or transient failure returns the same cached document instead of rendering or billing twice.
Is there a NuGet SDK, or do I just use HttpClient?
There's no dedicated NuGet package — Docweave is a plain REST API, so HttpClient with System.Text.Json covers it in a few lines, as shown above. .NET-based AI agents can alternatively call the open-source generate_pdf MCP tool via npx @docweave/mcp.
Ready to generate PDFs from your .NET app?
Get an API key