Skip to content

Novis\Pdf\Document::withCreationTime

$document->withCreationTime(Core\Time\Instant $at): Document$document->withCreationTime(
    Core\Time\Instant $at,
): Document

Sets the creation time that the PDF file shows.

A PDF viewer shows this time in the properties of the file. A render reads no clock, so without withCreationTime the file has no creation time. Pass Core\Time::now() for the time of the render, or the time of the order or the invoice the file is about.

A PDF/A file must have a creation time. With a PDF/A standard and no withCreationTime, render throws a LogicError.

withCreationTime returns a new Document and does not change the one you called it on.

Good to know: the same document with the same creation time always gives the same bytes. A test can compare two files, and a cache can use the bytes as a key.

The examples below write the time into an invoice, show that the same time gives the same file, and show that PDF/A needs a creation time.

Parameters

  • $atCore\Time\Instant

Return value

Returns Document.

Changelog

VersionChange
0.0.6Each Novis\Pdf\Document method that returns a new document now starts with with, for example withHeader() and withPassword(). created() is now withCreationTime().

Examples

The time an invoice was made

<?nvs
// Shows how to write the time an invoice was made into the PDF file.
use Novis\Pdf\Document;
var $invoice = Document::fromHtml('<h1>Invoice 1042</h1><p>Total: 5.70 EUR</p>');
// Without `withCreationTime`, the file has no creation time. This prints `false`.
bytes $plain = $invoice->render()->bytes;
echo Core\Bytes::contains($plain, '/CreationDate' as bytes) ? 'true' : 'false', "\n";
// With `withCreationTime`, the file has the time you give. A program usually gives `Core\Time::now()`.
bytes $dated = $invoice->withCreationTime(Core\Time\Instant::fromIso('2026-03-01T09:30:00Z'))->render()->bytes;
echo Core\Bytes::contains($dated, '/CreationDate' as bytes) ? 'true' : 'false', "\n";
Output
false
true

The same time gives the same file

<?nvs
// Shows that a document gives the same bytes again when its creation time is the same.
use Novis\Pdf\Document;
var $report = Document::fromHtml('<h1>Orders in March</h1><p>412 orders</p>');
var $march = Core\Time\Instant::fromIso('2026-03-31T23:00:00Z');
// The render reads no clock. The only time in the file is the one `withCreationTime` gives.
// So two renders with the same time are equal. This prints `same`.
bytes $first = $report->withCreationTime($march)->render()->bytes;
bytes $second = $report->withCreationTime($march)->render()->bytes;
echo $first == $second ? 'same' : 'different', "\n";
// A different time gives a different file. This prints `different`.
bytes $later = $report->withCreationTime(Core\Time\Instant::fromIso('2026-04-01T08:00:00Z'))->render()->bytes;
echo $first == $later ? 'same' : 'different', "\n";
Output
same
different

Pdf a needs a creation time

<?nvs
// Shows that a PDF/A file needs `withCreationTime`.
use Novis\Pdf\Document;
var $archive = Document::fromHtml(
"<html lang=\"en\"><head><title>Contract</title></head>"
. '<body><h1>Contract</h1><p>Shop rents a stand to Blog for one year.</p></body></html>',
)
->withStandard(Document::Standard::PdfA2b);
// Without a creation time, `render` throws a `LogicError`.
try {
$archive->render();
} catch (LogicError $e) {
echo "LogicError\n";
}
// With one, the file renders. This prints the page count.
echo $archive->withCreationTime(Core\Time\Instant::fromIso('2026-03-01T09:30:00Z'))->render()->pageCount, " page\n";
Output
LogicError
1 page