Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions api/v1/TICKET_DOCUMENT_V1.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Define las características de la impresora:

| Campo | Tipo | Requerido | Descripción | Valores |
|--------|--------|-----------|-------------------------------|-------------------------------------------------------|
| `type` | string | ✓ | Tipo de comando | text, image, separator, feed, cut, qr, table, barcode |
| `type` | string | ✓ | Tipo de comando | text, image, separator, feed, cut, qr, pdf417, table, barcode, raw, pulse, beep |
| `data` | object | ✓ | Datos específicos del comando | Varía según el tipo |

### 1. Text Command
Expand Down Expand Up @@ -147,7 +147,7 @@ Imprime imágenes en formato base64:
| `pixel_width` | integer | | Ancho deseado en píxeles | 128 | Mínimo: 1 |
| `align` | string | | Alineación de la imagen | center | left, center, right |
| `threshold` | integer | | Umbral B/N (0-255) | 128 | 0-255 |
| `dithering` | string | | Algoritmo de dithering | atkinson | threshold, atkinson |
| `dithering` | string | | Algoritmo de dithering | atkinson | threshold, atkinson, floyd_steinberg, ordered |
| `scaling` | string | | Algoritmo de escalado | bilinear | bilinear, nns |

### 3. Barcode Command
Expand All @@ -171,7 +171,7 @@ Genera códigos de barras:

| Campo | Tipo | Requerido | Descripción | Default | Valores |
|----------------|---------|-----------|-----------------------|---------|--------------------------------------------------------|
| `symbology` | string | ✓ | Tipo de simbología | | upca, upce, ean13, ean8, code39, code128, itf, codabar |
| `symbology` | string | ✓ | Tipo de simbología | | upca, upce, ean13, ean8, code39, code128, itf, codabar, code93, gs1_128, code128_auto |
| `data` | string | ✓ | Datos a codificar | | 1-25 caracteres |
| `width` | integer | | Ancho del módulo | | 2-6 |
| `height` | integer | | Altura en puntos | | 1-255 |
Expand Down Expand Up @@ -208,6 +208,35 @@ Genera códigos QR:
| `logo` | string | | Logo en Base64 | | |
| `circle_shape` | boolean | | Usar bloques circulares (solo para QR < 256px) | | |

### 4b. PDF417 Command

Genera códigos bidimensionales PDF417:

```json
{
"type": "pdf417",
"data": {
"data": "PDF417-PAYLOAD-DATA",
"columns": 0,
"rows": 0,
"module_width": 3,
"row_height": 3,
"error_correction": 2,
"align": "center"
}
}
```

| Campo | Tipo | Requerido | Descripción | Default | Valores |
|--------------------|---------|-----------|----------------------------------------------|---------|---------------------|
| `data` | string | ✓ | Datos a codificar en el PDF417 | | |
| `columns` | integer | | Número de columnas (0 = auto) | 0 | 0-30 |
| `rows` | integer | | Número de filas (0 = auto) | 0 | 0, 3-90 |
| `module_width` | integer | | Ancho del módulo en dots | 3 | 2-8 |
| `row_height` | integer | | Multiplicador de altura de fila | 3 | 2-8 |
| `error_correction` | integer | | Nivel de corrección de errores | 2 | 0-8 |
| `align` | string | | Alineación del código | center | left, center, right |

### 5. Table Command

