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.
- 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
View a demo here
npm install @tecuity/barcode-generator
or
yarn add @tecuity/barcode-generator
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})
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. |
Christopher Patty 💻 📖 |
This project follows the all-contributors specification. Contributions of any kind welcome!