If you have ever built an e-invoicing app for Saudi Arabia (KSA), you know ZATCA Phase-1 (Fatoora) requires every simplified tax invoice to carry a Base64-encoded TLV (Tag-Length-Value) QR code with 5 fields:
- Tag 1: Seller Name
- Tag 2: Seller VAT Registration Number (15 digits)
- Tag 3: Invoice Timestamp (ISO 8601)
- Tag 4: Invoice Grand Total (including VAT)
- Tag 5: VAT Total Amount
The #1 Bug That Breaks Arabic Seller Names
Many quick JS snippets use string.length as the TLV Length byte:
// BROKEN FOR ARABIC TEXT: "شركة" has length 4, but is 8 UTF-8 bytes! const badLength = sellerName.length; ZATCA's Fatoora QR scanner validates exact UTF-8 byte lengths, not UTF-16 code units. Whenever a seller name has Arabic letters, string.length corrupts the TLV frame.
The Zero-Dependency TypeScript Fix (TextEncoder)
Here is the exact browser-native implementation we use in BillCraft:
export interface ZatcaInvoiceInput { sellerName: string; vatNumber: string; timestampIso: string; totalWithVat: string; vatTotal: string; } function encodeTlvField(tag: number, value: string): Uint8Array { const bytes = new TextEncoder().encode(value.trim()); const out = new Uint8Array(2 + bytes.length); out[0] = tag; out[1] = bytes.length; // Exact UTF-8 byte length (Arabic-safe!) out.set(bytes, 2); return out; } export function generateZatcaPhase1Base64(input: ZatcaInvoiceInput): string { const segments = [ encodeTlvField(1, input.sellerName), encodeTlvField(2, input.vatNumber), encodeTlvField(3, input.timestampIso), encodeTlvField(4, input.totalWithVat), encodeTlvField(5, input.vatTotal), ]; const merged = new Uint8Array(segments.reduce((s, a) => s + a.length, 0)); let offset = 0; for (const seg of segments) { merged.set(seg, offset); offset += seg.length; } let bin = ""; for (let i = 0; i < merged.length; i++) bin += String.fromCharCode(merged[i]); return btoa(bin); } Try It Live (Zero Sign-Up)
Test live Arabic + English ZATCA Phase-1 QR generation, A4 PDF export, digital signatures, and Quote-to-Invoice conversion in our free browser-local tool:
https://billcraft-app.pages.dev/?lang=ar&theme=dark
Need the Complete White-Label Next.js 15 Source Code?
If you want to self-host this for an agency or launch your own $0-server billing app (includes UTF-8 Arabic TLV unit tests, Client Vault CRM, CSV/JSON export, and 3 A4 templates), the complete source kit is on Codester:
https://www.codester.com/items/76658/billcraft-invoice-and-quote-studio-with-zatca-qr?ref=Ahme3d

