Novis\Pdf\Reader::attachments
$reader->attachments(): array<Attachment>Returns the files attached to a PDF file.
attachments returns a list. Each item has a name, which is the file name the PDF gives, and data, which is the content as bytes. A PDF file with no attached files gives an empty list. An electronic invoice has its XML in this list too, with the name factur-x.xml.
The name comes from the sender of the file. Do not use it as a path on your server without a check.
The examples below list the files of a received invoice, show the empty list of a letter, and find one file by its name.
Return value
Returns array<Attachment>.
Examples
List the attached files
<?nvs// Shows how to list the files attached to a received PDF invoice.use Novis\Pdf\Document;use Novis\Pdf\Reader;
// The received 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 $invoice = 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;
// Each attached file has a `name` and its `data` as bytes.// This prints one line: the name of the XML file and its size in bytes.foreach (Reader::open($invoice)->attachments() as var $file) { echo $file->name, ': ', Core\Bytes::length($file->data), " bytes\n";}factur-x.xml: 361 bytesA file without attachments
<?nvs// Shows that a PDF file with no attached files gives an empty list.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 attached files, so this prints `0 files`.var $files = Reader::open($letter)->attachments();echo Core\Arr::count($files), " files\n";0 filesFind one file by name
<?nvs// Shows how to find one attached file by its name and read its content as text.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-8</ram:ID><ram:TypeCode>380</ram:TypeCode></rsm:ExchangedDocument>' . '</rsm:CrossIndustryInvoice>';var $invoice = Document::fromHtml( "<html lang=\"en\"><head><title>Invoice Shop-8</title></head>" . '<body><p>Invoice Shop-8</p></body></html>',) ->withStandard(Document::Standard::PdfA3b) ->withCreationTime(Core\Time\Instant::fromIso('2026-01-01T00:00:00Z')) ->withInvoiceXml($xml) ->render() ->bytes;
// The data of a file is bytes. `as string` converts it to text.// This prints whether the file `factur-x.xml` contains the invoice number.foreach (Reader::open($invoice)->attachments() as var $file) { if ($file->name == 'factur-x.xml') { tainted string $content = $file->data as string; echo $file->name, ' contains Shop-8: ', Core\Str::contains($content, 'Shop-8') ? 'yes' : 'no', "\n"; }}factur-x.xml contains Shop-8: yes