Novis\Pdf\Reader::invoiceXml
Returns the XML of an electronic invoice that is attached to a PDF file.
An electronic invoice in the Factur-X, ZUGFeRD or XRechnung format is a PDF file with its data attached as XML. The attached file has the name factur-x.xml, zugferd-invoice.xml or xrechnung.xml. invoiceXml returns that XML as a string, with the same text the sender attached. If the file is not an electronic invoice, the result is null.
An invoice XML that is not UTF-8 text makes Reader::open throw a ParseError. Apart from that, the XML is not checked. Use Core\Xml::parse to read it. Core\Xml::parse throws a ParseError for XML that is not well-formed.
The examples below read the XML of an uploaded invoice, show the null result for a letter, and sort uploads into invoices and other files.
Return value
Returns ?tainted string.
Examples
Read an uploaded invoice
<?nvs// Shows how to read the Factur-X XML of an uploaded electronic invoice.use Novis\Pdf\Document;use Novis\Pdf\Reader;
// The uploaded invoice. Here the program renders it first, so the example has a file to read.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-7</ram:ID><ram:TypeCode>380</ram:TypeCode></rsm:ExchangedDocument>' . '</rsm:CrossIndustryInvoice>';var $upload = Document::fromHtml( "<html lang=\"en\"><head><title>Invoice Shop-7</title></head>" . '<body><p>Invoice Shop-7</p></body></html>',) ->withStandard(Document::Standard::PdfA3b) ->withCreationTime(Core\Time\Instant::fromIso('2026-01-01T00:00:00Z')) ->withInvoiceXml($xml) ->render() ->bytes;
// `invoiceXml` returns the XML as a string. It is the same text the sender attached.tainted string $received = Reader::open($upload)->invoiceXml() ?? '';echo $received == $xml ? 'the XML is unchanged' : 'the XML changed', "\n";
// `Core\Xml::parse` reads the XML. This prints the name of the root element.foreach (Core\Xml::parse($received)->children() as Core\Xml\Node $root) { echo $root->name(), "\n";}
// This prints the invoice number, `Shop-7`.echo Core\Str::before(Core\Str::after($received, '<ram:ID>') ?? '', '</ram:ID>') ?? '', "\n";the XML is unchangedrsm:CrossIndustryInvoiceShop-7A pdf that is not an invoice
<?nvs// Shows the result for a PDF file that is not an electronic invoice.use Novis\Pdf\Document;use Novis\Pdf\Reader;
var $letter = Document::fromHtml('<p>Thank you for your order.</p>')->render()->bytes;
// The letter has no invoice XML, so the result is `null` and this prints `no invoice XML`.?tainted string $xml = Reader::open($letter)->invoiceXml();echo $xml ?? 'no invoice XML', "\n";no invoice XMLSort the uploads
<?nvs// Shows how to sort uploaded PDF files into electronic invoices and other files.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-9</ram:ID><ram:TypeCode>380</ram:TypeCode></rsm:ExchangedDocument>' . '</rsm:CrossIndustryInvoice>';var $invoice = Document::fromHtml( "<html lang=\"en\"><head><title>Invoice Shop-9</title></head>" . '<body><p>Invoice Shop-9</p></body></html>',) ->withStandard(Document::Standard::PdfA3b) ->withCreationTime(Core\Time\Instant::fromIso('2026-01-01T00:00:00Z')) ->withInvoiceXml($xml) ->render() ->bytes;var $scan = Document::fromHtml('<p>Scanned delivery note</p>')->render()->bytes;
// A file with invoice XML goes to accounting. Any other file goes to the inbox.// This prints `accounting` for the invoice and `inbox` for the delivery note.array<bytes> $uploads = [$invoice, $scan];foreach ($uploads as bytes $upload) { if (Reader::open($upload)->invoiceXml() == null) { echo "inbox\n"; } else { echo "accounting\n"; }}accountinginbox