How can I pass a Buffer to bwip-js?
ibliskavka opened this issue · comments
I need to create a Datamatrix from the following buffer using bwip-js.
02 00 25 4f b2 03 11 44 75 73 74 69 6e 20 4a 61 | ~~%O~~~Dustin Ja |
6d 65 73 20 42 75 63 6b | mes Buck~~~~~~~~ |
The closest I was able to get to was using latin1
encoding.
bwipjs.toCanvas(`canvas`, {
bcid: 'datamatrix',
text: buffer.toString('latin1)
});
When I read back the generated Datamatrix, I get the following result
02 00 25 4f c2 b2 03 11 44 75 73 74 69 6e 20 4a | ~~%O~~~~Dustin J |
61 6d 65 73 20 42 75 63 6b | ames Buck |
There is an extra c2
at index 4. I think maybe it has to do with non printable characters at indexes 0-7. That segments represents a date in a custom vendor format.
Is there a way to pass a raw buffer to bwip-js?
Very timely issue as this usage has only been supported in releases from just a couple of weeks ago. Convert the buffer to 'binary'
string and set binarytext:true
on the options object you pass in:
bwipjs.toCanvas(`canvas`, {
bcid: 'datamatrix',
text: buffer.toString('binary'),
binarytext: true,
});
You must use version 3.4.5 or 4.0.0+.
There may be a bug somewhere. I used binary encoding on a buffer, with binarytext
. I then read the generated Datamatrix using the online tool listed below. The data read by the tool was different than what was submitted to bwip-js.
it('compare binary encoding input with output', () => {
const data = Buffer.from(
'0200254fb2031144757374696e204a616d6573204275636b',
'hex'
);
const binaryEncoded = data.toString('binary');
// Generate a data matrix using bwip-js@4 with binarytext=true
// Read generated barcode using: https://online-barcode-reader.inliteresearch.com/
// Generated Datamatrix has a extra `c2` at position 8
const bwip = Buffer.from(
'0200254fc2b2031144757374696e204a616d6573204275636b',
'hex'
);
console.log({
expected: binaryEncoded,
dataMatrix: bwip.toString('binary'),
});
});
Result
{
expected: '\x02\x00%O²\x03\x11Dustin James Buck',
dataMatrix: '\x02\x00%O²\x03\x11Dustin James Buck'
}
I should be able to extract the original text from the Datamatrix.
Found the issue. Mistakenly marked binarytext as a bwip-js option but it is needed by the BWIPP wrapper code. Fixed in 4.0.1.
Thank you very much for the quick response! I confirmed the output is as expected, you may close this issue.
❤️