Skip to main content

Function Signature

function generateInvoiceXml(invoice: Invoice): string

Description

Converts a complete invoice object into an XML string representation. This function takes the structured invoice data and generates a properly formatted XML document that complies with Ecuadorian electronic invoicing standards. The generated XML is formatted with pretty printing for readability and includes all necessary namespaces and attributes required for electronic invoice processing.

Parameters

invoice
Invoice
required
The complete invoice object to convert to XML. This is typically the invoice object returned by the generateInvoice function.

Return Value

xml
string
A formatted XML string representation of the invoice. The XML is pretty-printed with proper indentation and includes all necessary XML namespaces and attributes required for Ecuadorian electronic invoicing.

Example

import { generateInvoice, generateInvoiceXml } from 'open-factura';

// First, generate the invoice object
const invoiceData = {
  infoTributaria: {
    ambiente: '1',
    tipoEmision: '1',
    razonSocial: 'Mi Empresa S.A.',
    nombreComercial: 'Mi Empresa',
    ruc: '1234567890001',
    codDoc: '01',
    estab: '001',
    ptoEmi: '001',
    secuencial: '000000001',
    dirMatriz: 'Av. Principal 123'
  },
  infoFactura: {
    fechaEmision: '15/03/2026',
    dirEstablecimiento: 'Av. Principal 123',
    obligadoContabilidad: 'SI',
    tipoIdentificacionComprador: '05',
    razonSocialComprador: 'Juan Pérez',
    identificacionComprador: '0987654321',
    direccionComprador: 'Calle Secundaria 456',
    totalSinImpuestos: '100.00',
    totalDescuento: '0.00',
    totalConImpuestos: {
      totalImpuesto: [
        {
          codigo: '2',
          codigoPorcentaje: '2',
          descuentoAdicional: '0.00',
          baseImponible: '100.00',
          tarifa: '12',
          valor: '12.00'
        }
      ]
    },
    importeTotal: '112.00',
    moneda: 'DOLAR',
    pagos: {
      pago: [
        {
          formaPago: '01',
          total: '112.00',
          plazo: '0',
          unidadTiempo: 'dias'
        }
      ]
    }
  },
  detalles: {
    detalle: [
      {
        codigoPrincipal: 'PROD001',
        codigoAuxiliar: 'AUX001',
        descripcion: 'Producto de ejemplo',
        cantidad: '1.000000',
        precioUnitario: '100.000000',
        descuento: '0.00',
        precioTotalSinImpuesto: '100.00',
        impuestos: {
          impuesto: [
            {
              codigo: '2',
              codigoPorcentaje: '2',
              tarifa: '12',
              baseImponible: '100.00',
              valor: '12.00'
            }
          ]
        }
      }
    ]
  }
};

const { invoice, accessKey } = generateInvoice(invoiceData);

// Convert the invoice to XML
const xml = generateInvoiceXml(invoice);

console.log(xml);

Example Output

<?xml version="1.0"?>
<factura xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         id="comprobante" 
         version="1.0.0">
  <infoTributaria>
    <ambiente>1</ambiente>
    <tipoEmision>1</tipoEmision>
    <razonSocial>Mi Empresa S.A.</razonSocial>
    <nombreComercial>Mi Empresa</nombreComercial>
    <ruc>1234567890001</ruc>
    <claveAcceso>1503202601123456789000110010010000000011234567890</claveAcceso>
    <codDoc>01</codDoc>
    <estab>001</estab>
    <ptoEmi>001</ptoEmi>
    <secuencial>000000001</secuencial>
    <dirMatriz>Av. Principal 123</dirMatriz>
  </infoTributaria>
  <infoFactura>
    <fechaEmision>15/03/2026</fechaEmision>
    <dirEstablecimiento>Av. Principal 123</dirEstablecimiento>
    <obligadoContabilidad>SI</obligadoContabilidad>
    <tipoIdentificacionComprador>05</tipoIdentificacionComprador>
    <razonSocialComprador>Juan Pérez</razonSocialComprador>
    <identificacionComprador>0987654321</identificacionComprador>
    <direccionComprador>Calle Secundaria 456</direccionComprador>
    <totalSinImpuestos>100.00</totalSinImpuestos>
    <totalDescuento>0.00</totalDescuento>
    <totalConImpuestos>
      <totalImpuesto>
        <codigo>2</codigo>
        <codigoPorcentaje>2</codigoPorcentaje>
        <descuentoAdicional>0.00</descuentoAdicional>
        <baseImponible>100.00</baseImponible>
        <tarifa>12</tarifa>
        <valor>12.00</valor>
      </totalImpuesto>
    </totalConImpuestos>
    <importeTotal>112.00</importeTotal>
    <moneda>DOLAR</moneda>
    <pagos>
      <pago>
        <formaPago>01</formaPago>
        <total>112.00</total>
        <plazo>0</plazo>
        <unidadTiempo>dias</unidadTiempo>
      </pago>
    </pagos>
  </infoFactura>
  <detalles>
    <detalle>
      <codigoPrincipal>PROD001</codigoPrincipal>
      <codigoAuxiliar>AUX001</codigoAuxiliar>
      <descripcion>Producto de ejemplo</descripcion>
      <cantidad>1.000000</cantidad>
      <precioUnitario>100.000000</precioUnitario>
      <descuento>0.00</descuento>
      <precioTotalSinImpuesto>100.00</precioTotalSinImpuesto>
      <impuestos>
        <impuesto>
          <codigo>2</codigo>
          <codigoPorcentaje>2</codigoPorcentaje>
          <tarifa>12</tarifa>
          <baseImponible>100.00</baseImponible>
          <valor>12.00</valor>
        </impuesto>
      </impuestos>
    </detalle>
  </detalles>
</factura>

Typical Workflow

  1. Create invoice data using the InvoiceInput type
  2. Generate the invoice object with generateInvoice()
  3. Convert to XML using generateInvoiceXml()
  4. Sign the XML using signXml()
  5. Submit for reception using documentReception()
  6. Get authorization using documentAuthorization()

See Also

Build docs developers (and LLMs) love