# Barcode generator — library for developers

Barcodes of 18 types (Code 128, Code 39, EAN, UPC, ITF, MSI, Pharmacode, Codabar) with labels, check digits, batch numbering, CSV and printable label sheets. Export to SVG, PNG, JPEG, WebP, PDF and ZIP.

## Installation

```sh
npm install @openlibsdev/barcode-generator
```

```js
import BarcodeGenerator from "https://cdn.jsdelivr.net/npm/@openlibsdev/barcode-generator@1.0.0/+esm";
```

## Usage

```js
import BarcodeGenerator from "@openlibsdev/barcode-generator";

const s = BarcodeGenerator.normalize({ format: "EAN13", data: "590123412345" });
console.log(BarcodeGenerator.isValid(s, s.data));   // true
const svg = BarcodeGenerator.svgFor(s, s.data);     // SVG markup as a string
document.body.insertAdjacentHTML("beforeend", svg);
await BarcodeGenerator.exportFile(s, "png");        // downloads the file
```

## API

### `BarcodeGenerator.normalize(input)`

Makes full settings from a partial object: missing fields get their defaults.

### `BarcodeGenerator.isValid(s, value)`

Checks a value for the chosen barcode type (length, characters, check digit).

### `BarcodeGenerator.svgFor(s, value)`

Barcode with its label as SVG markup.

### `BarcodeGenerator.drawSVG(s, value, svg)`

Draws a barcode into an existing <svg> element.

### `BarcodeGenerator.canvasFor(s, value, opts)`

Barcode as a <canvas>.

### `BarcodeGenerator.exportFile(s, kind, opts)`

Downloads one barcode or the whole batch: svg, png, jpeg, webp, pdf, zip.

### `BarcodeGenerator.printLayout(s)`

Builds a sheet of labels for printing.

### `BarcodeGenerator.values(s)`

All values of the batch: one value, a list or automatic numbering.

### `BarcodeGenerator.autonumber(o)`

Numbers from a start value with a step and a prefix.

### `BarcodeGenerator.csvValues(text)`

Reads values from CSV text.

### `BarcodeGenerator.toCsv(vals)`

Writes values to CSV text.

### `BarcodeGenerator.contrastOk(s)`

Whether the bar and background colors are contrasting enough for scanners.

### `BarcodeGenerator.FORMATS`

Barcode types with their rules.

### `BarcodeGenerator.PRESETS`

Ready label styles.

### `BarcodeGenerator.DEFAULTS`

Default settings.

### `BarcodeGenerator.findNamedColor(text, lang)`

Finds a named color by its name in the given language ("dark blue" → #00008b).

### `BarcodeGenerator.listNamedColors(lang)`

Lists the named colors with their names in the given language.

### `BarcodeGenerator.APP`

Name, version and release date of the library: {name, version, created}.

### `BarcodeGenerator.BACKDROPS`

Ready backgrounds by id, as SVG images.

### `BarcodeGenerator.BACKDROP_GROUPS`

Backgrounds by group: [group, [[id, English name, settings], …]].

### `BarcodeGenerator.FORMAT_GROUPS`

Barcode types by group: universal, retail, other.

### `BarcodeGenerator.LH`

Label height in pixels at scale 1.

### `BarcodeGenerator.LW`

Label width in pixels at scale 1.

### `BarcodeGenerator.MAX_VALUES`

Most values in one batch (200).

### `BarcodeGenerator.backdropSrc(s)`

Image of the chosen background (a ready one or an uploaded picture), or an empty string.

### `BarcodeGenerator.cardOn(s)`

Whether a white card is drawn under the code (on a background when turned on, and always on a label).

### `BarcodeGenerator.contrastRatio(a, b)`

Contrast ratio of two colors (1–21, as in WCAG).

### `BarcodeGenerator.fileName(s, value, ext)`

File name for a value: type_value.extension, with characters not allowed in file names replaced.

### `BarcodeGenerator.hasBackdrop(s)`

Whether a background is chosen.

### `BarcodeGenerator.hasLabel(s)`

Whether the label has a title, a price or a logo.

### `BarcodeGenerator.isPicture(s)`

Whether the result is a picture (background or label) rather than a bare code.

### `BarcodeGenerator.namedColorTexts(lang)`

Labels of the color field in the given language (the texts of the color picker), English for a missing language.

### `BarcodeGenerator.textFontData`

Data for a text and font block: the font list, loading a font style, adding a font file, the block's labels in a language and the default values.

### `BarcodeGenerator.validValues(s)`

Values of the batch that are valid for the chosen type.

## Dependencies

- @imagemagick/magick-wasm 0.0.35 (Apache-2.0)
- @imagemagick/magick-wasm 0.0.35 (Apache-2.0)
- @simonwep/pickr 1.10.2 (MIT)
- @simonwep/pickr 1.10.2 (MIT)
- dejavu-fonts-ttf 2.37.3 (Bitstream Vera Fonts License (free, redistributable); DejaVu changes are public domain)
- imagetracerjs 1.2.6 (Unlicense)
- jsbarcode 3.11.6 (MIT)
- jspdf 2.5.2 (MIT)
- jszip 3.10.1 ((MIT OR GPL-3.0-or-later))

- GitHub: https://github.com/OpenLibsDev/barcode-generator
- npm: https://www.npmjs.com/package/@openlibsdev/barcode-generator
- Open the app: https://openlibs.dev/en/app/barcode-generator/
