Pdfmake: can i create code128 barcode in it... i am not able to figure out how

Created on 28 Mar 2017  路  4Comments  路  Source: bpampuch/pdfmake

i am totally new to node and all.. my basic problem is can i create code128 barcode in it... i am not able to figure out how

Most helpful comment

barcode-generator library did not work for me, because of error 'Canvas is not a constructor' inside one of barcode-generator's files.

So I used another library (https://www.npmjs.com/package/jsbarcode) that let me pass my own canvas instance created the way I wanted...

// import jsbarcode library and Canvas
var JsBarcode = require('jsbarcode');
const { createCanvas } = require('canvas');

const canvas = createCanvas(0, 0);

// create a bar code with the number/text I want and populate the canvas with it..
JsBarcode(canvas, '123456');

// draw the code bar on pdfmake as an image...
{ image: canvas.toDataURL(), width: 80, height: 40 }, // bar code as image

That's it!

All 4 comments

They are supported only QR codes. Other codes you can insert into the PDF as an image that you prepare in a different library.

@liborm85
Thanks for the response but i have finally managed to do it through barcode generator

var Barc = require('barcode-generator')
       ,barc = new Barc();
var awb = "<Tracking Number>";    
//create a 300x200 px image with the barcode 1234
var buf = barc.code128(awb, width=200, height=100);

and in docDefinition just added

header("Scan for the Tracking "),
content: [
{
    image: buf, 
}, '\n',
]

barcode-generator library did not work for me, because of error 'Canvas is not a constructor' inside one of barcode-generator's files.

So I used another library (https://www.npmjs.com/package/jsbarcode) that let me pass my own canvas instance created the way I wanted...

// import jsbarcode library and Canvas
var JsBarcode = require('jsbarcode');
const { createCanvas } = require('canvas');

const canvas = createCanvas(0, 0);

// create a bar code with the number/text I want and populate the canvas with it..
JsBarcode(canvas, '123456');

// draw the code bar on pdfmake as an image...
{ image: canvas.toDataURL(), width: 80, height: 40 }, // bar code as image

That's it!

barcode-generator library did not work for me, because of error 'Canvas is not a constructor' inside one of barcode-generator's files.

So I used another library (https://www.npmjs.com/package/jsbarcode) that let me pass my own canvas instance created the way I wanted...

// import jsbarcode library and Canvas
var JsBarcode = require('jsbarcode');
const { createCanvas } = require('canvas');

const canvas = createCanvas(0, 0);

// create a bar code with the number/text I want and populate the canvas with it..
JsBarcode(canvas, '123456');

// draw the code bar on pdfmake as an image...
{ image: canvas.toDataURL(), width: 80, height: 40 }, // bar code as image

That's it!

Hi @Girdacio,
could you tell how to import jsBardcode library and canvas
i have installed it but i dont know where and how to import

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kamilkp picture kamilkp  路  3Comments

Masber picture Masber  路  3Comments

SummerSonnet picture SummerSonnet  路  3Comments

jkd003 picture jkd003  路  3Comments

svenyonson picture svenyonson  路  3Comments