Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/VellumPdf.Kernel/Document/PdfDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public sealed class PdfDocument : IDisposable
/// </summary>
public DateTimeOffset? Timestamp { get; set; }

/// <summary>
/// Optional fixed document <c>/ID</c> (the permanent element of the PDF <c>/ID</c> array).
/// When null, the ID is derived from the document content and <see cref="Timestamp"/>.
/// Pin this together with <see cref="Timestamp"/> for byte-reproducible output.
/// </summary>
public byte[]? DocumentId { get; set; }

/// <summary>
/// Requested PDF/A conformance level.
/// When non-<see cref="PdfConformance.None"/>:
Expand Down Expand Up @@ -539,7 +546,7 @@ public void Save(Stream destination)
outputIntentsRef = BuildOutputIntents(registry);

// ── Build document /ID (MD5 over title + producer + page count + timestamp) ─
var documentId = ComputeDocumentId(ts);
var documentId = DocumentId ?? ComputeDocumentId(ts);

// ── Build /Encrypt dictionary (AES-256, V5/R6) and arm the encryptor ──
// The /Encrypt object itself MUST be written BEFORE calling WriteAll,
Expand Down Expand Up @@ -821,7 +828,7 @@ private void WriteWithSignaturePlaceholders(Stream destination, SignaturePlaceho
registry.SetValue(metadataRef, metadataStream);

// ── Document ID ───────────────────────────────────────────────────────
var documentId = ComputeDocumentId(ts);
var documentId = DocumentId ?? ComputeDocumentId(ts);

// ── Outline tree ──────────────────────────────────────────────────────
PdfIndirectReference? outlinesRef = null;
Expand Down
2 changes: 2 additions & 0 deletions src/VellumPdf.Kernel/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ VellumPdf.Document.PdfDocument.Conformance.get -> VellumPdf.Document.PdfConforma
VellumPdf.Document.PdfDocument.Conformance.set -> void
VellumPdf.Document.PdfDocument.DefaultPageSize.get -> VellumPdf.Document.PdfRectangle!
VellumPdf.Document.PdfDocument.DefaultPageSize.set -> void
VellumPdf.Document.PdfDocument.DocumentId.get -> byte[]?
VellumPdf.Document.PdfDocument.DocumentId.set -> void
VellumPdf.Document.PdfDocument.Dispose() -> void
VellumPdf.Document.PdfDocument.Encrypt(VellumPdf.Encryption.PdfEncryptionSettings! settings) -> void
VellumPdf.Document.PdfDocument.Info.get -> VellumPdf.Document.PdfDocumentInfo!
Expand Down
86 changes: 86 additions & 0 deletions tests/VellumPdf.Kernel.Tests/DeterminismTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2026 Timothy van der Ham (@Tim81)
// SPDX-License-Identifier: Apache-2.0

using VellumPdf.Annotations;
using VellumPdf.Document;

namespace VellumPdf.Kernel.Tests;

/// <summary>
/// Pinning <see cref="PdfDocument.Timestamp"/> and <see cref="PdfDocument.DocumentId"/> must make
/// identical content produce byte-identical output — the foundation for golden snapshots and
/// reproducible builds (issue #5).
/// </summary>
public sealed class DeterminismTests
{
private static readonly DateTimeOffset PinnedTime = DateTimeOffset.FromUnixTimeSeconds(1_700_000_000);
private static readonly byte[] PinnedId = Convert.FromHexString("000102030405060708090A0B0C0D0E0F");

private static byte[] BuildSample(PdfConformance conformance)
{
using var doc = new PdfDocument
{
Timestamp = PinnedTime,
DocumentId = PinnedId,
Conformance = conformance,
};
doc.Info.Title = "Determinism";
doc.Info.Author = "VellumPdf";

var page = doc.AddPage(PageSize.A4);
doc.AddOutlineEntry(new PdfOutlineEntry { Title = "Section 1", DestPage = page, Level = 0 });

var ms = new MemoryStream();
doc.Save(ms);
return ms.ToArray();
}

[Fact]
public void Save_withPinnedTimestampAndId_isByteIdentical()
{
Assert.Equal(BuildSample(PdfConformance.None), BuildSample(PdfConformance.None));
}

[Fact]
public void Save_pdfA2b_withPins_isByteIdentical()
{
// Exercises the timestamp-dependent XMP metadata + output-intent path.
Assert.Equal(BuildSample(PdfConformance.PdfA2b), BuildSample(PdfConformance.PdfA2b));
}

[Fact]
public void DocumentId_pin_changesOutputVsComputedId_andIsStable()
{
static byte[] Build(byte[]? id)
{
using var doc = new PdfDocument { Timestamp = PinnedTime, DocumentId = id };
doc.Info.Title = "Same";
doc.AddPage(PageSize.A4);
var ms = new MemoryStream();
doc.Save(ms);
return ms.ToArray();
}

// Same content + timestamp: the only difference is the pinned vs content-derived /ID.
Assert.NotEqual(Build(PinnedId), Build(null));
// The pinned variant is itself byte-stable.
Assert.Equal(Build(PinnedId), Build(PinnedId));
}

[Fact]
public void NoDocumentIdPin_stillDeterministic_whenTimestampPinned()
{
// Without an explicit DocumentId, the content+timestamp-derived /ID must still be stable.
static byte[] Build()
{
using var doc = new PdfDocument { Timestamp = PinnedTime };
doc.Info.Title = "Stable";
doc.AddPage(PageSize.A4);
var ms = new MemoryStream();
doc.Save(ms);
return ms.ToArray();
}

Assert.Equal(Build(), Build());
}
}