0

I need to be able to convert any string into a code128 barcode that can be printed on a PDF.

My idea was to embed a code128 font into the PDF and simply use that font to render the string that I need to show as a barcode, letter for letter.

However, I found out that I also need to calculate a checksum and include the start and stop characters.

Is this not possible using PHP? I have not found any solution anywhere. The only solutions that I could find are for directly rendering the barcode as an image, which does not help in my current situation, since I need to use a font to create the barcode in the PDF.

Louis B.
  • 2,348
  • 1
  • 29
  • 51

2 Answers2

4

If you restrict your efforts to 128B, you have access to upper and lower case characters, numbers and most punctuation. It also saves you from having to write code to shift in and out of A and C symbologies. This makes the code to calculate the checksum really trivial.

Code 128B start character has a value of 104. The stop character for all Code 128 variations has a value of 106, but that value does not figure into the chksum calculation. Let's pick the string "Hello" for a real life exercise. You'll want to make sure you have access to the Code 128 table. All the values I will be discussing are out of that table and not ASCII or UTF-8.

The checksum is calculated by adding the multiple of a character’s value by its position in the barcode with the exception of the start code.

While the start code’s position is ‘1’, so is the position of the first character following the start code. So the start code and the first byte of data (‘H’) are both multiplied by the number 1 ((104 × 1) + (40 × 1) = 144).

The following 4 bytes get an incrementally higher multiplier ((69 x 2) + (76 × 3) + (76 × 4) + (79 × 5) = 1065). Summing it all up together (144 + 1065) we get 1209.

You should be able to use the modulus operator (1209 % 103) in PHP to get 76 for the checksum character for the Code 128B string “Hello” (the 103 is a constant, trust me on that). So the final array of codes to map "Hello" into a Code 128 barcode is: [104 40 69 76 76 79 76 106].

You'll need lookup tables to convert the string character values to Code 128B character values to whatever your barcode font is expecting. But all you need is a loop, an array index, an accumulator for the sum of the factors and a modulus operator against the constant value of 103.

Brian Anderson
  • 1,661
  • 1
  • 17
  • 27
3

The easisest way to generate a barcode in PHP for PDF is to use a dedicated software library.

AFAIK, the most complete one is currently the tc-lib-barcode (https://github.com/tecnickcom/tc-lib-barcode) that allows you to generate both linear and bidimensional barcodes. The included example should give you a quick start.

The source code is fully PSR-2 compliant and can be easily added to your PHP projects using Composer.

The original code has been ported and refactored from TCPDF and already used in billions of documents.

Nicola Asuni
  • 1,500
  • 16
  • 9
  • What is your affiliation with the project? – Martijn Pieters Jun 30 '15 at 13:13
  • 1
    @MartijnPieters he's the developer – jeffmcneill Aug 23 '16 at 07:59
  • 1
    This is great but a huge project and overkill for simple checksum calculation. @BrianAnderson's answer below describes what is needed: read in characters, lookup table for their numeric value, incremental multipliers, addition of these plus the start and stop characters, and modulo. Then reverse lookup of character based on numeric value. Then output the start character, text string, checksum character, and stop character. Finally, using a barcode font such as Grand Zebu's GPL font Code128. Grand Zebu's got some VBA code on his site also: http://grandzebu.net/informatique/codbar-en/code128.htm – jeffmcneill Aug 23 '16 at 08:28