# Angular Barcode Component

Barcode draws linear barcodes as SVG rendered on the server: Code 128, EAN-13, EAN-8, UPC-A, UPC-E, Code 39, ITF-14, Interleaved 2 of 5 and Codabar, with the human-readable digits, computed and validated check digits, an error state and PNG or SVG downloads. It has no dependency.

## Basic

Barcode encodes its value into a linear barcode, drawn as an inline SVG that is crisp at any zoom and rendered on the server. The default format is code128 , which encodes any ASCII text: the encoder picks the code sets that make the shortest symbol, two digits a bar pattern in runs of digits. The value is printed under the bars.

```typescript
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BarcodeModule } from '@exhaustive-ui/core/barcode';
import { InputTextModule } from '@exhaustive-ui/core/inputtext';

@Component({
    template: `
        <div class="card flex flex-col items-center gap-4">
            <p-barcode [value]="value" />
            <div class="flex flex-col gap-2 w-full max-w-80">
                <label for="barcode-basic-value">Value</label>
                <input id="barcode-basic-value" pInputText [(ngModel)]="value" maxlength="40" />
            </div>
        </div>
    `,
    standalone: true,
    imports: [BarcodeModule, InputTextModule, FormsModule]
})
export class BarcodeBasicDemo {
    value = 'EXH-2026-0042';
}
```

## Formats

format selects the symbology. Every encoder is part of the component, with no dependency. code128 : any ASCII text, with automatic switches between the code sets A, B and C for the shortest symbol. Shipping labels, logistics, tickets. ean13 and ean8 : retail products, 12 or 7 digits and a check digit. upca and upce : retail products in North America; UPC-E is the zero-suppressed form of a UPC-A number, with a number system 0 or 1. code39 : upper case letters, digits, space and - . $ / + % , common in industry and on badges. itf14 : shipping cartons, 13 digits and a check digit, with bearer bars above and below. itf : Interleaved 2 of 5, an even number of digits. codabar : digits and - $ : / . + between start and stop letters A to D, used by libraries and blood banks. Each format has the quiet zone its specification recommends, and its own text layout.

```typescript
import { Component } from '@angular/core';
import { BarcodeFormat, BarcodeModule } from '@exhaustive-ui/core/barcode';

@Component({
    template: `
        <div class="card">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-8 justify-items-center">
                @for (sample of samples; track sample.format) {
                    <figure class="m-0 flex flex-col items-center gap-2">
                        <p-barcode [value]="sample.value" [format]="sample.format" [height]="60" />
                        <figcaption class="text-sm text-muted-color">{{ sample.name }}</figcaption>
                    </figure>
                }
            </div>
        </div>
    `,
    standalone: true,
    imports: [BarcodeModule]
})
export class BarcodeFormatsDemo {
    samples: { format: BarcodeFormat; name: string; value: string }[] = [
        { format: 'code128', name: 'Code 128', value: 'Exhaustive-128' },
        { format: 'ean13', name: 'EAN-13', value: '400638133393' },
        { format: 'ean8', name: 'EAN-8', value: '9638507' },
        { format: 'upca', name: 'UPC-A', value: '03600029145' },
        { format: 'upce', name: 'UPC-E', value: '0123456' },
        { format: 'code39', name: 'Code 39', value: 'CODE-39' },
        { format: 'itf14', name: 'ITF-14', value: '1540014128876' },
        { format: 'itf', name: 'Interleaved 2 of 5', value: '12345678' },
        { format: 'codabar', name: 'Codabar', value: 'A40156B' }
    ];
}
```

## Text

The human-readable value is printed under the bars, the check digit included. EAN and UPC follow the layout of product packaging: the start, center and end guard bars go down between the digit groups, the first digit of EAN-13 sits in the left quiet zone, and the first and last digits of UPC-A and UPC-E are printed smaller outside the bars. The quiet zone grows when a digit needs more room. displayValue set to false hides the text, and fontSize sets its size in pixels. The font is the barcode.text.font.family design token, a monospace stack.

