Factura NCF Electrónica RD: Guía 2026 para Crear Facturas Profesionales en República Dominicana
Factura NCF Electrónica RD · Generador y Solicitud de Pago `
);
newWin.document.close();
} else {
// Intento 2: modal embebido
$('pdf-iframe').src = pdfUri;
$('pdf-modal').classList.add('open');
}
} catch(e) {
// Intento 3: descarga directa con link
const blob = doc.output('blob');
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `Factura_${invNum}_${ncf}.pdf`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
setTimeout(() => URL.revokeObjectURL(url), 1000);
}
}function closePdfModal() {
$('pdf-modal').classList.remove('open');
$('pdf-iframe').src = '';
}// ===================== FUNCIÓN WORD CORREGIDA =====================
function exportWord() {
const { sym, code } = getCur();
const { subtotal, totalDisc, itbis, itbisPct, retention, retPct, total } = calcTotals();
const ncf = buildNCF(); const invNum = $('inv-num').value; const itbisPctV = parseFloat($('itbis-pct').value)||0;const itemRows = items.map(it => {
const { gross, discAmt, net } = calcItem(it);
const itbisItem = net * (itbisPctV/100);
return `
| ${it.desc||''} | ${sym}${fmt(it.rate)} | ${it.qty} | ${it.disc>0?`-${sym}${fmt(discAmt)} (${it.disc}%)`:'—'} | ${itbisPctV>0?`${sym}${fmt(itbisItem)}`:'—'} | ${sym}${fmt(net+itbisItem)} |
`;
}).join('');const bankHTML = banks.length ? banks.map(b=>`
${b.banco||'—'}
${b.titular?`
${b.titular}
`:''}
${b.numCuenta?`
Cuenta: ${b.numCuenta}
`:''}
${b.tipo?`
${b.tipo}`:''}
`).join('') : '
Sin cuentas bancarias
';const totRows = [
`
| Subtotal: | ${sym}${fmt(subtotal)} |
`,
totalDisc>0 ? `
| Descuento: | -${sym}${fmt(totalDisc)} |
` : '',
itbis>0 ? `
| ITBIS (${itbisPct}%): | ${sym}${fmt(itbis)} |
` : '',
retention>0 ? `
| Retención (${retPct}%): | -${sym}${fmt(retention)} |
` : '',
].join('');const notes = $('inv-notes').value;
const htmlContent = `
Factura — Solicitud de Pago de ServiciosComprobante Fiscal (NCF)
${ncf}
${$('e-name').value||'—'}
${$('e-addr').value.replace(/\n/g,'
')}${$('e-phone').value?'
'+$('e-phone').value:''}${$('e-email').value?'
'+$('e-email').value:''}
${$('e-rnc').value?`
RNC: ${$('e-rnc').value}`:''}
| Factura N° | ${invNum} |
| Fecha | ${fmtDate($('inv-date').value)} |
| Vence | ${fmtDate($('inv-due').value)} |
| Condición | ${$('inv-cond').value} |
| Moneda | ${code} |
Prestador / Quien Solicita el Pago ${$('e-name').value||'—'} ${$('e-addr').value.replace(/\n/g,' ')}${$('e-phone').value?' '+$('e-phone').value:''}${$('e-email').value?' '+$('e-email').value:''}${$('e-rnc').value?' RNC: '+$('e-rnc').value:''} | Empresa / Cliente que Pagará ${$('c-name').value||'—'} ${[$('c-addr').value,$('c-phone').value,$('c-email').value,$('c-rnc').value?'RNC: '+$('c-rnc').value:''].filter(Boolean).join(' ')} |
| Artículo / Servicio | Tarifa | Cant. | Descuento | ITBIS | Total |
${itemRows||'| Sin artículos |
'}
Datos para Realizar el Pago
${bankHTML} | ${totRows} | | Total a Pagar: | ${sym}${fmt(total)} |
SALDO DEUDOR · ${code}
${sym}${fmt(total)} | |
${notes?`
Notas: ${notes}
`:''}
Documento generado electrónicamente — República Dominicana
NCF: ${ncf} `;// ── CORRECCIÓN: múltiples métodos de descarga para Word ──
const filename = `Factura_${invNum}_${ncf}.doc`;
const fullHtml = '\ufeff' + htmlContent;// Método 1: Blob + object URL (funciona en browsers modernos fuera de sandbox)
try {
const blob = new Blob([fullHtml], { type: 'application/msword;charset=utf-8' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
setTimeout(() => URL.revokeObjectURL(url), 2000);
} catch(e1) {
// Método 2: data URI base64
try {
const b64 = btoa(unescape(encodeURIComponent(fullHtml)));
const a = document.createElement('a');
a.href = 'data:application/msword;charset=utf-8;base64,' + b64;
a.download = filename;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} catch(e2) {
// Método 3: abrir en nueva pestaña para copiar/guardar
const w = window.open('', '_blank');
if (w) {
w.document.write(htmlContent);
w.document.close();
alert('Abre el menú del navegador → "Guardar como" → elige formato Word o HTML.');
}
}
}
}