lindell / JsBarcode

Barcode generation library written in JavaScript that works in both the browser and on Node.js
http://lindell.me/JsBarcode
MIT License
5.45k stars 1.1k forks source link

How can I generate a brazilian bank slip ("boleto bancário") #297

Open ghashi opened 5 years ago

ghashi commented 5 years ago

Which format should I use (CODE128; EAN / UPC; CODE39; ITF-14; MSI; Pharmacode; Codabar;) to generate a brazilian "Boleto bancário" or bank slip? Or is it possible to do this using this lib? This thread says I should use 'itf' but it doesn't seem to be working.

I tested using this number "03399.33335 33886.349134 88111.901026 6 79580000014289", but the generated barcode isn't recognized by any bank app barcode scanner.

alexandrefugita commented 5 years ago

Actually, in order to make a valid barcode for Brazilian bank slips, the code you need to transform is not the direct billet number - instead, it is a combination of a rearrangement of the digits, and some checksum digits. Simply put - after you generate the 47-digit billet number, you can do something like:

getBarcode(billetNumber: string) {
    return billetNumber.replace(/^(\d{4})(\d{5})\d{1}(\d{10})\d{1}(\d{10})\d{1}(\d{15})$/, '$1$5$2$3$4');
}

Then you can input such value using the itf encoding and it should return a valid barcode to be scanned by Brazilian banking systems.

See references at the Febraban site (current Brazilian bank slips standards) and at this repo (a nice lib from a Brazilian developer which explains in further detail how it works, js-wise).