{
  "id": 21600992,
  "title": "How to Generate Official Saudi ZATCA Phase-1 TLV QR Codes in Pure TypeScript (Fixing the Arabic UTF-8 Bug)",
  "url": "https://urgent.news/2026/11/24/how-to-generate-official-saudi-zatca-phase-1-tlv-qr-codes-in-pure",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-24T13:33:32.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/andrew_zachary_ab67833254/how-to-generate-official-saudi-zatca-phase-1-tlv-qr-codes-in-pure-typescript-fixing-the-arabic-2ml2"
  },
  "original_language": "en",
  "account": "In the realm of e-invoicing applications for Saudi Arabia, ZATCA Phase-1 (known as Fatoora) mandates that every simplified tax invoice include a Base64-encoded TLV (Tag-Length-Value) QR code. This QR code contains five crucial fields: the seller's name, their VAT registration number, the invoice timestamp, the invoice grand total (including VAT), and the VAT total amount individually.\n\nHowever, a significant bug has plagued many quick JavaScript snippets that aim to generate these QR codes. The bug primarily lies in the calculation of the TLV (Tag-Length-Value) Length byte. In languages like JavaScript, the `string.length` method is commonly used as the TLV Length byte. While this works fine for English or Latin characters, it falls short when dealing with Arabic text. This is because `string.length` in JavaScript measures the number of Unicode code units, not the actual number of UTF-8 bytes.\n\nFor example, the Arabic word \"شركة\" (meaning \"company\") consists of 8 UTF-8 bytes, but the `string.length` method would incorrectly return 4. This discrepancy leads to the TLV frame being corrupted and subsequently, the ZATCA's Fatoora QR scanner failing to validate the invoice correctly.\n\nThe fix to this problem lies in utilizing the `TextEncoder` API, a browser-native implementation, in TypeScript. This method accurately calculates the UTF-8 byte length for the seller's name, even when it contains Arabic characters. The implementation is straightforward and does not require any external dependencies.\n\nA TypeScript function named `encodeTlvField` is created to encapsulate this logic. This function takes two parameters: the tag number and the value string. It first trims the value string, then encodes it into UTF-8 bytes using the `TextEncoder`. Following this, it creates a new `Uint8Array` with a length of 2 plus the actual byte length of the value. The tag number is placed at the first byte position, the byte length follows at the second byte, and the actual UTF-8 bytes are set starting from the third position. This ensures that the TLV frame is correctly formed for Arabic texts as well.\n\nFinally, the `generateZatcaPhase1Base64` function is provided to generate the complete Base64-encoded TLV QR code. This function accepts an object of type `ZatcaInvoiceInput`, which contains all the necessary data for an invoice. It maps each invoice field to a TLV field using the `encodeTlvField` function, merges these segments into a single byte array, and finally encodes this array into a Base64 string.\n\nFor those interested in testing this implementation, a live demo is available online. It allows users to input data, including Arabic seller names, and generates a QR code compliant with ZATCA Phase-1 requirements. This tool supports A4 PDF export, digital signatures, and conversion from quote to invoice, all without requiring any server setup. The complete source code, which includes comprehensive tests for UTF-8 Arabic TLV units, is available on Codester for those who wish to self-host this solution for their own applications.",
  "summary": "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…",
  "key_points": [
    "JavaScript's string.length method inaccurate for Arabic text",
    "TextEncoder API fixes UTF-8 byte length calculation",
    "Generate ZATCA Phase-1 QR codes with Base64 encoding"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}