PHP code example of dealerweb / einvoice

1. Go to this page and download the library: Download dealerweb/einvoice library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dealerweb / einvoice example snippets


use Dealerweb\EInvoice\Invoice;
use Dealerweb\EInvoice\Profile;
use Dealerweb\EInvoice\Render\HtmlRenderer;
use Dealerweb\EInvoice\Validation\Validator;

$invoice = Invoice::fromFile('invoice.pdf');      // XML (XRechnung, ZUGFeRD, Factur-X, Peppol) or a ZUGFeRD PDF
$invoice->number;                                 // "R-2026-0001"
$invoice->seller->name;                           // "Muster GmbH"
$invoice->totals->grossAmount;                    // "119.00"

$report = (new Validator())->validateFile('invoice.pdf');   // with the official rules: $report->isValid()
$html = (new HtmlRenderer('de'))->render($invoice);        // readable, in German, English or French

$invoice = new Invoice();                         // built with the same names - see Creating an invoice
$invoice->number = 'R-2026-0001';
// ...
$xml = $invoice->toXml(Profile::XRechnung);       // XRechnung, ZUGFeRD / Factur-X, EN 16931 or Peppol BIS as XML
$yourPdf = file_get_contents('R-2026-0001.pdf');  // the invoice as your system prints it
$pdf = $invoice->toPdf(Profile::En16931, pdf: $yourPdf);   // ZUGFeRD / Factur-X: your PDF with the XML embedded

use Dealerweb\EInvoice\Invoice;

$invoice = Invoice::fromFile('invoice.xml');      // XML or ZUGFeRD / Factur-X PDF - the file tells which
$invoice = Invoice::fromXml($xml);                // from a string (a PDF given here is read as a PDF)
$invoice = Invoice::fromPdf($pdf);                // from the content of a ZUGFeRD / Factur-X PDF
$xml = Invoice::xmlFromPdf($pdf);                 // the XML such a PDF carries, byte for byte as embedded

$invoice->number;                                 // BT-1: "R-2026-0001" - MODEL.md names every property
$invoice->issueDate;                              // "2026-09-26"
$invoice->buyer->address->city;                   // objects are always there - no check for null
foreach ($invoice->lines as $line) {
    $line->name;                                  // "Screw set"
    $line->quantity;                              // "2"
    $line->netAmount;                             // "100.00"
}
$invoice->isCreditNote();                         // a credit note code (381, 396, ...) or a UBL CreditNote
$invoice->isSelfBilled();                         // issued by the buyer (self-billing, "Gutschrift" in German tax law)
$invoice->unread();                               // what the document holds and the model has no place for, by its path
$invoice->toArray();                              // everything given, with the names of the model (also JSON)

foreach ($invoice->attachments as $attachment) {  // BG-24 supporting documents
    $attachment->filename;                        // as delivered, e.g. "Aufmass.pdf"
    $attachment->content();                       // the file - null where it only names an address (never opened)
    $attachment->saveTo('/tmp');                  // writes it under safeFilename() and never overwrites a file
}

$summary = $invoice->summary();                   // the key facts for capturing it as a document
$summary->dueDate;                                // the earliest due date (cash discount, instalments)
$summary->subject;                                // the first line, its description where the name is an article number
$summary->toArray();                              // nested arrays, e.g. for JSON

use Dealerweb\EInvoice\Invoice;
use Dealerweb\EInvoice\Model\Attachment;
use Dealerweb\EInvoice\Model\Identifier;
use Dealerweb\EInvoice\Model\Line;
use Dealerweb\EInvoice\Model\PaymentMeans;
use Dealerweb\EInvoice\Model\PaymentMeansCode;
use Dealerweb\EInvoice\Model\UnitCode;
use Dealerweb\EInvoice\Model\VatCategoryCode;
use Dealerweb\EInvoice\Profile;
use Dealerweb\EInvoice\Syntax;