```typescript
import { Component } from '@angular/core';
import { BarcodeModule } from '@exhaustive-ui/core/barcode';

@Component({
    template: `
        <div class="card flex flex-wrap justify-center items-end gap-8">
            <p-barcode value="590123412345" format="ean13" [fontSize]="18" />
            <p-barcode value="03600029145" format="upca" />
            <p-barcode value="SHIP-7741" [displayValue]="false" ariaLabel="Code 128 barcode of the shipment SHIP-7741" />
        </div>
    `,
    standalone: true,
    imports: [BarcodeModule]
})
export class BarcodeTextDemo {
}
```

## Sizing and Colors

width is the width of the narrowest bar, a module, in pixels: 2 by default. Whole pixels keep the edges sharp on screen. height is the height of the bars without the text, 80 pixels by default. margin is the quiet zone on each side, in modules; leave it to the default of the format, as scanners need that light space to find the bars. The SVG shrinks to the width of its container on small screens. color paints the bars and the text, background the background and the quiet zones; both accept any CSS color and default to the barcode.foreground and barcode.background design tokens, black on white in both color schemes. Scanners read the contrast between dark bars and a light background: keep the bars dark, avoid red bars, which laser scanners cannot see, and inverted colors.

```typescript
import { Component } from '@angular/core';
import { BarcodeModule } from '@exhaustive-ui/core/barcode';

@Component({
    template: `
        <div class="card flex flex-col items-center gap-8">
            <p-barcode value="SMALL-01" [width]="1" [height]="40" [fontSize]="12" />
            <p-barcode value="LARGE-01" [width]="3" [height]="100" [fontSize]="20" />
            <div class="flex flex-wrap justify-center gap-8">
                <p-barcode value="4006381333931" format="ean13" color="#1e3a8a" background="#dbeafe" />
                <p-barcode value="96385074" format="ean8" color="#14532d" background="#f0fdf4" [margin]="10" />
            </div>
        </div>
    `,
    standalone: true,
    imports: [BarcodeModule]
})
export class BarcodeSizingDemo {
}
```

## Validation

EAN, UPC and ITF-14 take the digits with or without their check digit: when it is missing it is computed, when it is given it is validated. checksum adds the optional check character of Code 39 (modulo 43) and of Interleaved 2 of 5 (modulo 10); Code 128 always has its own. A value the format cannot encode draws no bars: a wrong check digit, a character the format does not have or a wrong length. The component displays an error state instead, announced to screen readers, and emits onError with a reason , invalid-character , invalid-length , invalid-check-digit or invalid-format , and an English message that tells what is wrong, for example the expected check digit. The error template replaces the error state, and its texts can be translated through labels . An empty value draws nothing and is not an error.

```typescript
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BarcodeErrorEvent, BarcodeModule } from '@exhaustive-ui/core/barcode';
import { InputTextModule } from '@exhaustive-ui/core/inputtext';

@Component({
    template: `
        <div class="card flex flex-col items-center gap-4">
            <div class="flex flex-col gap-2 w-full max-w-80">
                <label for="barcode-validation-value">EAN-13, try 4006381333932</label>
                <input id="barcode-validation-value" pInputText [(ngModel)]="value" (ngModelChange)="lastError = null" maxlength="20" inputmode="numeric" />
                <small class="text-muted-color">{{ lastError ? lastError.message : 'Valid EAN-13' }}</small>
            </div>
            <p-barcode [value]="value" format="ean13" (onError)="lastError = $event" />
            <p-barcode value="code 39" format="code39">
                <ng-template #error let-error>
                    <span class="text-sm">Code 39 has no lower case letter: {{ error.value }}</span>
                </ng-template>
            </p-barcode>
        </div>
    `,
    standalone: true,
    imports: [BarcodeModule, InputTextModule, FormsModule]
})
export class BarcodeValidationDemo {
    value = '400638133393';

    lastError: BarcodeErrorEvent | null = null;
}
```

## Download

download(filename, type, scale) saves the barcode as a PNG or SVG file and toDataURL(type, scale) returns it as a data URL; both return a promise and are only available in the browser. The PNG is scale times the size displayed, 1 by default: a whole number keeps every module the same width. toSVG() returns the SVG markup as a string, to print a label or to send to a server. The exports have the colors and the font displayed.

