Skip to content

Novis\Pdf\Document::withInvoiceXml

$document->withInvoiceXml(string $xml): Document$document->withInvoiceXml(
    string $xml,
): Document

Attaches the XML of an electronic invoice to the PDF file, in the Factur-X format.

A Factur-X invoice, also called ZUGFeRD, is one file for two readers. A person reads the PDF pages, and accounting software reads the attached XML. Your program builds the XML and passes it as a string. The render attaches it under the name factur-x.xml, with the fields the standard needs.

The XML needs the standard Document::Standard::PdfA3b, and PDF/A needs a creation time from withCreationTime. Without PDF/A-3b, render throws a LogicError. XML that is not well-formed makes render throw a ParseError. XML with elements nested more than 256 levels deep also throws a ParseError. Apart from that, the render does not check the XML. Your program must make sure that the invoice fields in it are correct.

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

The examples below make an electronic invoice, show the error without PDF/A-3b, and show the error for XML that is not well-formed.

Parameters

  • $xmlstring

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

A factur x e invoice

<?nvs
// Shows how to make an electronic invoice: a PDF/A-3b file with its Factur-X XML attached.
use Novis\Pdf\Document;
use Novis\Pdf\Reader;
string $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
. "<rsm:CrossIndustryInvoice xmlns:rsm=\"urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100\""
. " xmlns:ram=\"urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100\">"
. '<rsm:ExchangedDocument><ram:ID>Shop-1042</ram:ID><ram:TypeCode>380</ram:TypeCode></rsm:ExchangedDocument>'
. '</rsm:CrossIndustryInvoice>';
string $html = "<html lang=\"en\"><head><title>Invoice Shop-1042</title></head>"
. '<body><h1>Invoice Shop-1042</h1><p>Total: 5.70 EUR</p></body></html>';
// The XML needs the standard PDF/A-3b, and PDF/A needs a creation time.
var $rendered = Document::fromHtml($html)
->withStandard(Document::Standard::PdfA3b)
->withCreationTime(Core\Time\Instant::fromIso('2026-03-01T09:00:00Z'))
->withInvoiceXml($xml)
->render();
// The XML is attached under the name the standard sets. This prints that name.
foreach (Reader::open($rendered->bytes)->attachments() as var $file) {
echo $file->name, "\n";
}
// `Reader::invoiceXml` reads the same XML back from the file.
echo Reader::open($rendered->bytes)->invoiceXml() == $xml ? 'the XML is unchanged' : 'the XML changed', "\n";
Output
factur-x.xml
the XML is unchanged

An invoice without pdf a 3b

<?nvs
// Shows the error for invoice XML in a file that is not PDF/A-3b.
use Novis\Pdf\Document;
string $xml = "<rsm:CrossIndustryInvoice xmlns:rsm=\"urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100\"/>";
// This document has no standard, so `render` throws a `LogicError`.
try {
Document::fromHtml('<h1>Invoice Shop-1042</h1>')->withInvoiceXml($xml)->render();
} catch (LogicError $e) {
echo $e->message, "\n";
}
// PDF/A-2b is not enough either. Only PDF/A-3b allows an attached file.
try {
Document::fromHtml("<html lang=\"en\"><head><title>Invoice</title></head><body><h1>Invoice Shop-1042</h1></body></html>")
->withStandard(Document::Standard::PdfA2b)
->withCreationTime(Core\Time\Instant::fromIso('2026-03-01T09:00:00Z'))
->withInvoiceXml($xml)
->render();
} catch (LogicError $e) {
echo $e->message, "\n";
}
Output
an invoice XML is embedded only under the PDF/A-3b standard
an invoice XML is embedded only under the PDF/A-3b standard

Xml that is not well formed

<?nvs
// Shows the error for invoice XML that is not well-formed, such as XML with a missing end tag.
use Novis\Pdf\Document;
string $broken = "<rsm:CrossIndustryInvoice xmlns:rsm=\"urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100\">"
. '<rsm:ExchangedDocument>';
// `render` checks that the XML is well-formed before it makes the file.
// This XML is not, so `render` throws a `ParseError`.
try {
Document::fromHtml("<html lang=\"en\"><head><title>Invoice</title></head><body><h1>Invoice Shop-1042</h1></body></html>")
->withStandard(Document::Standard::PdfA3b)
->withCreationTime(Core\Time\Instant::fromIso('2026-03-01T09:00:00Z'))
->withInvoiceXml($broken)
->render();
echo "rendered\n";
} catch (ParseError $e) {
echo "ParseError\n";
}
Output
ParseError