Export: Streams & Async
Every SaveAs* method has a Stream overload, so you can return a document from a web endpoint, upload it to blob storage, or attach it to an email without a temp file. VDocs never closes or disposes the stream you pass in. The path overloads simply wrap these.
C#
1using Vega;
2
3// ASP.NET Core minimal API: write asynchronously to the response body
4app.MapGet("/invoice.pdf", async (HttpContext ctx) =>
5{
6 var doc = BuildInvoice();
7 ctx.Response.ContentType = "application/pdf";
8 await doc.SaveAsPdfAsync(ctx.Response.Body, ctx.RequestAborted);
9});
10
11// Bytes, e.g. for an email attachment or blob upload
12byte[] pdf = doc.ToPdfBytes();
13byte[] docx = doc.ToWordBytes();
14
15// Open from a stream (Word)
16using var upload = File.OpenRead("report.docx");
17var opened = await VDocs.OpenFromWordAsync(upload);
Rendering is CPU-bound, so the async methods render synchronously into a buffer and then write to your stream asynchronously (SaveAsWordAsync, SaveAsPdfAsync, SaveAsHtmlAsync, OpenFromWordAsync). Each accepts a CancellationToken.
Related API
Frequently asked questions
Can I generate documents in parallel?
Yes — use one VDocs instance per thread or task. A single instance is not thread-safe; don't share it between threads.