From 50441e7622a672a338af563d6d5b69230c630811 Mon Sep 17 00:00:00 2001 From: ancplua Date: Thu, 4 Jun 2026 12:49:48 +0200 Subject: [PATCH] feat: add Document.ToBytesAsync() for in-memory PDF output (3.0.5) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds an in-memory output to the producer side: Pdf.Create(...).AddText(...).ToBytesAsync() returns the PDF bytes without touching disk. PdfWriter was already stream-first (PdfWriter(Stream,...), _buffer.CopyToAsync(_output), and it never disposes _output), so this is pure surface. SaveAsync(string) is reimplemented on top of ToBytesAsync, so the existing SaveAsync tests cover the new method transitively — 100% coverage preserved, no behavior change. Motivation: consumers that need the bytes (e.g. uploading to object storage, building multipart content in tests) currently round-trip through disk; ToBytesAsync removes that. Co-Authored-By: Claude Opus 4.8 (1M context) --- CreatePdf.NET/CreatePdf.NET.csproj | 2 +- CreatePdf.NET/Document.cs | 30 +++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/CreatePdf.NET/CreatePdf.NET.csproj b/CreatePdf.NET/CreatePdf.NET.csproj index 8670f06..4c676ed 100644 --- a/CreatePdf.NET/CreatePdf.NET.csproj +++ b/CreatePdf.NET/CreatePdf.NET.csproj @@ -7,7 +7,7 @@ CreatePdf.NET CreatePdf.NET - Simple PDF Creation Library - 3.0.4 + 3.0.5 Alexander Nachtmann Lightweight PDF generation library for .NET 10/9/8 with text rendering, bitmap graphics, and OCR CreatePdf.NET is a simple, fast PDF creation library for .NET applications. Generate PDF documents with text rendering, bitmap graphics, and optional OCR text extraction. Lightweight, dependency-free, 100% test coverage. Perfect for creating reports, invoices, receipts, labels, and documents. Supports .NET 10, .NET 9, and .NET 8 with fluent C# API. diff --git a/CreatePdf.NET/Document.cs b/CreatePdf.NET/Document.cs index 020fcc1..34d66b1 100644 --- a/CreatePdf.NET/Document.cs +++ b/CreatePdf.NET/Document.cs @@ -202,21 +202,37 @@ public async Task SaveAndShowDirectoryAsync(string? filename = null) } /// - /// Saves the document to a PDF file. + /// Renders the document to an in-memory PDF and returns its bytes, without touching the disk. /// - /// Optional filename. If not provided, generates a timestamped name. - /// The full path to the saved PDF file. - public async Task SaveAsync(string? filename = null) + /// The complete PDF document as a byte array. + /// + /// + /// byte[] pdf = await Pdf.Create(Dye.White) + /// .AddText("Hello World") + /// .ToBytesAsync(); + /// + /// + public async Task ToBytesAsync() { - var path = FileOperations.GetOutputPath(filename); - - await using var stream = File.Create(path); + await using var stream = new MemoryStream(); await using var writer = new PdfWriter(stream, _background); foreach (var content in _contents) content.Render(writer); await writer.FinalizeAsync(); + return stream.ToArray(); + } + + /// + /// Saves the document to a PDF file. + /// + /// Optional filename. If not provided, generates a timestamped name. + /// The full path to the saved PDF file. + public async Task SaveAsync(string? filename = null) + { + var path = FileOperations.GetOutputPath(filename); + await File.WriteAllBytesAsync(path, await ToBytesAsync()); return path; }