Spread the love

OBS: barcodes4me has closed their service

Michael Francois Knudsen Has made me aware that one of the largest downsides to using third party has occurred, barcodes4.me has decided to close their service from January 2021, luckily Michael Francois Knudsen has been so kind as to share his barcode code, which you can see below:

local procedure GenerateBarcode(ItemNo: Text[20])
var
  ClientHttp: HttpClient;
  Request: HttpRequestMessage;
  Response: HttpResponseMessage;
  Instr: InStream;
  Method: text;
  Outstr: Outstream;
  TypeHelper: Codeunit "Type Helper";
begin
  Method := (STRSUBSTNO('https://barcode.tec-it.com/barcode.ashx?data='+TypeHelper.UrlEncode(ItemNo)+'&code=Code128&translate- 
  esc=on&imagetype=Jpg'));
  Request.SetRequestUri(Method);

  if not clienthttp.send(Request, Response) then
    Error('Call failed');
  
  response.content.ReadAs(Instr);
  clear(TenantMedia);
  TenantMedia.Content.CreateOutStream(Outstr);
  copystream(outstr,Instr);
end;

Once again all credit should go to Michael Francois Knudsen for providing this code

To update my code example to use the new webservice your code could look something like this

procedure CreateBarcode(ItemNo : Text) : Text 
     var  
        Base64Convert: Codeunit "Base64 Convert";  
        TempBlob: Codeunit "Temp Blob";
        TypeHelper: Codeunit "Type Helper";  
        client: HttpClient;  
        response: HttpResponseMessage;  
        InStr: InStream;  

    begin  
        client.Get('https://barcode.tec-it.com/barcode.ashx?data=' + TypeHelper.UrlEncode(BarCode) + '&code=Code128&translate-esc=on&imagetype=Jpg', response);  
        TempBlob.CreateInStream(InStr);  
        response.Content().ReadAs(InStr);  
        BarCode := Base64Convert.ToBase64(InStr);  
        exit(BarCode); 
    end; 

I recently had a Business Central Cloud customer, who wanted to add barcodes to their reports, now at first you might think that is no big deal 🤷‍♀️, you just use a barcode font, and everyone is happy😊 . But sadly, when it comes to the cloud you cannot just use any font, so what do you do🤔 ? Well, there are some great apps on app source that you can use which can generate barcodes for you, but to be honest I was a little skeptical 😒 to create a dependency in my extension just to get a barcode. So, what other options are there, well you can call a webservice which will generate a barcode for you which you then can use🤓 , now this might sound very complicated but fear not it is not and I will show you just how you can accomplish this with very little code. 

—– barcodes4me not longer provides this service —->

First off, we are going to use the free API provide by http://barcodes4.me they have some fantastic documentation both for barcode and QR codes where you can choose the size of the barcode and the font. For a very simple barcode you could use the following code: 

procedure CreateBarcode(ItemNo : Text) : Text 
     var  
        Base64Convert: Codeunit "Base64 Convert";  
        TempBlob: Codeunit "Temp Blob";  
        client: HttpClient;  
        response: HttpResponseMessage;  
        InStr: InStream;  

    begin  
        client.Get('http://barcodes4.me/barcode/c39/' + ItemNo + '.png', response);  
        TempBlob.CreateInStream(InStr);  
        response.Content().ReadAs(InStr);  
        BarCode := Base64Convert.ToBase64(InStr);  
        exit(BarCode); 
    end; 

<—– barcodes4me not longer provides this service —-

This will leave you with a Base64 string in your barcode variable which you can then use as your image source in you report 

And that was it for this very short post, you can of course extend the function to take font and size, so that it is more generic, but I think you get the idea, until next time stay safe. 😷

Leave a Reply