This guide walks you through everything you need to issue your first CFDI 4.0 electronic invoice using CfdiUtils. By the end you will have installed the library via Composer, built aDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/eclipxe13/CfdiUtils/llms.txt
Use this file to discover all available pages before exploring further.
Comprobante node with an Emisor, Receptor, and Concepto, computed the totals automatically, digitally signed the document with your private key, validated the result against SAT’s schemas, and exported it as XML — all in a single PHP script.
Install
Add CfdiUtils to your project with Composer:libxml, dom, xsl, simplexml, mbstring, openssl, iconv, json) are present.
Create a CFDI 4.0
Load your certificate
Instantiate a The certificate object is used both to embed the
CfdiUtils\Certificado\Certificado object by passing the path to your SAT-issued CER file. CfdiUtils will read the serial number, RFC, and certificate contents automatically.NoCertificado and Certificado attributes in the XML and to verify that your private key matches the certificate during signing.Instantiate CfdiCreator40 with Comprobante attributes
Create a
CfdiCreator40 instance, passing an array of Comprobante attributes and your certificate. The constructor automatically sets NoCertificado, Certificado, and — by default — the Emisor RFC and name from the certificate.Add Emisor and Receptor
Retrieve the root
Comprobante node and add the Emisor and Receptor child nodes. Because the certificate was passed to the constructor, the Rfc and Nombre on Emisor are already populated; you only need to provide RegimenFiscal.Add a Concepto with a tax traslado
Call
addConcepto() on the Comprobante node. It returns the new Concepto element, on which you can chain addTraslado() to attach a transferred tax (IVA).Compute totals with addSumasConceptos()
Call
addSumasConceptos() to automatically calculate and set SubTotal, Total, Descuento, and the Impuestos node totals based on all Concepto entries. Pass null as the first argument to let the library build the sums object for you.Sign with addSello()
Call The
addSello() with the path to your PEM-encoded private key and its passphrase. The method builds the cadena de origen via XSLT, signs it with SHA-256, and stores the Base64 result in the Sello attribute.file:// URI prefix tells CfdiUtils to read the key from disk. If the key does not belong to the certificate loaded in step 1, a RuntimeException is thrown immediately.Validate the document
Call
validate() to run all standard creation validators — XSD schema checks, signature verification, and business-rule assertions. The method returns an Asserts collection you can inspect for errors and warnings.Complete Working Example
The following self-contained script combines all eight steps above into a single runnable example.Next Steps
Reading CFDI
Parse and extract data from existing CFDI 3.2, 3.3, and 4.0 XML documents.
Validation Overview
Deep dive into schema validation, signature checks, and custom business-rule validators.
Certificates
Work with SAT CER files, read serial numbers and RFC, and manage certificate chains.
XML Resolver
Configure the local SAT resource cache for XSD and XSLT files.