```typescript
import { Component } from '@angular/core';
import { BarcodeModule } from '@exhaustive-ui/core/barcode';
import { ButtonModule } from '@exhaustive-ui/core/button';
import { download as downloadIcon } from '@exhaustive-ui/icons/round';

@Component({
    template: `
        <div class="card flex flex-col items-center gap-4">
            <p-barcode #barcode value="1540014128876" format="itf14" />
            <div class="flex flex-wrap justify-center gap-2">
                <p-button label="PNG" [icon]="downloadIcon" (onClick)="barcode.download('carton', 'png', 3)" />
                <p-button label="SVG" [icon]="downloadIcon" severity="secondary" (onClick)="barcode.download('carton', 'svg')" />
            </div>
        </div>
    `,
    standalone: true,
    imports: [BarcodeModule, ButtonModule]
})
export class BarcodeDownloadDemo {
    downloadIcon = downloadIcon;
}
```

## Accessibility

Screen Reader The SVG has the img role and an accessible name made of the barcode text of labels , the format and the encoded value with its check digit, for example "Barcode EAN-13: 4006381333931", so that a screen reader user gets the number without a scanner. ariaLabel replaces it, for example with what the barcode is for, and ariaLabelledBy refers to a visible caption. The bars and the printed digits are inside the image and are not read one by one. When the value cannot be encoded, the error state is plain text that tells what is wrong, and a polite live region announces it when the value changes, "Invalid barcode value. Its check digit is wrong.". The warning icon is hidden from assistive technologies. Every text can be translated through labels . The bars stay black on white in dark mode, the contrast scanners need; the error state follows the color scheme.

## Barcode

Barcode draws a linear barcode as SVG: Code 128, EAN-13, EAN-8, UPC-A, UPC-E, Code 39, ITF-14, Interleaved 2 of 5 or Codabar, with the human-readable text, computed check digits and PNG or SVG export.

### Props

| Name | Type | Default | Description |
|------|------|---------|-------------|
| value | string | '' | Data to encode. EAN, UPC and ITF-14 take the digits with or without the check digit: it is computed when missing and validated when given. |
| format | BarcodeFormat | 'code128' | Symbology of the barcode. |
| width | number | 2 | Width of the narrowest bar, a module, in pixels. |
| height | number | 80 | Height of the bars, in pixels, without the text. |
| displayValue | boolean | true | Whether the human-readable value is printed under the bars. EAN and UPC have their guard bars extended and their digits grouped as on products. |
| fontSize | number | 16 | Size of the human-readable text, in pixels. |
| margin | number | - | Quiet zone, the light space on the left and the right of the bars that scanners need, in modules. Defaults to the size the format recommends: 10 for Code 128, Code 39, ITF and Codabar, 11 and 7 for EAN-13, 7 for EAN-8, 9 for UPC-A, 9 and 7 for UPC-E. |
| color | string | - | Color of the bars and the text, any CSS color. Defaults to the  `barcode.foreground`  design token. |
| background | string | - | Color of the background and the quiet zones, any CSS color. Defaults to the  `barcode.background`  design token. |
| checksum | boolean | false | Whether to add the optional check character of Code 39 (modulo 43) and ITF (modulo 10). EAN, UPC, ITF-14 and Code 128 always have their check digit. |
| ariaLabel | string | - | Accessible name of the barcode. Defaults to the  `barcode`  label followed by the format and the value, for example "Barcode EAN-13: 4006381333931". |
| ariaLabelledBy | string | - | Id of the element that labels the barcode. |
| labels | Partial<BarcodeLabels> | - | Texts of the component. Missing keys keep their default. |
| dt | Object | undefined | Defines scoped design tokens of the component. |
| unstyled | boolean | undefined | Indicates whether the component should be rendered without styles. |
| pt | BarcodePassThrough | undefined | Used to pass attributes to DOM elements inside the component. |
| ptOptions | PassThroughOptions | undefined | Used to configure passthrough(pt) options of the component. |

### Emits

| Name | Parameters | Description |
|------|------------|-------------|
| onError | event: BarcodeErrorEvent | Callback to invoke when the value cannot be encoded in the format: nothing is drawn and the error state is displayed. |

