PHP code example of invoicexml / zugferd-xrechnung

1. Go to this page and download the library: Download invoicexml/zugferd-xrechnung 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/ */

    

invoicexml / zugferd-xrechnung example snippets


use InvoiceXml\ZugferdXrechnung\Client;

$client = new Client(getenv('INVOICEXML_API_KEY'));

$pdf = $client->createZugferd([
    'invoiceNumber'  => 'INV-2026-00045',
    'issueDate'      => '2026-05-17',
    'currency'       => 'EUR',
    'seller'         => ['name' => 'Acme Engineering GmbH', 'vatIdentifier' => 'DE123456789', 'postalAddress' => ['line1' => 'Hauptstraße 12', 'city' => 'Berlin', 'postCode' => '10115', 'country' => 'DE']],
    'buyer'          => ['name' => 'Globex SAS', 'postalAddress' => ['line1' => '15 rue de Rivoli', 'city' => 'Paris', 'postCode' => '75001', 'country' => 'FR']],
    'paymentDetails' => ['paymentAccountIdentifier' => 'DE89370400440532013000'],
    'lines'          => [['quantity' => 10, 'priceDetails' => ['netPrice' => 150], 'vatInformation' => ['rate' => 19], 'item' => ['name' => 'Senior consulting']]],
]);

$pdf->save('invoice.pdf');

$pdf = $client->createZugferd($invoice, [
    'language'   => 'de',          // labels of the rendered PDF: en, de or fr
    'brandColor' => '#0F766E',
    'logoUrl'    => 'https://example.com/logo.png',
]);

$pdf->save($pdf->filename);       // for example invoice-zugferd-INV-2026-00045.pdf

$xml = $client->createXrechnung([
    'invoiceNumber'  => 'RE-2026-3311',
    'issueDate'      => '2026-07-28',
    'dueDate'        => '2026-08-27',
    'currency'       => 'EUR',
    'buyerReference' => '08111-00234-45',
    'seller' => [
        'name'              => 'Neckarblick IT GmbH',
        'vatIdentifier'     => 'DE315498762',
        'postalAddress'     => ['line1' => 'Königstraße 82', 'city' => 'Stuttgart', 'postCode' => '70173', 'country' => 'DE'],
        'contact'           => ['name' => 'Miriam Vogel', 'phone' => '+49 711 2203980', 'email' => '[email protected]'],
        'electronicAddress' => ['identifier' => 'DE315498762', 'schemeId' => '0204'],
    ],
    'buyer' => [
        'name'              => 'Landeshauptstadt Stuttgart, Schulverwaltungsamt',
        'postalAddress'     => ['line1' => 'Hauptstätter Straße 79', 'city' => 'Stuttgart', 'postCode' => '70178', 'country' => 'DE'],
        'electronicAddress' => ['identifier' => '08111-00234-45', 'schemeId' => '0204'],
    ],
    'paymentDetails' => ['paymentMeansCode' => 58, 'paymentAccountIdentifier' => 'DE75512108001245126199'],
    'lines' => [[
        'quantity'       => 210,
        'unitCode'       => 'HUR',
        'item'           => ['name' => 'Betreuung Schul-IT, Juli 2026'],
        'priceDetails'   => ['netPrice' => 92.00],
        'vatInformation' => ['rate' => 19, 'categoryCode' => 'S'],
    ]],
]);

$xml->save('xrechnung.xml');

$result = $client->validateZugferd('incoming.pdf');      // or validateXrechnung('incoming.xml')

if (!$result->valid) {
    foreach ($result->errors as $error) {
        echo "{$error->rule} ({$error->layer}): {$error->message}\n";
    }
}

$report = $client->validateZugferdReport('incoming.pdf', validationLanguage: 'de');   // or validateXrechnungReport('incoming.xml')

$report->file->save('report.pdf');

if ($report->valid === false) {
    echo "The invoice failed validation, see the report.\n";
}

$zugferd = $client->embedZugferd('layout.pdf', 'factur-x.xml');

$xml     = $client->createCii($invoice, ['language' => 'de']);
$zugferd = $client->embedZugferd('layout.pdf', $xml);

$ubl = $client->createUbl($invoice, ['profile' => 'peppol-bis-3']);

$preview = $client->renderXmlToPdf('xrechnung.xml', language: 'de');
$preview = $client->renderXmlToPdf('invoice.xml', logoUrl: 'https://example.com/logo.png', 

$ubl = $client->convertZugferdToUbl('incoming.pdf');
$pdf = $client->convertCiiToZugferd('invoice.xml', language: 'de');

$result = $client->emailZugferd($invoice, [
    'from'     => '[email protected]',    // me Billing',
    'cc'       => ['[email protected]'],   // cc and bcc hold at most 2 addresses together
    'bcc'      => ['[email protected]'],
    'replyTo'  => '[email protected]',
    'subject'  => 'Your invoice INV-2026-00045',
    'message'  => "Hello Anna,\n\nyour invoice is attached.",
], ['language' => 'de']);

echo $result->status, ' ', $result->messageId;

$result = $client->emailEmbeddedZugferd('layout.pdf', 'factur-x.xml', [
    'from' => '[email protected]',
    'to'   => '[email protected]',
]);

$data = $client->extractJson('incoming.pdf');             // ZUGFeRD PDF or XRechnung XML
$invoice = $data['invoice'];                              // same shape the create methods accept

$zip = $client->extractAttachments('incoming.pdf');       // supporting documents as a ZIP

$parsed  = $client->parseJson('scan.pdf');
$invoice = $parsed['invoice'];                            // same shape the create methods accept

$zugferd = $client->transformToZugferd('invoice.pdf', language: 'de');
$xml     = $client->transformToXrechnung('invoice.pdf', buyerReference: '08111-00234-45');

use InvoiceXml\ZugferdXrechnung\ApiException;

try {
    $client->createXrechnung($invoice);
} catch (ApiException $e) {
    echo $e->statusCode, ' ', $e->errorCode, ': ', $e->detail, "\n";
    foreach ($e->errors as $error) {
        echo "{$error->rule}: {$error->message}\n";
    }
}

$client = new Client(
    apiKey: getenv('INVOICEXML_API_KEY'),
    httpClient: $psr18Client,          // any PSR-18 client, Guzzle by default
    requestFactory: $psr17Factory,     // PSR-17 factories, Guzzle's by default
    streamFactory: $psr17Factory,
    maxRetries: 3,
);