Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/Eseperio/verifactu-php/llms.txt

Use this file to discover all available pages before exploring further.

QrGeneratorService generates the AEAT VERI*FACTU QR code that must be printed on every invoice. The QR encodes a verification URL containing the issuer’s NIF, the invoice series number, the issue date, and the SHA-256 huella. Any citizen or inspector can scan the QR to verify the invoice at the AEAT portal. Namespace: eseperio\verifactu\services\QrGeneratorService Requires: bacon/bacon-qr-code (installed automatically as a Composer dependency).
For most use cases, call Verifactu::generateInvoiceQr() or VerifactuService::generateInvoiceQr() instead of using this service directly. Those methods read the base verification URL from the global configuration automatically.

Constants

Destination constants

ConstantValueDescription
DESTINATION_STRING"string"Return the raw image binary (PNG) or text (SVG) as a PHP string.
DESTINATION_FILE"file"Save the QR to a temporary file and return its absolute path.

Renderer constants

ConstantValueDescription
RENDERER_GD"gd"PNG output via PHP’s built-in GD extension. Broadest compatibility.
RENDERER_IMAGICK"imagick"PNG output via ImageMagick. Higher quality; requires the imagick PHP extension.
RENDERER_SVG"svg"Scalable Vector Graphics output. Ideal for print and PDF embedding.

Methods

generateQr(InvoiceRecord $record, string $baseVerificationUrl, string $dest, int $size, string $engine): string

Generates a QR code for the given invoice record.
record
InvoiceRecord
required
An InvoiceSubmission or InvoiceCancellation. The hash field should be populated before calling this method — if hash is empty the huella query parameter is omitted from the QR URL.
baseVerificationUrl
string
required
The AEAT verification base URL. This is prepended to the query string. Example: "https://sede.agenciatributaria.gob.es/Sede/verifactu/verifactu.html".
dest
string
default:"DESTINATION_STRING"
Output destination. DESTINATION_STRING returns raw image/SVG data. DESTINATION_FILE writes to a temp file via sys_get_temp_dir() and returns the path. The file is named qr_<uniqid>.<ext>.
size
int
default:"300"
Output resolution in pixels (PNG) or viewBox units (SVG). AEAT regulations require the QR code to be legible — a minimum of 300 px / 300 pt is recommended.
engine
string
default:"RENDERER_GD"
Renderer backend. One of RENDERER_GD, RENDERER_IMAGICK, or RENDERER_SVG. Throws \RuntimeException if an unsupported value is supplied.
Returns: Raw PNG/SVG data string, or the absolute path to the temporary file.

buildQrContent() (protected)

Constructs the full QR payload URL from an InvoiceRecord. Although protected (not directly callable), understanding its output is important for debugging and compliance. URL format:
<baseVerificationUrl>?nif=<issuerNif>&num=<seriesNumber>&fecha=<issueDate>&huella=<hash>
The huella parameter is only appended when $record->hash is non-empty. For a VERI*FACTU submission the hash must always be set before generating the QR. Query parameters:
ParameterSourceExample
nifInvoiceId::$issuerNifB12345678
numInvoiceId::$seriesNumberFACT-2024-001
fechaInvoiceId::$issueDate01-01-2024
huellaInvoiceRecord::$hash3B4C1A2D...
Parameters are URL-encoded via PHP’s http_build_query().

Code examples

PNG via GD (default)

use eseperio\verifactu\services\QrGeneratorService;

$pngData = QrGeneratorService::generateQr(
    $invoice,
    'https://sede.agenciatributaria.gob.es/Sede/verifactu/verifactu.html',
    QrGeneratorService::DESTINATION_STRING,
    300,
    QrGeneratorService::RENDERER_GD
);

// Serve directly
header('Content-Type: image/png');
echo $pngData;

PNG via GD — save to file

$filePath = QrGeneratorService::generateQr(
    $invoice,
    'https://sede.agenciatributaria.gob.es/Sede/verifactu/verifactu.html',
    QrGeneratorService::DESTINATION_FILE,
    300,
    QrGeneratorService::RENDERER_GD
);

// $filePath is a temp path like /tmp/qr_64f3a1b2c3d4e.png
// Copy to permanent storage or embed in PDF
copy($filePath, '/var/invoices/qr_FACT-2024-001.png');

High-quality PNG via ImageMagick

$pngData = QrGeneratorService::generateQr(
    $invoice,
    'https://sede.agenciatributaria.gob.es/Sede/verifactu/verifactu.html',
    QrGeneratorService::DESTINATION_STRING,
    600,
    QrGeneratorService::RENDERER_IMAGICK
);

Scalable SVG (for PDF / print)

$svgData = QrGeneratorService::generateQr(
    $invoice,
    'https://sede.agenciatributaria.gob.es/Sede/verifactu/verifactu.html',
    QrGeneratorService::DESTINATION_STRING,
    400,
    QrGeneratorService::RENDERER_SVG
);

// Embed in HTML
echo '<div>' . $svgData . '</div>';

// Or save to disk
file_put_contents('/var/invoices/qr_FACT-2024-001.svg', $svgData);

Via VerifactuService (reads base URL from config)

use eseperio\verifactu\services\VerifactuService;
use eseperio\verifactu\services\QrGeneratorService;

// Assumes VerifactuService::config() has been called with qrValidationUrl
$pngData = VerifactuService::generateInvoiceQr(
    $invoice,
    QrGeneratorService::DESTINATION_STRING,
    300,
    QrGeneratorService::RENDERER_GD
);

File extension mapping

RendererFile extension
RENDERER_GD.png
RENDERER_IMAGICK.png
RENDERER_SVG.svg

Requirements by renderer

RendererPHP extension requiredNotes
RENDERER_GDext-gdAvailable in most PHP distributions by default.
RENDERER_IMAGICKext-imagickMust be compiled against ImageMagick.
RENDERER_SVGNonePure PHP SVG rendering via bacon/bacon-qr-code.
The hash field on the InvoiceRecord must be set before generating the QR for a VERI*FACTU invoice. If you call generateQr() on a freshly-constructed record without a hash, the huella parameter will be absent from the URL, which may cause AEAT’s verification portal to reject the lookup.

Build docs developers (and LLMs) love