Skip to content

Generate and read ZATCA QR payloads

A ZATCA invoice QR code contains a base64-encoded TLV (tag-length-value) payload. fatoority exposes that text so you can store it or pass it to a QR image renderer. This walkthrough reads an existing payload from a signed invoice and saves qr.txt.

Prepare the input

Download signed.xml into a working folder, or use an invoice you created with the signing guide. The download is a public demonstration fixture. Signed imports check local integrity; this establishes neither certificate issuer trust nor acceptance by ZATCA.

Choose the operation

Read the embedded payload when you want exactly what an existing signed invoice contains. When creating a new signed invoice, signing also produces its QR data; sign first, then read that payload. The command-line example additionally shows how to generate payloads from unsigned XML.

Run the example

Choose your language, then follow its setup and run instructions.

Install Rust and Cargo, then create a project:

cargo new fatoority-guide
cd fatoority-guide
cargo add fatoora-core

Save the code below as src/main.rs. Put the input files in the project root and run:

cargo run

Download this example.

use fatoora_core::invoice::xml::parse::parse_signed_invoice_xml;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let xml = std::fs::read_to_string("signed.xml")?;
    let invoice = parse_signed_invoice_xml(&xml)?;
    std::fs::write("qr.txt", invoice.qr_code())?;
    println!("{}", invoice.qr_code());
    Ok(())
}

Install the package in your Python environment:

pip install fatoority

Save the code as qr.py beside the input files, then run:

python qr.py

Download this example. See Python setup for package details.

from pathlib import Path
from fatoora import SignedInvoice

with SignedInvoice.from_file("signed.xml") as invoice:
    payload = invoice.qr_code
Path("qr.txt").write_text(payload, encoding="utf-8")
print(payload)

Use Node.js 22 or later. In a new working folder, install the package:

npm install fatoority

Save the code as qr.mjs beside the input files, then run:

node qr.mjs

Download this example. These examples run in Node.js and can also be adapted to TypeScript. In a browser, replace filesystem reads with your application's file input and configure the WASM asset as described in the browser setup. Keep objects in the WASM instance that created them; do not pass them between workers.

import { readFileSync, writeFileSync } from 'node:fs';
import { SignedInvoice } from 'fatoority';

const invoice = SignedInvoice.fromXml(readFileSync('signed.xml', 'utf8'));
const payload = invoice.qrCode;
writeFileSync('qr.txt', payload);
console.log(payload);

Use the .NET 8 SDK or later. Create a console application:

dotnet new console -n FatoorityGuide
cd FatoorityGuide
dotnet add package Fatoority

Save the code as Program.cs. Put the input files beside the project file, then run:

dotnet run

Download this example. The using declarations release native objects. Check the supported .NET platforms before deployment.

using Fatoora;

using var invoice = SignedInvoice.FromXml(File.ReadAllText("signed.xml"));
string payload = invoice.QrCode;
File.WriteAllText("qr.txt", payload);
Console.WriteLine(payload);

For this desktop example, use JDK 17+ and Gradle 8. Create an empty project folder and download build.gradle.kts and settings.gradle.kts into it. The build uses Maven Central and includes the JSON library needed by the validation example.

Save the code as src/main/kotlin/Main.kt (create the folders first). Put the input files in the project root and run:

gradle run

Download this example. use releases each native object. For an existing project, or Android's separate artifact and file handling, follow Kotlin setup. The binding also supports Maven; its dependency coordinates are listed there.

import com.fatoora.*
import java.io.File

fun main(args: Array<String>) {
    SignedInvoice.fromXml(File("signed.xml").readText()).use { invoice ->
        val payload = invoice.qrCode
        File("qr.txt").writeText(payload)
        println(payload)
    }
}

Use JDK 17+ and Maven for this walkthrough. In an empty project folder, save this pom.xml. It includes the library, a JSON parser for the validation example, and the plugin used by the run command.

Save the code as src/main/java/Guide.java (create the folders first). Put the input files beside pom.xml, then run:

mvn compile exec:java

Download this example. Try-with-resources releases native objects. The library itself supports Java 8+; see Java setup for platform prerequisites, Gradle installation, and Android configuration.

import com.fatoora.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;

public class Guide {
    static String read(String path) throws java.io.IOException {
        return new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
    }
    static void write(String path, String text) throws java.io.IOException {
        Files.write(Paths.get(path), text.getBytes(StandardCharsets.UTF_8));
    }
    public static void main(String[] args) throws Exception {
        try (SignedInvoice invoice = SignedInvoice.fromXml(read("signed.xml"))) {
            String payload = invoice.getQrCode();
            write("qr.txt", payload);
            System.out.println(payload);
        }
    }
}

Download and extract the fatoora-ffi-* archive for your platform from GitHub Releases. Save the example as qr.c in the extracted folder, beside the input files.

On Linux, compile the example and the supplied adapter, then run:

cc -std=c11 -Iinclude qr.c src/fatoora.c -L. -lfatoora_ffi -Wl,-rpath,'$ORIGIN' -o qr
./qr signed.xml > qr.txt

Download this example. For other platforms, follow the C installation and linking reference. The cleanup section releases handles and returned strings, including on failure.

#include "fatoora.h"
#include <stdio.h>

int main(int argc, char **argv) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s signed-invoice.xml\n", argv[0]);
        return 1;
    }
    FatooraSignedInvoice *invoice = NULL;
    FatooraError *error = NULL;
    char *qr = NULL;
    FatooraStatus status = fatoora_signed_invoice_from_file(argv[1], &invoice, &error);
    if (status == FATOORA_OK)
        status = fatoora_signed_invoice_qr_code(invoice, &qr, &error);
    if (status == FATOORA_OK) puts(qr);
    else fprintf(stderr, "Error %d: %s\n", (int)status, fatoora_error_message(error));
    fatoora_string_free(qr);
    fatoora_signed_invoice_free(invoice);
    fatoora_error_free(error);
    return status == FATOORA_OK ? 0 : 1;
}

Install the command-line tool with cargo install fatoora-cli, or extract a release binary and add it to your PATH. See the CLI guide for platform setup. Run beside your input files:

fatoora qr-read --invoice signed.xml > qr.txt

To generate a payload from an unsigned invoice instead:

fatoora qr --invoice invoice.xml > qr.txt

qr generates or regenerates a payload. qr-read extracts the payload already embedded in signed XML. Add --fail-on-signed to qr if your workflow should reject signed input. These commands write encoded text, not an image.

Check the result

The example writes qr.txt, containing the base64-encoded payload. It is text, not a PNG or SVG. Tags 1–5 carry seller and amount data; signing tags are present when supplied by the invoice. The encoded payload must fit the supported size limit.

Render a QR image

Read the payload from qr.txt, removing any trailing line break from terminal output. Pass that base64 text directly to your application's QR rendering library. Do not base64-encode the text again. Select the image size and output format for your receipt or invoice layout, then scan the rendered image to confirm it decodes to the same payload.

Image rendering is a separate step; fatoority does not produce an image file here.

If something goes wrong

Symptom What to check
No embedded payload can be read Use signed XML that contains QR data, or create a signed invoice first.
Generation reports missing seller fields Supply the seller name and VAT identifier in the invoice.
Payload exceeds a length limit Check field byte lengths, including UTF-8 text, and the encoded payload length.
QR scanning returns unexpected text Feed the encoded payload directly to the renderer; avoid extra encoding or wrapping.

Next, validate the invoice's QR and signature checks. See the QR reference for payload limits and error types.