Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
B

bwip-js

> 编程语言
Open source

Barcode Writer in Pure JavaScript

2.4K stars0 likes2 views
WebsiteGitHub

About

Barcode Writer in Pure JavaScript

// Barcode Writer in Pure JavaScript

bwip-js is a translation to native JavaScript of the amazing code provided in Barcode Writer in Pure PostScript. The translated code can run on any modern browser or JavaScript-based server framework.

The library has encoding modules for over 100 different barcode types and standards. All linear and two-dimensional barcodes in common use (and many uncommon ones) are available. An exhaustive list of supported barcode types can be found at the end of this document. Barcodes are generated as PNG images (node-js and react-native) or to a canvas (browser) or as SVG (all platforms).

Note to users of ean2,ean5,ean8,ean13,upca,upce,isbn, .... A recent release of BWIPP added the guarddescent option, which changed final image height. Prior to the new release, symbol height was determined by the long "guard" bars. New releases determine height using the short bars. The resulting image height increases by 5px at scale=1, 10px at scale=2, etc. To achieve the old image height, decrease height by 1.75mm or set height to 23.6mm if using the default. This change appears in version 4.10.

Version 4.8 includes the new text layout features provided in recent releases of BWIPP. If you use a custom drawing interface and want to take advantage of the new capabilities, see the example drawing object and look for the font.rotate property passed into the text() method.

Status

  • Current bwip-js version is 4.11.4 (2026-08-19)
  • Current BWIPP version is 2026-05-28
  • Node.js compatibility: 6.0
  • Browser compatibility: Edge, Firefox, Chrome

Supported Platforms

  • Browser
  • Node.js
  • SVG (All Platforms)
  • React
  • React Native
  • Electron
  • Command Line

Links

  • Home Page
  • GitHub Repository
  • bwipjs Methods Reference
  • Online Barcode Generator
  • Online Barcode API
  • npm Page
  • BWIPP Documentation
  • Differences From BWIPP
  • Supported Barcode Types
  • Using Your Own Fonts
  • Annotated Example Drawing Object
  • Working with the Raw BWIPP Rendering Data

Installation

The bwip-js package has been partitioned into four platform-specific packages plus the main cross-platform package. If you install the main package and cannot get its exports to work with your build stack, try installing a platform-specific package. The platform-specific packages are ES modules only, so you will need a modern build environment. The exception is the node package, which also contains a require() compatible export.

You can download the main package using:

npm install bwip-js

Or one of the platform-specific packages using:

npm install @bwip-js/node
npm install @bwip-js/browser
npm install @bwip-js/react-native
npm install @bwip-js/generic

The node, browser and react-native packages include both an image rending interface (toCanvas() on the browser, toBuffer() on node, toDataURL() on react-native), plus the SVG and custom drawing context interfaces.

The generic package contains only exports that run on any platform, namely the SVG and custom drawing context interfaces.

The latest code can be obtained from github:

https://github.com/metafloor/bwip-js

The bwip-js branches and the npm versions are automatically sync'd with each release. The main package is located under the master branch; the platform-specific packages are maintained under their like-named branches.

Online Barcode Generator

An online barcode generator demonstrates all of the features of bwip-js. The app is also available in the root bwip-js directory. See Demo HTML App.

Online Barcode API

A bwip-js barcode service is available online, ready to serve up barcode images on demand.

You can generate barcodes from anywhere on the web. Embed the URLs in your HTML documents or retrieve the barcode images directly from your non-JavaScript server. (JavaScript-based servers should use the bwip-js code directly - it will be a lot more performant.)

For details on how to use this service, see Online Barcode API.

Working With bwip-js Methods

Most of the public methods of the bwip-js export use an options object. Only two values are required:

  • bcid : The name of the barcode type/symbol.
  • text : The text to encode.

All remaining options are optional, though you may find some quite useful.

The options values can be divided into two parts, bwip-js specific options and BWIPP options.