$invoice = new Invoice();
$invoice->number = 'R-2026-0001';
$invoice->typeCode = '380';                        // or InvoiceTypeCode::COMMERCIAL_INVOICE
$invoice->issueDate = '2026-09-26';                // or a DateTimeInterface
$invoice->currency = 'EUR';
$invoice->businessProcess = 'urn:fdc:peppol.eu:2017:poacc:billing:01:1.0';
$invoice->buyerReference = '04011000-12345-34';    // the Leitweg-ID of a German public buyer
$invoice->paymentTerms = 'Payable within 14 days.';
$invoice->seller->name = 'Muster GmbH';
$invoice->seller->vatId = 'DE123456789';
$invoice->seller->electronicAddress = new Identifier('[email protected]', 'EM');
$invoice->seller->address->line1 = 'Hauptstrasse 1';
$invoice->seller->address->postcode = '08523';
$invoice->seller->address->city = 'Plauen';
$invoice->seller->address->country = 'DE';
$invoice->seller->contact->name = 'Max Muster';
$invoice->seller->contact->phone = '+49 3741 123456';
$invoice->seller->contact->email = '[email protected]';
$invoice->buyer->name = 'Stadt Beispielhausen';
$invoice->buyer->electronicAddress = new Identifier('[email protected]', 'EM');
$invoice->buyer->address->postcode = '12345';
$invoice->buyer->address->city = 'Beispielhausen';
$invoice->buyer->address->country = 'DE';
$invoice->paymentMeans[] = new PaymentMeans(typeCode: PaymentMeansCode::SEPA_CREDIT_TRANSFER, accountId: 'DE02120300000000202051');
$invoice->lines[] = new Line(id: '1', name: 'Screw set', quantity: 2, unit: UnitCode::PIECE, netPrice: '50.00',
    vatCategory: VatCategoryCode::STANDARD_RATE, vatRate: 19);
$invoice->attachments[] = Attachment::fromFile('Aufmass.pdf');   // a supporting document: name, type, content

$xml = $invoice->toXml(Profile::XRechnung);        // CII; the totals and the VAT breakdown are calculated
$xml = $invoice->toXml(Profile::XRechnung, Syntax::UblInvoice);   // the same invoice in UBL
$pdf = $invoice->toPdf(Profile::En16931, pdf: file_get_contents('R-2026-0001.pdf'));   // your PDF, the XML in it
$pdf = $invoice->toPdf(Profile::En16931, 'de');    // without a PDF of your own: rendered in German, the XML in it

// Peppol takes electronic addresses with a scheme of Peppol - no e-mail address (EM):
$invoice->seller->electronicAddress = new Identifier('DE123456789', '9930');        // the German VAT identifier
$invoice->buyer->electronicAddress = new Identifier('04011000-12345-34', '0204');   // the Leitweg-ID
$xml = $invoice->toXml(Profile::Peppol);           // Peppol BIS Billing 3.0 in UBL

$invoice = Invoice::fromArray([                    // the same as array, with the same names
    'number' => 'R-2026-0001',
    'purchaseOrder' => 'PO-4711',                  // an object with one main value may be given as that value
    'seller' => ['name' => 'Muster GmbH', 'address' => ['city' => 'Plauen', 'country' => 'DE']],
    'lines' => [['id' => '1', 'name' => 'Screw set', 'quantity' => 2, 'unit' => 'H87', 'netPrice' => '50.00']],
    // ...
]);

use Dealerweb\EInvoice\Generation\Generator;
use Dealerweb\EInvoice\Profile;

$xml = (new Generator(Profile::XRechnung))->xml([
    'BT-1' => 'R-2026-0001',                   // keys: the ids of the Factur-X field list
    'BT-2' => '2026-09-26',                    // dates as YYYY-MM-DD, YYYYMMDD or DateTimeInterface
    'BT-3' => '380',
    'BT-5' => 'EUR',
    'BT-10' => '04011000-12345-34',
    'BT-20' => 'Payable within 14 days.',
    'BT-23' => 'urn:fdc:peppol.eu:2017:poacc:billing:01:1.0',
    'BG-4' => [                                // seller
        'BT-27' => 'Muster GmbH',
        'BT-31' => 'DE123456789',              // its scheme VA is set by the generator
        'BT-34' => ['value' => '[email protected]', 'BT-34-1' => 'EM'],   // a value with an attribute
        'BG-5' => ['BT-35' => 'Hauptstrasse 1', 'BT-37' => 'Plauen', 'BT-38' => '08523', 'BT-40' => 'DE'],
        'BG-6' => ['BT-41' => 'Max Muster', 'BT-42' => '+49 3741 123456', 'BT-43' => '[email protected]'],
    ],
    'BG-7' => [                                // buyer
        'BT-44' => 'Stadt Beispielhausen',
        'BT-49' => ['value' => '[email protected]', 'BT-49-1' => 'EM'],
        'BG-8' => ['BT-50' => 'Rathausplatz 1', 'BT-52' => 'Beispielhausen', 'BT-53' => '12345', 'BT-55' => 'DE'],
    ],
    'BG-16' => ['BT-81' => '58', 'BG-17' => ['BT-84' => 'DE02120300000000202051']],
    'BG-22' => ['BT-106' => '100.00', 'BT-109' => '100.00', 'BT-110' => '19.00', 'BT-112' => '119.00', 'BT-115' => '119.00'],
    'BG-23' => [['BT-116' => '100.00', 'BT-117' => '19.00', 'BT-118' => 'S', 'BT-119' => '19']],
    'BG-25' => [                               // one array per line
        ['BT-126' => '1', 'BT-129' => '2', 'BT-130' => 'H87', 'BT-131' => '100.00',
            'BG-29' => ['BT-146' => '50.00'], 'BG-30' => ['BT-151' => 'S', 'BT-152' => '19'], 'BG-31' => ['BT-153' => 'Screw set']],
    ],
]);

