承接 mahdiabderraouf/facturx-php 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mahdiabderraouf/facturx-php

Composer 安装命令:

composer require mahdiabderraouf/facturx-php

包简介

A PHP package for managing Factur-x/ZUGFeRD compliant PDF invoices

README 文档

README

A PHP library for managing Factur-x/ZUGFeRD compliant PDF invoices.

Table of Contents

Features

  • XML generation: Generate Factur-X XML file from an Invoice object, supports minimum, basicwl and basic profiles.
  • PDF generation: Generate PDF-A3b Factur-X invoice from a given PDF file and a generated/provided XML file.
  • XML validation: Validates Factur-X XML against the offical Extension Schema Definition (XSD).
  • Parsing: Extract XML file from a Factur-X.

Requirements

Installation

Using composer

composer require mahdiabderraouf/facturx-php

Documentation

The full documentation can be found here.

Usage

Here are some quick examples of usage. For advanced usage please refer to the documentation

Generate minimum profile XML

use MahdiAbderraouf\FacturX\Builder;
use MahdiAbderraouf\FacturX\Enums\Profile;
use MahdiAbderraouf\FacturX\Models\Invoice;

// Create an invoice object from an array or you can use constructor instead (new Invoice (...))
$invoice = Invoice::createFromArray([
    'profile' => Profile::MINIMUM,
    'number' => 'F-202400001',
    'typeCode' => InvoiceTypeCode::COMMERCIAL_INVOICE,
    'issueDate' => DateTime::createFromFormat('Y-m-d', '2024-12-02'),
    'totalAmountWithoutVAT' => 80.00,
    'totalVATAmount' => 20.00,
    'totalAmountWithVAT' => 100.00,
    'amountDueForPayment' => 22.25,
    'buyer' => [
        'name' => 'Buyer NAME',
        'address' => [
            'countryCode' => 'FR',
        ],

        // optional
        'buyerReference' => 'BUYER-0001',
        'legalRegistrationIdentifier' => 'BUYER-SIRET',
        // Precise the legal registration scheme identifier if different from SIRET
        'schemeIdentifier' => SchemeIdentifier::SIREN,
    ],
    'seller' => [
        'name' => 'Seller NAME',
        'vatIdentifier' => 'FR12345678901',
        'address' => [
            'countryCode' => 'FR',
        ],

        // optional
        'legalRegistrationIdentifier' => 'SELLER-SIRET',
    ],

    // optional
    'businessProcessType' => 'A2', // default A1
    'purchaseOrderReference' => 'PO-202400005',
    'currencyCode' => 'EUR', // default EUR
]);

$xml = $invoice->toXml();
// Or
$xml = Builder::build($invoice);

Validate XML against XSD

Validate Factur-X XML against XSD. The XML source can be either a PDF file path, an XML file path or an XML string.

use MahdiAbderraouf\FacturX\Enums\Profile;
use MahdiAbderraouf\FacturX\Exceptions\InvalidXmlException;
use MahdiAbderraouf\FacturX\Exceptions\UnableToExtractXmlException;
use MahdiAbderraouf\FacturX\Validator;

$profile = Profile::MINIMUM;

// PDF path, XML path or XML string
$source = '/path/to/invoice.pdf';

try {
    Validator::validate(
        $source,

        // Validate against a specific profile, or auto-detect it from the XML.
        Profile::EN16931,
    );
} catch (UnableToExtractXmlException $e) {
    // Failed to extract XML from given PDF
    $message = $e->getMessage();
} catch (InvalidXmlException $e) {
    // array of LibXMLError
    $errors = $e->getErrors();
}

// Or simply check if its valid or not
$isValid = Validator::isValid($source);

Generate a Factur-X PDF

Using the static method Generator::generate you can embed an XML into a PDF file to generate a Factur-X file.

To ensure the integrity of every Factur-X file, the XML is validated before being embedded, so there is no need to validate it beforehand.

Important: The input PDF must be PDF/A-3b compliant as required by the Factur-X specification. This library does not convert PDFs to PDF/A-3b.

Please note about attachment relationship:

  • The only relationships that can be used for the XML file are Data, Source and Alternative.
  • In Germany the only relationship allowed is Alternative.
  • For profiles minimum and basicwl only the relationship Data is allowed.
  • It is not recommanded to use the relationship UNSPECIFIED when adding additional attachments.

If you are generating invoices in Germany, the profiles minimum and basicwl are not considered legally as an invoice since they don't contain enough information. Same rule will be applied in France in the future so you should be using at least the basic profile.

use MahdiAbderraouf\FacturX\Enums\Profile;
use MahdiAbderraouf\FacturX\Exceptions\InvalidXmlException;
use MahdiAbderraouf\FacturX\Generator;

$profile = Profile::BASIC;
$invoice = Invoice::createFromArray([...]);

try {
    $pdfString = Generator::generate(
        // path or PDF string
        '/path/to/Invoice.pdf',
        // Invoice, XML string or path
        $invoice,

        // optional
        AttachmentRelationship::DATA, // default one
        'outputPath.pdf',
        Profile::BASIC, // validates against a specific profile
        // add more attachments if needed
        [
            [
                'file' => 'extra_file.txt',
                // optional
                'filename' => 'Extra file name', // Defaults to the given file name
                'relationship' => AttachmentRelationship::SUPPLEMENT, // Defaults to AttachmentRelationship::UNSPECIFIED when not present
                'description' => 'This is some extra file description',
            ]
        ]
    );
} catch (InvalidXmlException $e) {
    $errors = $e->getErrors();
}

Parse Factur-X PDF

Parse a Factur-X PDF file, the parser will look by default for files factur-x.xml and zugferd-invoice.xml.

Please note that the filename zugferd-invoice.xml is not used since the version 2.3 of ZUGFeRD. You can specify the files you want to search for

use MahdiAbderraouf\FacturX\Enums\XmlFilename;
use MahdiAbderraouf\FacturX\Exceptions\UnableToExtractXmlException;
use MahdiAbderraouf\FacturX\Parser;

// PDF path
$pdf = '/path/to/invoice.pdf';

try {
    // this will look for 'factur-x.xml' or 'zugferd-invoice.xml' files inside the given PDF
    $xml = Parser::getXml(
        $pdf,
        XmlFilename::FACTUR_X, // look only for 'factur-x.xml'
    );
} catch (UnableToExtractXmlException $e) {
    // This probably means that the file is not Factur-X
    $message = $e->getMessage();
}

Bug reports

Create an issue using the bug report template.

Contributions

Contributions are welcome, here are some guidelines:

  • Code must be PSR-12
  • Provide an explanation of the changes
  • Provide the reason for the changes
  • If needed, update the documentation and provide an example.

Roadmap

  • Add support for generating en16931 and extended profiles
  • Factur-X parsing: parse a Factur-X file to an Invoice object.

mahdiabderraouf/facturx-php 适用场景与选型建议

mahdiabderraouf/facturx-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.61k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 12 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mahdiabderraouf/facturx-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mahdiabderraouf/facturx-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 mahdiabderraouf/facturx-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.61k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-22