The bwip-js options are:

  • scaleX : The x-axis scaling factor. Must be an integer > 0. Default is 2.

  • scaleY : The y-axis scaling factor. Must be an integer > 0. Default is scaleX.

  • scale : Sets both the x-axis and y-axis scaling factors. Must be an integer > 0.

  • rotate : Allows rotating the image to one of the four orthogonal orientations. A string value. Must be one of:

    • 'N' : Normal (not rotated). The default.
    • 'R' : Clockwise (right) 90 degree rotation.
    • 'L' : Counter-clockwise (left) 90 degree rotation.
    • 'I' : Inverted 180 degree rotation.
  • binarytext : The default behavior is to encode the text string as UTF-8 binary bytes. If the text is already 8-bit encoded, you can disable this behavior by setting the flag to true.

  • padding : Shorthand for setting paddingtop, paddingleft, paddingright, and paddingbottom.

  • paddingwidth : Shorthand for setting paddingleft and paddingright.

  • paddingheight : Shorthand for setting paddingtop and paddingbottom.

  • paddingtop : Sets the height of the padding area, in points, on the top of the barcode image. Must be an integer >= 0. Rotates and scales with the image.

  • paddingleft : Sets the width of the padding area, in points, on the left side of the barcode image. Must be an integer >= 0. Rotates and scales with the image.

  • paddingright : Sets the width of the padding area, in points, on the right side of the barcode image. Must be an integer >= 0. Rotates and scales with the image.

  • paddingbottom : Sets the height of the padding area, in points, on the bottom of the barcode image. Must be an integer >= 0. Rotates and scales with the image.

  • backgroundcolor : This is actually a BWIPP option but is better handled by the bwip-js drawing code. Expects either a hex RGB, RRGGBB or CCMMYYKK string value or CSS-style #RGB or #RRGGBB string value.

For the BWIPP specific options, you will need to consult the BWIPP documentation to determine what options are available for each barcode type.

All of the BWIPP color options (e.g. barcolor, textcolor, bordercolor) can be specified using either RGB, RRGGBB or CCMMYYKK formats or the CSS-style #RGB and #RRGGBB formats.

Note that bwip-js normalizes the BWIPP width and height options to be in millimeters. The resulting images are rendered at 72 dpi. To convert to pixels, use a factor of 2.835 px/mm (72 dpi / 25.4 mm/in). The bwip-js scale options multiply the width, height, and padding.

An important note about the BWIPP width and height parameters.

Barcodes have the concept of module width (and height if a two-dimensional barcode). For linear barcodes, the module width is the width of the narrowest bar, and all other bar widths are a multiple of it. For 2D symbols, module width and height are the dimensions of the square or rectangle that defines the symbol's layout grid.

For a barcode to be "in spec", the individual module dimensions must be consistent throughout the symbol. With high resolution printing, you can add/subtract a dot to adjust the size of individual modules so the overall image meets the requested width or height, while still keeping the module size within spec. This is the intention behind BWIPP's width and height parameters.

bwip-js is designed for web usage, with a target display resolution of 72dpi. (All of BWIPP's internals are calculated in points and bwip-js just maps 1pt to 1px.) At that low resolution, it is not possible to add or subtract pixels without causing the symbol to go out of spec. Imagine a fairly common module width of 2px. If you add or subtract a pixel, you have changed the size by 50%. Typical barcode specs require module sizes to be within 5-10 pecent of nominal.

For this reason, bwip-js uses a constant module size so the resulting image is as large as possible, without exceeding the requested width or height. The design causes the rendered barcodes to grow in "quantums". An image will be X-pixels wide with a module with of 2px, and Y-pixels wide with a module width of 3px, and can not vary between those two sizes.

With bwip-js, the scale parameters can be thought of as requesting a particular module width. scale=1 maps to a 1px module. scale=2 is a 2px module. Etc.

When you specify width, you are effectively changing the scale of the final image. Internally, bwip-js calcuates the requested width x scale, then divides by the number of modules the symbol requires. The floor of that value is the module width (scale) of the rendered barcode.

Browser Usage

To use in a browser without a bundler, add the following to the head of your page:

<script type="text/javascript" src="url-path-to/bwip-js/dist/bwip-js-min.js"></script>

While developing your project, you may want to use dist/bwip-js.js to get better stack traces.

If you are using RequireJS or a common-js bundling utility, the bwip-js scripts are structured as UMDs and should work with your environment.

The scripts adds a single bwipjs global object. To draw a barcode to a canvas:

try {
    // The return value is the canvas element
    let canvas = bwipjs.toCanvas('mycanvas', {
        bcid:        'code128',       // Barcode type
        text:        '0123456789',    // Text to encode
        scale:       3,               // 3x scaling factor
        height:      10,              // Bar height, in millimeters
        includetext: true,            // Show human-readable text
        textxalign:  'center',        // Always good to set this
    });
} catch (e) {
    // `e` may be a string or Error object
}

The bwipjs.toCanvas() method takes two parameters:

  • The canvas on which to render the barcode. This can by an id string or the actual canvas element. The rendering will automatically resize the canvas to match the barcode image.
  • A bwip-js/BWIPP options object.

On return from toCanvas(), the barcode image will be fully rendered to the canvas.

If you would prefer to display the barcode using an `` tag or with CSS background-image, pass in a detached or hidden canvas, and use the canvas method HTMLCanvasElement.toDataURL to get a data URL. For exa

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言