Skip to content

tecuity/barcode-generator

Repository files navigation

barcode-generator

Ever needed to generate a barcode in a JS-only environment? barcode-generator generates SVG barcodes in a browser, NodeJS, or anywhere else Javascript can run.

Features:

  • Javascript-only. No canvas or browser emulator needed
  • Support for the common 3 of 9 format
  • Returns a barcode as a base64 data url, or raw SVG
  • Zero dependencies

Demo Site

View a demo here

Installation

npm install @tecuity/barcode-generator

or

yarn add @tecuity/barcode-generator

Usage

For a default usage, just import and call the generator with a string like this:

import generateBarcode from '@tecuity/barcode-generator'

const barcode = generateBarcode("1234567")

And that's it! By default the generator will return a barcode as a base64-encoded data url. Ready to pop into the src attribute of an img element.

For further customization, you can pass an options object as the second parameter of the function like so:

import generateBarcode from '@tecuity/barcode-generator'

const barcode = generateBarcode("1234567", {raw: true, spacing: 10})

API Reference:

Key Type Default Description
spacing int || float 5 The spacing between characters, relative to the viewBox of the resulting SVG.
raw boolean false Returns the barcode as a raw SVG string rather than a base64 data-url.
height int || float 172.89 The viewBox height of the resulting SVG.

Contributors ✨


Christopher Patty

💻 📖

This project follows the all-contributors specification. Contributions of any kind welcome!