### Templates

| Name | Type | Description |
|------|------|-------------|
| error | TemplateRef<BarcodeErrorTemplateContext> | Custom content displayed in place of the barcode when the value is invalid. |

### Methods

| Name | Parameters | Return Type | Description |
|------|------------|-------------|-------------|
| toSVG |  | void | Returns the barcode as an SVG document, with the colors displayed and the text font. Empty when there is nothing to draw. |
| toDataURL | type: BarcodeExportType, scale: number | void | Returns the barcode as a data URL: a PNG image,  `scale`  times the size displayed, or an SVG document. |
| download | filename: string, type: BarcodeExportType, scale: number | void | Downloads the barcode as a PNG or SVG file. |

## Pass Through Options

| Name | Type | Description |
|------|------|-------------|
| host | PassThroughOption<HTMLElement, I> | Used to pass attributes to the host's DOM element. |
| root | PassThroughOption<HTMLElement, I> | Used to pass attributes to the root's DOM element, the host. |
| svg | PassThroughOption<SVGElement, I> | Used to pass attributes to the SVG element that draws the barcode, with the img role. |
| background | PassThroughOption<SVGRectElement, I> | Used to pass attributes to the background rectangle of the SVG. |
| bars | PassThroughOption<SVGPathElement, I> | Used to pass attributes to the path of the bars. |
| bearer | PassThroughOption<SVGPathElement, I> | Used to pass attributes to the path of the bearer bars of ITF-14. |
| text | PassThroughOption<SVGTextElement, I> | Used to pass attributes to the SVG text elements of the human-readable value. |
| error | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the element displayed in place of the barcode when the value is invalid. |
| errorIcon | PassThroughOption<SVGElement, I> | Used to pass attributes to the icon of the error state. |
| errorTitle | PassThroughOption<HTMLSpanElement, I> | Used to pass attributes to the title of the error state. |
| errorMessage | PassThroughOption<HTMLSpanElement, I> | Used to pass attributes to the reason of the error state. |
| liveRegion | PassThroughOption<HTMLSpanElement, I> | Used to pass attributes to the live region that announces errors. |

## Theming

### CSS Classes

| Class | Description |
|-------|-------------|
| p-barcode | Class name of the root element |
| p-barcode-svg | Class name of the SVG element |
| p-barcode-background | Class name of the background rectangle |
| p-barcode-bars | Class name of the bars path |
| p-barcode-bearer | Class name of the bearer bars path |
| p-barcode-text | Class name of the human-readable text |
| p-barcode-error | Class name of the error state |
| p-barcode-error-icon | Class name of the error icon |
| p-barcode-error-title | Class name of the error title |
| p-barcode-error-message | Class name of the error reason |

### Design Tokens

| Token | CSS Variable | Description |
|-------|--------------|-------------|
| barcode.foreground | --p-barcode-foreground | Color of the bars and the text of root |
| barcode.background | --p-barcode-background | Color of the background and the quiet zones of root |
| barcode.border.radius | --p-barcode-border-radius | Border radius of root |
| barcode.text.font.family | --p-barcode-text-font-family | Font family of text |
| barcode.text.font.weight | --p-barcode-text-font-weight | Font weight of text |
| barcode.error.background | --p-barcode-error-background | Background of error |
| barcode.error.border.color | --p-barcode-error-border-color | Border color of error |
| barcode.error.color | --p-barcode-error-color | Color of error |
| barcode.error.gap | --p-barcode-error-gap | Gap of error |
| barcode.error.min.width | --p-barcode-error-min-width | Min width of error |
| barcode.error.padding | --p-barcode-error-padding | Padding of error |
| barcode.error.font.size | --p-barcode-error-font-size | Font size of error |
| barcode.error.icon.size | --p-barcode-error-icon-size | Size of error icon |
| barcode.error.icon.color | --p-barcode-error-icon-color | Color of error icon |
| barcode.error.title.font.weight | --p-barcode-error-title-font-weight | Font weight of error title |
| barcode.error.title.color | --p-barcode-error-title-color | Color of error title |

