rustedf.cloud
Menu

Save a PDF in C#: to disk, as a download or in blob storage

Published 2 min read

Once a PDF has been rendered, “save as PDF” is a question about where the bytes go: a file on disk, a download in the browser, a storage account, or a link you give to a customer. With the rustedf.cloud API the answer to a render request is the file itself, so each of these is a few lines. This guide shows them with the typed client from the C# and ASP.NET Core guide.

To a file on disk

byte[] pdf = await client.RenderAsync(templateId, data, "en-US", cancellationToken);
await File.WriteAllBytesAsync("invoice-2026-0042.pdf", pdf, cancellationToken);

For a large document, stream the response to the file instead of holding it in memory, and write to a temporary name first so a failed run never leaves half a file under the real one:

using var request = new HttpRequestMessage(HttpMethod.Post, "v1/render")
{
Content = JsonContent.Create(new { template_id = templateId, data }),
};
using var response = await http.SendAsync(
request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
response.EnsureSuccessStatusCode();
var temporary = path + ".part";
await using (var file = File.Create(temporary))
{
await response.Content.CopyToAsync(file, cancellationToken);
}
File.Move(temporary, path, overwrite: true);

As a download from ASP.NET Core

Give the file a name and the browser downloads it; leave the name out and the browser shows it in a tab.

app.MapGet("/invoices/{number}.pdf", async (string number, PdfClient pdf) =>
{
var file = await pdf.RenderAsync(invoiceTemplate, DataFor(number), "en-US");
return Results.File(file, "application/pdf", fileDownloadName: $"invoice-{number}.pdf");
});

If any part of the name comes from a user, clean it first. A name with a path or a line break in it is a bug at best.

Let the service keep it

When you only need to hand someone a link, ask for output: "url". The service keeps the PDF and answers with a signed address:

using System.Text.Json.Serialization;
using var response = await http.PostAsJsonAsync(
"v1/render", new { template_id = templateId, data, output = "url" }, cancellationToken);
response.EnsureSuccessStatusCode();
var link = await response.Content.ReadFromJsonAsync<RenderLink>(cancellationToken);
public sealed record RenderLink(
string Url,
[property: JsonPropertyName("expires_at")] long ExpiresAt,
int Pages);

The link works without an API key until expires_at, then stops, and how long files are kept depends on your plan. It is a way to deliver a document, not an archive: to keep it, download it once and store it yourself.

Into blob storage

The bytes go to any store. With Azure Blob Storage:

var blob = container.GetBlobClient($"invoices/{number}.pdf");
await blob.UploadAsync(
new BinaryData(pdf),
new BlobUploadOptions
{
HttpHeaders = new BlobHttpHeaders { ContentType = "application/pdf" },
},
cancellationToken);

Set the content type, so the browser knows what it opens when you hand out the blob address.

Do not save an error as a PDF

A failed render answers with JSON, not a PDF. Code that writes the response body to a .pdf without looking at the status leaves a file that no reader opens. Check first, as the typed client does, and read the code:

Status What to do
401 The key is missing, wrong or revoked. Fix the configuration.
402 No credits left. Nothing was rendered.
422 The template or its data failed. Read message.
429, 503 Wait for the Retry-After header and send the request again.
504 The document took longer than your plan allows.

A failed render costs no credit, so retrying 429, 503 and 5xx is safe.

Read Rendering a PDF for the request and the response, and Errors and limits for every code.

← All posts