Skip to content

Upgrading to 2.0

metafloor edited this page Dec 14, 2019 · 4 revisions

The 2.0 release brings some much needed code changes:

  • UMD single file distribution for both the browser and nodejs.
  • New font rendering that does not rely on emscripten-compiled FreeType or bitmapped glyphs.
  • Embedded OCR-A and OCR-B fonts. No more async XHR requirements.
  • Drawing to a canvas is now synchronous.
  • Promise or callback interface for async operations.
  • Pluggable drawing interface.
  • Ability to retrieve the "raw" encoding data directly from the PostScript code.

These changes, especially the UMD structure, had a significant impact on the public interface and will require some minor changes to your code.

Generic Browser Usage

The number of scripts you need to include has dropped to one, either the full source version:

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

Or the minified version:

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

The script adds a single global object called bwipjs. See demo.html for detailed usage. See the main README for a full example.

React Usage

The structure of the exports object has changed and the rendering call is now synchronous. If your old code looked something like:

import bwipjs from 'bwip-js';
...
bwipjs('mycanvas', options, 
       function (err, cvs) {
           ...
       });

Your new code will be structured as:

import bwipjs from 'bwip-js';
...
try {
    // The return value is the actual canvas element
    let cvs = bwipjs.toCanvas('mycanvas', options);
} catch (e) {
   ...
}

Note the toCanvas method name. See the main README for more details.

Nodejs Usage

The bwipjs.toBuffer() interface has not changed (although it will now return a Promise if you omit the callback).

The http request/response handler is now called by name. The old code used:

    bwipjs(req, res);

The method name request is now needed:

    bwipjs.request(req, res);

Electron

If you were using the public bwipjs.toBuffer() interface, there are no required changes. As above, if you omit the callback, a Promise will now be returned.

You can’t perform that action at this time.