Skip to content

feat: Document.ToBytesAsync() — in-memory PDF output (3.0.5)#10

Merged
ANcpLua merged 1 commit into
mainfrom
feat/in-memory-pdf-output
Jun 4, 2026
Merged

feat: Document.ToBytesAsync() — in-memory PDF output (3.0.5)#10
ANcpLua merged 1 commit into
mainfrom
feat/in-memory-pdf-output

Conversation

@ANcpLua

@ANcpLua ANcpLua commented Jun 4, 2026

Copy link
Copy Markdown
Owner

Adds an in-memory output to the producer side so callers can get a built PDF's bytes without going through disk:

byte[] pdf = await Pdf.Create(Dye.White).AddText("Hello World").ToBytesAsync();

PdfWriter was already stream-first (PdfWriter(Stream, …), _buffer.CopyToAsync(_output), and DisposeAsync never disposes _output), so this is pure surface — no rendering changes. SaveAsync(string) is reimplemented as ToBytesAsync() + File.WriteAllBytes, so:

  • The existing SaveAsync tests (11 call sites) cover ToBytesAsync transitively → 100% coverage preserved, no new test.
  • No observable behavior change to SaveAsync.

Version bumped to 3.0.5. Verified: dotnet test -c Release → 256/256 passed.

🤖 Generated with Claude Code

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) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 4, 2026 10:49
@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

@coderabbitai autofix

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an in-memory PDF output API (Document.ToBytesAsync) so callers can retrieve rendered PDF bytes without writing to disk, and bumps the package version to 3.0.5.

Changes:

  • Introduces Document.ToBytesAsync() to render a document into a byte[] using an in-memory stream.
  • Reimplements SaveAsync to write the output bytes to disk via File.WriteAllBytesAsync.
  • Updates package version from 3.0.4 → 3.0.5.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
CreatePdf.NET/Document.cs Adds ToBytesAsync() and rewires SaveAsync implementation.
CreatePdf.NET/CreatePdf.NET.csproj Bumps NuGet package version to 3.0.5.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread CreatePdf.NET/Document.cs
Comment on lines +232 to 237
public async Task<string> SaveAsync(string? filename = null)
{
var path = FileOperations.GetOutputPath(filename);
await File.WriteAllBytesAsync(path, await ToBytesAsync());
return path;
}

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 50441e7622

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread CreatePdf.NET/Document.cs
public async Task<string> SaveAsync(string? filename = null)
{
var path = FileOperations.GetOutputPath(filename);
await File.WriteAllBytesAsync(path, await ToBytesAsync());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep SaveAsync streaming to the file

For large bitmap-heavy documents, routing SaveAsync through ToBytesAsync substantially raises peak memory: PdfWriter already buffers the whole PDF internally, then ToBytesAsync copies it into a MemoryStream and ToArray() before the file write starts. A document with long AddPixelText content can therefore require multiple full-size copies of the PDF where the previous File.Create(path) path only copied the writer buffer directly to disk. Keeping SaveAsync on the file-stream writer path (or factoring a shared render-to-stream helper) avoids making normal file saves fail with OOM in cases that previously completed.

Useful? React with 👍 / 👎.

@ANcpLua ANcpLua merged commit 41fd3c2 into main Jun 4, 2026
6 of 8 checks passed
@ANcpLua ANcpLua deleted the feat/in-memory-pdf-output branch June 4, 2026 12:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants