factur_x 0.1.2
factur_x: ^0.1.2 copied to clipboard
A Dart library to generate Factur-X (ZUGFeRD) invoices. Compliant with the German "E-Rechnungspflicht".
factur_x #
A Dart library to generate Factur-X (ZUGFeRD) invoices. Compliant with the German "E-Rechnungspflicht". It is also accepted in France and other European countries that follow the EN16931 standard.
Motivation #
Factur-X (also known as ZUGFeRD 2.1) is a standard for electronic invoices that combines a PDF/A-3 file with an embedded XML file containing the invoice data. This library allows you to generate the XML part of a Factur-X invoice in Dart.
The goal is to provide a flexible API but since I don't have much time,
the library only exposes a package:factur_x/simple.dart that generates a Factur-X compliant XML using the EN16931 profile which is accepted in Germany and France.
The API should capture most use cases.
Getting Started #
➤ Installation #
Add the following to your pubspec.yaml:
dependencies:
factur_x: <version>
➤ Simple Usage #
Let's create a simple invoice using buildFacturXml from package:factur_x/simple.dart:
import 'package:factur_x/simple.dart';
void main() {
String facturXml = buildFacturXml(
invoiceNumber: '2020-01',
issueDate: DateTime(2020, 1, 1),
deliveryDate: DeliveryDate.date(DateTime(2020, 1, 1)),
dueDate: DateTime(2020, 1, 31),
seller: InvoiceParty(
name: 'Max Mustermann',
street: 'Musterstraße 1',
postcode: '12345',
city: 'Berlin',
countryCode: 'DE',
vatId: 'DE 123 456 789',
email: 'info@example.com',
),
buyer: InvoiceParty(
name: 'Max Consumer',
street: 'Consumerstraße 1',
postcode: '12345',
city: 'Berlin',
countryCode: 'DE',
),
itemList: InvoiceItemList(
items: [
InvoiceLineItem(
lineId: '1',
name: 'Some Work',
unitPrice: 50,
quantity: 3,
),
],
),
paymentInfo: PaymentInfo(
iban: 'DE12 3456 7890 1234 5678 90',
),
);
File('example.xml').writeAsStringSync(facturXml);
}
➤ PDF integration #
We can use the pdf package to create a PDF/A-3 file and embed the generated XML:
Note: use pdf_plus while https://github.com/DavBfr/dart_pdf/issues/1822 is open.
import 'package:factur_x/simple.dart';
import 'package:pdf_plus/pdf.dart';
import 'package:pdf_plus/widgets.dart' as pw;
Future<Uint8List> createInvoicePdf() async {
// 1. Create PDF document with Factur-X metadata
final pdf = pw.Document(
metadata: PdfaRdf(
title: 'Invoice 2020-01',
author: 'Max Mustermann',
creator: 'Max Mustermann',
producer: 'Max Mustermann',
creationDate: DateTime.now(),
invoiceRdf: PdfaFacturxRdf().create(filename: 'factur-x.xml'),
).create(),
);
// 2. Add ICC color profile for PDF/A-3 compliance
final iccData = File('assets/sRGB2014.icc').readAsBytesSync();
PdfaColorProfile(pdf.document, iccData);
// 3. Load embedded fonts (required for PDF/A)
final fontRegular = pw.Font.ttf(File('assets/Roboto-Regular.ttf').readAsBytesSync().buffer.asByteData());
final fontBold = pw.Font.ttf(File('assets/Roboto-Bold.ttf').readAsBytesSync().buffer.asByteData());
// 4. Build your invoice PDF pages
pdf.addPage(
pw.Page(
pageTheme: pw.PageTheme(
theme: pw.ThemeData.withFont(base: fontRegular, bold: fontBold),
),
build: (_) => pw.Column(
children: [
// ... your invoice layout here ...
],
),
),
);
// 5. Generate the Factur-X XML
final facturXml = buildFacturXml(
// ...
);
// 6. Attach the XML to the PDF
PdfaAttachedFiles(pdf.document, {'factur-x.xml': facturXml});
return await pdf.save();
}
License #
MIT License
Copyright (c) 2026 Tien Do Nam
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.