Crea tablas formateadas:
Expand Down
70 changes: 68 additions & 2 deletions api/v1/ticket_document.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"feed",
"cut",
"qr",
"pdf417",
"table",
"barcode",
"raw",
Expand Down Expand Up @@ -124,6 +125,9 @@
{
"$ref": "#/definitions/QRCommand"
},
{
"$ref": "#/definitions/PDF417Command"
},
{
"$ref": "#/definitions/TableCommand"
},
Expand Down Expand Up @@ -295,7 +299,9 @@
"type": "string",
"enum": [
"threshold",
"atkinson"
"atkinson",
"floyd_steinberg",
"ordered"
],
"default": "atkinson"
},
Expand Down Expand Up @@ -410,6 +416,63 @@
}
}
},
"PDF417Command": {
"type": "object",
"required": [
"data"
],
"properties": {
"data": {
"type": "string",
"description": "PDF417 barcode data to encode"
},
"columns": {
"type": "integer",
"description": "Number of columns (0-30, 0 = auto)",
"minimum": 0,
"maximum": 30,
"default": 0
},
"rows": {
"type": "integer",
"description": "Number of rows (0 or 3-90, 0 = auto)",
"minimum": 0,
"maximum": 90,
"default": 0
},
"module_width": {
"type": "integer",
"description": "Module width in dots (2-8)",
"minimum": 2,
"maximum": 8,
"default": 3
},
"row_height": {
"type": "integer",
"description": "Row height multiplier (2-8)",
"minimum": 2,
"maximum": 8,
"default": 3
},
"error_correction": {
"type": "integer",
"description": "Error correction level (0-8)",
"minimum": 0,
"maximum": 8,
"default": 2
},
"align": {
"type": "string",
"enum": [
"left",
"center",
"right"
],
"description": "PDF417 barcode alignment",
"default": "center"
}
}
},
"BarcodeCommand": {
"type": "object",
"required": [
Expand All @@ -427,7 +490,10 @@
"code39",
"code128",
"itf",
"codabar"
"codabar",
"code93",
"gs1_128",
"code128_auto"
],
"description": "Barcode symbology type",
"default": "code128"
Expand Down
157 changes: 157 additions & 0 deletions pkg/commands/qrcode/pdf417.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package qrcode

import (
"errors"
"fmt"

"github.com/adcondev/poster/pkg/commands/shared"
)

// PDF417 command constants and limits.
const (
// MaxPDF417Columns defines the maximum number of columns in a PDF417 barcode.
MaxPDF417Columns = 30
// MinPDF417Rows defines the minimum number of rows when specified.
MinPDF417Rows = 3
// MaxPDF417Rows defines the maximum number of rows in a PDF417 barcode.
MaxPDF417Rows = 90
// MinPDF417ModuleWidth defines the minimum module width in dots.
MinPDF417ModuleWidth = 2
// MaxPDF417ModuleWidth defines the maximum module width in dots.
MaxPDF417ModuleWidth = 8
// MinPDF417RowHeight defines the minimum row height multiplier.
MinPDF417RowHeight = 2
// MaxPDF417RowHeight defines the maximum row height multiplier.
MaxPDF417RowHeight = 8
// MaxPDF417ErrorCorrection defines the maximum error correction level.
MaxPDF417ErrorCorrection = 8
// MaxPDF417DataLength defines the maximum byte length of PDF417 data.
MaxPDF417DataLength = 65532
)

var (
// ErrPDF417Columns indicates an invalid column count (0-30).
ErrPDF417Columns = errors.New("invalid PDF417 columns (try 0-30)")
// ErrPDF417Rows indicates an invalid row count (0 or 3-90).
ErrPDF417Rows = errors.New("invalid PDF417 rows (try 0, or 3-90)")
// ErrPDF417ModuleWidth indicates an invalid module width (2-8).
ErrPDF417ModuleWidth = errors.New("invalid PDF417 module width (try 2-8)")
// ErrPDF417RowHeight indicates an invalid row height (2-8).
ErrPDF417RowHeight = errors.New("invalid PDF417 row height (try 2-8)")
// ErrPDF417ErrorCorrection indicates an invalid error correction level (0-8).
ErrPDF417ErrorCorrection = errors.New("invalid PDF417 error correction level (try 0-8)")
// ErrPDF417DataEmpty indicates empty PDF417 data payload.
ErrPDF417DataEmpty = errors.New("PDF417 data cannot be empty")
// ErrPDF417DataTooLong indicates data payload exceeds 65532 bytes.
ErrPDF417DataTooLong = errors.New("PDF417 data too long (maximum 65532 bytes)")
)

// SetPDF417Columns sets the number of columns in the PDF417 data area (fn = 65).
// Range: 0 <= columns <= 30 (0 sets automatic processing).
func SetPDF417Columns(columns byte) ([]byte, error) {
if columns > MaxPDF417Columns {
return nil, fmt.Errorf("%w: columns=%d", ErrPDF417Columns, columns)
}
return []byte{shared.GS, '(', 'k', 0x03, 0x00, 48, 65, columns}, nil
}

// SetPDF417Rows sets the number of rows in the PDF417 data area (fn = 66).
// Range: rows == 0 || (rows >= 3 && rows <= 90) (0 sets automatic processing).
func SetPDF417Rows(rows byte) ([]byte, error) {
if rows != 0 && (rows < MinPDF417Rows || rows > MaxPDF417Rows) {
return nil, fmt.Errorf("%w: rows=%d", ErrPDF417Rows, rows)
}
return []byte{shared.GS, '(', 'k', 0x03, 0x00, 48, 66, rows}, nil
}

// SetPDF417ModuleWidth sets the module width of the PDF417 symbol (fn = 67).
// Range: 2 <= width <= 8 dots.
func SetPDF417ModuleWidth(width byte) ([]byte, error) {
if width < MinPDF417ModuleWidth || width > MaxPDF417ModuleWidth {
return nil, fmt.Errorf("%w: width=%d", ErrPDF417ModuleWidth, width)
}
return []byte{shared.GS, '(', 'k', 0x03, 0x00, 48, 67, width}, nil
}

// SetPDF417RowHeight sets the row height of the PDF417 symbol (fn = 68).
// Range: 2 <= height <= 8 (height = module width × row height).
func SetPDF417RowHeight(height byte) ([]byte, error) {
if height < MinPDF417RowHeight || height > MaxPDF417RowHeight {
return nil, fmt.Errorf("%w: height=%d", ErrPDF417RowHeight, height)
}
return []byte{shared.GS, '(', 'k', 0x03, 0x00, 48, 68, height}, nil
}

// SetPDF417ErrorCorrection sets the error correction level for PDF417 (fn = 69, m = 48).
// Range: 0 <= level <= 8.
func SetPDF417ErrorCorrection(level byte) ([]byte, error) {
if level > MaxPDF417ErrorCorrection {
return nil, fmt.Errorf("%w: level=%d", ErrPDF417ErrorCorrection, level)
}
return []byte{shared.GS, '(', 'k', 0x04, 0x00, 48, 69, 48, 48 + level}, nil
}

// StorePDF417Data stores symbol data into the PDF417 symbol storage area (fn = 80, m = 48).
// Range: 1 <= len(data) <= 65532 bytes.
func StorePDF417Data(data []byte) ([]byte, error) {
if len(data) == 0 {
return nil, ErrPDF417DataEmpty
}
if len(data) > MaxPDF417DataLength {
return nil, fmt.Errorf("%w: %d bytes", ErrPDF417DataTooLong, len(data))
}

length := len(data) + 3
pL := byte(length & 0xFF)
pH := byte((length >> 8) & 0xFF)

cmd := make([]byte, 0, 8+len(data))
cmd = append(cmd, shared.GS, '(', 'k', pL, pH, 48, 80, 48)
cmd = append(cmd, data...)
return cmd, nil
}

// PrintPDF417 prints the PDF417 symbol data stored in the symbol storage area (fn = 81, m = 48).
func PrintPDF417() []byte {
return []byte{shared.GS, '(', 'k', 0x03, 0x00, 48, 81, 48}
}

// ComposePDF417 combines all setup commands, data storage, and the print command into a single atomic byte sequence.
func ComposePDF417(columns, rows, width, rowHeight, ecLevel byte, data []byte) ([]byte, error) {
colCmd, err := SetPDF417Columns(columns)
if err != nil {
return nil, fmt.Errorf("pdf417 set columns: %w", err)
}
rowCmd, err := SetPDF417Rows(rows)
if err != nil {
return nil, fmt.Errorf("pdf417 set rows: %w", err)
}
widthCmd, err := SetPDF417ModuleWidth(width)
if err != nil {
return nil, fmt.Errorf("pdf417 set module width: %w", err)
}
heightCmd, err := SetPDF417RowHeight(rowHeight)
if err != nil {
return nil, fmt.Errorf("pdf417 set row height: %w", err)
}
ecCmd, err := SetPDF417ErrorCorrection(ecLevel)
if err != nil {
return nil, fmt.Errorf("pdf417 set error correction: %w", err)
}
storeCmd, err := StorePDF417Data(data)
if err != nil {
return nil, fmt.Errorf("pdf417 store data: %w", err)
}
printCmd := PrintPDF417()

totalLen := len(colCmd) + len(rowCmd) + len(widthCmd) + len(heightCmd) + len(ecCmd) + len(storeCmd) + len(printCmd)
buf := make([]byte, 0, totalLen)
buf = append(buf, colCmd...)
buf = append(buf, rowCmd...)
buf = append(buf, widthCmd...)
buf = append(buf, heightCmd...)
buf = append(buf, ecCmd...)
buf = append(buf, storeCmd...)
buf = append(buf, printCmd...)
return buf, nil
}
Loading
Loading