2024-04-22 16:30:55 +02:00
# @fin.cx/xinvoice
2025-04-03 20:45:26 +00:00
A comprehensive TypeScript library for creating, manipulating, and embedding XML invoice data within PDF files, supporting multiple European electronic invoice standards including ZUGFeRD (v1 & v2), Factur-X, XRechnung, UBL, and FatturaPA.
2024-04-22 16:30:55 +02:00
2025-04-03 20:45:26 +00:00
## Features
2024-04-22 16:30:55 +02:00
2025-04-03 20:45:26 +00:00
- **Multi-format support**: Process invoices in ZUGFeRD (v1 & v2), Factur-X, XRechnung, UBL, and FatturaPA
- **PDF handling**: Extract XML from PDF/A-3 invoices and embed XML into PDFs
- **Validation**: Validate invoices against format-specific rules
- **Conversion**: Convert between different invoice formats
- **TypeScript**: Fully typed API with TypeScript definitions
- **Modular architecture**: Extensible design with specialized components
2024-04-22 16:30:55 +02:00
2025-04-03 20:45:26 +00:00
## Install
To install `@fin.cx/xinvoice` , you'll need a package manager. We recommend using pnpm:
2025-03-17 15:28:55 +00:00
```shell
2025-04-03 20:45:26 +00:00
# Using pnpm (recommended)
2025-03-17 15:28:55 +00:00
pnpm add @fin .cx/xinvoice
2024-04-22 16:30:55 +02:00
2025-04-03 20:45:26 +00:00
# Using npm
npm install @fin .cx/xinvoice
2024-04-22 16:30:55 +02:00
2025-04-03 20:45:26 +00:00
# Using yarn
yarn add @fin .cx/xinvoice
2024-04-22 16:30:55 +02:00
```
2025-04-03 20:45:26 +00:00
## Usage
2024-04-22 16:30:55 +02:00
2025-04-03 20:45:26 +00:00
The `@fin.cx/xinvoice` module streamlines the management of electronic invoices, handling the creation, manipulation, and embedding of structured invoice data in PDF files. Below are examples of common use cases.
2024-12-31 13:41:45 +01:00
2025-04-03 20:45:26 +00:00
### Basic Usage
2024-04-22 16:30:55 +02:00
```typescript
import { XInvoice } from '@fin .cx/xinvoice';
2024-12-31 13:41:45 +01:00
import { promises as fs } from 'fs';
2025-04-03 20:45:26 +00:00
// Create a new invoice
const invoice = new XInvoice();
invoice.id = 'INV-2023-001';
invoice.from = {
name: 'Supplier Company',
// Add more details...
};
invoice.to = {
name: 'Customer Company',
// Add more details...
};
// Add more invoice details...
// Export to XML
const xml = await invoice.exportXml('zugferd');
// Load from XML
const loadedInvoice = await XInvoice.fromXml(xml);
// Load from PDF
const pdfBuffer = await fs.readFile('invoice.pdf');
const invoiceFromPdf = await XInvoice.fromPdf(pdfBuffer);
// Export to PDF
const pdfWithXml = await invoice.exportPdf(pdfBuffer);
await fs.writeFile('invoice-with-xml.pdf', pdfWithXml);
2024-12-31 13:41:45 +01:00
```
2024-04-22 16:30:55 +02:00
2025-04-03 20:45:26 +00:00
### Working with Different Invoice Formats
2024-12-31 13:41:45 +01:00
```typescript
2025-04-03 20:45:26 +00:00
// Load a ZUGFeRD invoice
const zugferdXml = await fs.readFile('zugferd-invoice.xml', 'utf8');
const zugferdInvoice = await XInvoice.fromXml(zugferdXml);
2024-12-31 13:41:45 +01:00
2025-04-03 20:45:26 +00:00
// Load a Factur-X invoice
const facturxXml = await fs.readFile('facturx-invoice.xml', 'utf8');
const facturxInvoice = await XInvoice.fromXml(facturxXml);
2024-04-22 16:30:55 +02:00
2025-04-03 20:45:26 +00:00
// Load an XRechnung invoice
const xrechnungXml = await fs.readFile('xrechnung-invoice.xml', 'utf8');
const xrechnungInvoice = await XInvoice.fromXml(xrechnungXml);
2024-04-22 16:30:55 +02:00
```
2025-04-03 20:45:26 +00:00
### PDF Handling
2024-04-22 16:30:55 +02:00
2024-12-31 13:41:45 +01:00
```typescript
2025-04-03 20:45:26 +00:00
// Extract XML from PDF
const pdfBuffer = await fs.readFile('invoice.pdf');
const invoice = await XInvoice.fromPdf(pdfBuffer);
// Embed XML into PDF
const existingPdf = await fs.readFile('document.pdf');
const pdfWithInvoice = await invoice.exportPdf(existingPdf);
await fs.writeFile('invoice-with-xml.pdf', pdfWithInvoice);
2024-12-31 13:41:45 +01:00
```
2025-04-03 20:45:26 +00:00
### Validating Invoices
2024-04-22 16:30:55 +02:00
```typescript
2025-04-03 20:45:26 +00:00
// Validate an invoice
const validationResult = await invoice.validate();
if (validationResult.valid) {
console.log('Invoice is valid');
} else {
console.log('Validation errors:', validationResult.errors);
2024-12-31 13:41:45 +01:00
}
2025-04-03 20:45:26 +00:00
```
2024-04-22 16:30:55 +02:00
2025-04-03 20:45:26 +00:00
## Architecture
2024-12-31 13:41:45 +01:00
2025-04-03 20:45:26 +00:00
XInvoice uses a modular architecture with specialized components:
2024-12-31 13:41:45 +01:00
2025-04-03 20:45:26 +00:00
### Core Components
2024-04-22 16:30:55 +02:00
2025-04-03 20:45:26 +00:00
- **XInvoice**: The main class that provides a high-level API for working with invoices
- **Decoders**: Convert format-specific XML to a common invoice model
- **Encoders**: Convert the common invoice model to format-specific XML
- **Validators**: Validate invoices against format-specific rules
2024-12-31 13:41:45 +01:00
2025-04-03 20:45:26 +00:00
### PDF Processing
2024-12-31 13:41:45 +01:00
2025-04-03 20:45:26 +00:00
- **PDF Extractors**: Extract XML from PDF files using multiple strategies:
- Standard Extraction: Extracts XML from standard PDF/A-3 embedded files
- Associated Files Extraction: Extracts XML from associated files (AF entry)
- Text-based Extraction: Extracts XML by searching for patterns in the PDF text
- **PDF Embedders**: Embed XML into PDF files
2024-12-31 13:41:45 +01:00
2025-04-03 20:45:26 +00:00
This modular approach ensures maximum compatibility with different PDF implementations and invoice formats.
2024-12-31 13:41:45 +01:00
2025-04-03 20:45:26 +00:00
## Supported Invoice Formats
2024-12-31 13:41:45 +01:00
2025-04-03 20:45:26 +00:00
| Format | Version | Read | Write | Validate |
|--------|---------|------|-------|----------|
| ZUGFeRD | 1.0 | ✅ | ✅ | ✅ |
| ZUGFeRD | 2.0/2.1 | ✅ | ✅ | ✅ |
| Factur-X | 1.0 | ✅ | ✅ | ✅ |
| XRechnung | 1.2+ | ✅ | ✅ | ✅ |
| UBL | 2.1 | ✅ | ✅ | ✅ |
| CII | 16931 | ✅ | ✅ | ✅ |
| FatturaPA | 1.2 | ✅ | ✅ | ✅ |
2024-04-22 16:30:55 +02:00
2025-04-03 20:45:26 +00:00
## Advanced Usage
2025-03-17 15:28:55 +00:00
2025-04-03 20:45:26 +00:00
### Custom Encoders and Decoders
2025-03-17 15:28:55 +00:00
```typescript
2025-04-03 20:45:26 +00:00
// Using specific encoders
import { ZUGFeRDEncoder, FacturXEncoder } from '@fin .cx/xinvoice';
2024-12-31 13:41:45 +01:00
2025-04-03 20:45:26 +00:00
// Create ZUGFeRD XML
const zugferdEncoder = new ZUGFeRDEncoder();
const zugferdXml = await zugferdEncoder.createXml(invoiceData);
2024-12-31 13:41:45 +01:00
2025-04-03 20:45:26 +00:00
// Create Factur-X XML
const facturxEncoder = new FacturXEncoder();
const facturxXml = await facturxEncoder.createXml(invoiceData);
2024-12-31 13:41:45 +01:00
2025-04-03 20:45:26 +00:00
// Using specific decoders
import { ZUGFeRDDecoder, FacturXDecoder } from '@fin .cx/xinvoice';
2024-12-31 13:41:45 +01:00
2025-04-03 20:45:26 +00:00
// Decode ZUGFeRD XML
const zugferdDecoder = new ZUGFeRDDecoder(zugferdXml);
const zugferdData = await zugferdDecoder.decode();
2025-03-17 15:28:55 +00:00
2025-04-03 20:45:26 +00:00
// Decode Factur-X XML
const facturxDecoder = new FacturXDecoder(facturxXml);
const facturxData = await facturxDecoder.decode();
```
2025-03-17 15:28:55 +00:00
2025-04-03 20:45:26 +00:00
### Circular Encoding and Decoding
2025-03-17 15:28:55 +00:00
```typescript
// Start with invoice data
const invoiceData = { /* your structured invoice data */ };
// Create XML
const encoder = new FacturXEncoder();
2025-04-03 20:45:26 +00:00
const xml = await encoder.createXml(invoiceData);
2025-03-17 15:28:55 +00:00
// Decode XML back to structured data
2025-03-17 16:30:23 +00:00
const decoder = new FacturXDecoder(xml);
2025-04-03 20:45:26 +00:00
const extractedData = await decoder.decode();
2025-03-17 15:28:55 +00:00
// Now extractedData contains the same information as your original invoiceData
```
2025-04-03 20:45:26 +00:00
## Development
2025-03-17 15:28:55 +00:00
2025-04-03 20:45:26 +00:00
### Building the Project
2025-03-17 15:28:55 +00:00
2025-04-03 20:45:26 +00:00
```bash
# Install dependencies
pnpm install
2025-03-17 15:28:55 +00:00
2025-04-03 20:45:26 +00:00
# Build the project
pnpm run build
```
### Running Tests
2025-03-17 15:28:55 +00:00
2025-04-03 20:45:26 +00:00
```bash
# Run all tests
pnpm test
2025-03-17 15:28:55 +00:00
2025-04-03 20:45:26 +00:00
# Run specific test
pnpm test test/test.xinvoice.ts
```
2025-03-17 15:28:55 +00:00
The library includes comprehensive test suites that verify:
- XML creation capabilities
- Format detection logic
- XML encoding/decoding circularity
- Special character handling
- Different invoice types (invoices, credit notes)
2025-04-03 20:45:26 +00:00
- PDF extraction and embedding
2025-03-17 15:28:55 +00:00
2025-04-03 20:45:26 +00:00
## Key Features
2025-03-17 15:28:55 +00:00
1. **PDF Integration**
- Embed XML invoices in PDF documents
2025-04-03 20:45:26 +00:00
- Extract XML from existing PDF invoices using multiple strategies
2025-03-17 15:28:55 +00:00
- Handle different XML attachment methods
2024-12-31 13:41:45 +01:00
2025-03-17 15:28:55 +00:00
2. **Encoding & Decoding**
- Create standards-compliant XML from structured data
- Parse XML invoices back to structured data
- Support multiple format standards
- Circular encoding/decoding integrity
2024-12-31 13:41:45 +01:00
2025-03-17 15:28:55 +00:00
3. **Format Detection**
- Automatic detection of invoice XML format
- Support for different XML namespaces
- Graceful handling of malformed XML
2024-12-31 13:41:45 +01:00
2025-04-03 20:45:26 +00:00
4. **Validation**
- Validate invoices against format-specific rules
- Detailed error reporting
- Support for different validation levels
2025-03-17 15:28:55 +00:00
By embracing `@fin.cx/xinvoice` , you simplify the handling of electronic invoice documents, fostering seamless integration across different financial processes, thus empowering practitioners with robust, flexible tools for VAT invoices in ZUGFeRD/Factur-X compliance or equivalent digital formats.
2024-04-22 16:30:55 +02:00
## License and Legal Information
2025-04-03 20:45:26 +00:00
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license ](license ) file within this repository.
2024-04-22 16:30:55 +02:00
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
### Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task .vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.