sped-da

repository·master·Indexed 41 days ago

https://github.com/nfephp-org/sped-da

PHP library for generating auxiliary documents used in Brazilian Sped standards, transforming XML data into transport-ready PDF or HTML documents. It supports the generation of DANFE, Dacte, Damdfe, Dacce, NFCe, and DaBPe, following SEFAZ guidelines.

Tokens
12.5K
Snippets
43
Records
61
Agent score
99%

What's inside sped-da

  1. DANFSe layout and technical specifications

    master

    When generating DANFSe PDFs, keep the following layout rules in mind:

    • Format: Generated in A4 portrait, single page, including the national public consultation QR Code.
    • Missing Data: Fields absent in the XML are printed with a dash (-).
    • Taxation: The Federal Taxation line is omitted for periods after 2026.
    • IBS/CBS Logic: The IBS/CBS block prioritizes consolidated values from NFSe/infNFSe/IBSCBS. It uses infDPS/IBSCBS as a fallback for data only provided in the DPS.
    • Fonts: The engine uses standard fonts like Arial, Helvetica, or Times. While NT 008 mentions Microsoft Sans Serif, this package uses compatible standard fonts to preserve visual hierarchy and minimum sizes. For strict font compliance, the integrator must provide a licensed font and a compatible registration mechanism for the legacy FPDF engine.
  2. Core concepts of document generation in sped-da

    master

    The sped-da library is designed to generate auxiliary documents (such as DANFE, Dacte, Damdfe, Dacce, and NFCe) used in Sped standards.

    Key Principles

    • Purpose: These documents are intended exclusively to accompany goods during transport. They are not intended to be used as administrative or operational data sources; for those purposes, the XML file should be used.
    • Genericity: The classes are designed to be as generic and simple as possible, following SEFAZ guidelines. If you require a highly customized layout, the project recommends building your own PDF generator rather than modifying these classes.
    • Input Formats: Documents can be rendered from either an XML file or from the constructor classes provided by the specific document packages.
    • Output Formats: Rendering is typically performed in PDF or HTML.

    Architecture Evolution

    The current sped-da repository is being refactored and split into specialized repositories to improve modularity:

    • sped-da-common: Common classes used for PDF creation.
    • sped-da-nfe: Classes for NFe (Electronic Invoice) PDFs.
    • sped-da-cte: Classes for CTe (Electronic Bill of Lading) PDFs.
    • sped-da-mdfe: Classes for MDFe (Electronic Manifest of Goods) PDFs.

    Note: For NFC-e (Consumer Invoice), which is often printed on thermal POS printers, the posprint package is recommended instead of using PDF-based generation.

  3. Initialize DaBPe with BPe XML content

    master

    To create a DaBPe (Documento Auxiliar da BPe) PDF, instantiate the NFePHP\DA\BPe\Dabpe class by passing the complete XML content of a BPe as a single parameter.

    Important: Do not pass a file path; you must pass the actual XML string content. Ensure all characters are UTF-8 encoded.

    $xml = "<conteudo do xml>";
    $da = new Dabpe($xml);
  4. Instantiate Danfe2 with NFe XML

    master

    To use the Danfe2 class, you must instantiate it by passing a valid NFe (modelo 55) XML string. The XML can be either a raw XML or one that includes the authorization protocol. If the XML is invalid or not model 55, an exception will be thrown.

    // Only accepts properly formed XML and NFe model 55 (with or without authorization protocol)
    $xml = file_get_contents(__DIR__ . '/arquivo_xml_nfe_modelo_55.xml');
    $danfe2 = new Danfe2($xml);
  5. Generate a DaNFe PDF from XML

    master

    To generate a DaNFe (Documento Auxiliar da NF-e) as a PDF, use the Danfe class. You must instantiate the class by passing the XML content of the NF-e as a string to the constructor, and then call the render() method to produce the encoded PDF data.

    // 1. Instantiate the Danfe class with the XML string
    $danfe = new Danfe([$xmlString]);
    
    // 2. Render the PDF
    $pdf = $danfe->render();
  6. Generate DANFSe PDF from NFS-e XML

    master

    The NFePHP\DA\NFSe\Danfse class generates the national DANFSe v2.0 layout (per NT 008) locally as a PDF. It does not depend on an external national PDF generation API.

    To use it, instantiate the class with the NFS-e XML and call the render() method. You can optionally provide a logo to replace the default national NFS-e logo.

    // Initialize with the NFS-e XML
    $danfse = new Danfse($xml);
    
    // Render the PDF (returns binary content)
    $pdf = $danfse->render();
    
    // Render with a custom logo
    $pdf = $danfse->render($logo);
  7. Install sped-da via Composer

    master

    The sped-da package is distributed via Packagist and should be installed using Composer.

    For stable releases

    To install the stable version, run:

    composer require nfephp-org/sped-da

    Or add it to your composer.json:

    "require": {
        "nfephp-org/sped-da" : "^0.3"
    }

    For development versions

    If you need to use the development version (dev-master), you must first update your minimum-stability in composer.json to dev:

    "minimum-stability": "dev"

    Then run:

    composer require nfephp-org/sped-da:dev-master

    Or add it to your composer.json:

    "require": {
        "nfephp-org/sped-da" : "dev-master"
    }
  8. How product descriptions are constructed in DANFE

    master

    The DANFE generates product descriptions by aggregating several XML elements from the itemProd node. The description includes:

    • Base Product Name: The value from xProd.
    • Additional Information: Content from infAdProd (subject to the exibirInfAdProd setting).
    • Tax Information: If descProdInfoComplemento is enabled, it appends ICMS tax details (like vBCFCP, pFCP, vFCP, pICMSST, etc.) and ICMSUFDest details.
    • Batch/Lote Info: If descProdInfoLoteTxt is enabled, it appends batch information (nLote, qLote, dFab, dVal, vPMC) from the rastro tag.
    • ANVISA/FCI: Includes cProdANVISA and nFCI tags if present.
    • Order Item: If exibirNumeroItemPedido is enabled, it appends the order item number (nItemPed).

    Note: The description handles HTML entity decoding and can support line breaks if descProdQuebraLinha is enabled.

  9. How the Transport (Carrier) section is rendered

    master

    The transporte method constructs the 'TRANSPORTADOR / VOLUMES TRANSPORTADOS' section of the DANFE. It extracts and formats data from the following XML structures:

    • Carrier Info: Name (xNome), CNPJ/CPF, and Address (xEnder, xMun, UF, IE).
    • Freight Type: Maps modFrete to human-readable strings (e.g., 0-Por conta do Emit, 1-Por conta do Dest, 9-Sem Transporte).
    • Vehicle/Transport Details: ANT/RNTC code (RNTC), License Plate (placa), and State (UF).
    • Volumes: Aggregates total quantity (qVol), gross weight (pesoB), and net weight (pesoL), as well as species (esp), brand (marca), and numbering (nVol).
  10. How vehicle data is displayed for 0 Km vehicles

    master

    When a product contains the veicProd tag (typically for 0 Km vehicles), the DANFE includes a specific detailed section below the item. This section renders technical details such as:

    • Chassi, Cor, Cilindrada, Cmkg
    • Tipo (Vehicle Type), Nº Motor, Código Modelo, HP
    • Tipo Pintura, Marca/Modelo
    • Especie (Species), Combustivel (Fuel), Serial
    • Ano Fab/Mod, Distancia Entre Eixos

    The library uses internal mapping tables to convert numeric codes into human-readable strings for renavamEspecie, renavamTiposVeiculos, renavamCombustivel, and renavamTipoPintura.

  11. How additional data and contingency information are displayed

    master

    The dadosAdicionais section handles two main types of information:

    1. INFORMAÇÕES COMPLEMENTARES: General additional information provided in the XML.
    2. RESERVADO AO FISCO: Information reserved for tax authorities, including messages from the XML (xMsg) and contingency status.

    Contingency Statuses handled:

    • 4: CONTINGÊNCIA EPEC
    • 5: CONTINGÊNCIA FSDA
    • 6: CONTINGÊNCIA SVC-AN
    • 7: CONTINGÊNCIA SVC-RS

    When in contingency, the DANFE displays the justification (xJust) and the entry date/time (dhCont).