$xml = (new Generator(Profile::Extended, validate: false))->xml($invoice);   // the model, without the validator
$pdf = (new Generator(Profile::En16931))->pdf($xml, null, 'de');              // a PDF of XML written before

use Dealerweb\EInvoice\Profile;
use Dealerweb\EInvoice\Validation\Severity;
use Dealerweb\EInvoice\Validation\Validator;

$report = (new Validator())->validateFile('invoice.xml');   // or a ZUGFeRD / Factur-X PDF - the file tells which
$report = (new Validator())->validate($xml);                // XML - or the content of a PDF - as string
$report = (new Validator())->validate($xml, Profile::Core); // against a given profile instead of the one named in BT-24

$report->isValid();        // meets its profile: no message is an error (the KoSIT validator's "accept")
$report->isEInvoice();     // valid and an invoice in the sense of EN 16931 - ZUGFeRD MINIMUM and BASIC WL are not
$report->profile();        // Profile::XRechnung, Profile::Extended, ... - null when no rules apply
$report->scenario();       // e.g. "EN16931 XRechnung (UBL Invoice)", "ZUGFeRD / Factur-X EXTENDED (Factur-X 1.09.2)"
$report->ruleSets();       // e.g. ["en16931-ubl", "xrechnung-ubl"] - empty when the schema already failed

foreach ($report->errors() as $message) {   // also warnings(), messages(Severity::Information), messages()
    $message->code;        // rule id ("BR-CO-10") - or SCHEMA, NOT_WELL_FORMED, NO_SCENARIO, PROCESSING_ERROR, ...
    $message->text;        // the text of the rule or of the error
    $message->source;      // "schema", "document" or the rule set, e.g. "xrechnung-ubl"
    $message->location;    // XPath of the node a rule fired on
    $message->line;        // line of a schema error
}
$report->notes();          // e.g. that the own rules of another CIUS are not 

use Dealerweb\EInvoice\Render\HtmlRenderer;
use Dealerweb\EInvoice\Render\PdfRenderer;

$html = (new HtmlRenderer('de'))->render($invoice);   // one self-contained HTML page - languages de, en, fr
$pdf = (new PdfRenderer('de'))->render($invoice);     // the same layout as A4 PDF (dompdf), to read

use Dealerweb\EInvoice\CodeList;
use Dealerweb\EInvoice\CodeLists;
use Dealerweb\EInvoice\Labels;

Labels::name('BT-1', 'fr');       // "Numéro de facture" - names in de, en, fr
Labels::name('BT-X-202', 'de');   // "Lieferung › Lieferschein › Nummer": the properties of the model that lead to it

CodeLists::name(CodeList::PaymentMeans, '58', 'de');   // "SEPA-Überweisung" - names of code values in de, en, fr
CodeLists::name(CodeList::Unit, '2I', 'de', fallback: false);   // null - no German name (English otherwise)
CodeLists::nameForField('BT-81', '58', 'en');         // the code list found through the business term
CodeLists::forField('BT-151');                         // [CodeList::TaxScheme, CodeList::VatCategory]
CodeLists::property(CodeList::DocumentType, '381', 'interpretation');   // "Credit Note"