tecnickcom/tc-lib-barcode

GS1 128

ghola opened this issue · 9 comments

ghola commented

I'm having a hard time figuring out what character i should be using for FNC1 (i'm trying to generate GS1 128 barcodes). Btw, is it even possible to generate such barcodes with his library?

ghola commented

I figured out it's 241 and it does work as long as i have it at the beginning of the barcode number (attempting to work with barcode type C here), but as soon as i move it somewhere in the middle of the barcode it complains about the length not being even. My use case requires me to use multiple FCN1 characters.

Maybe if i produce a barcode for each piece starting with FCN1 and then concatenate them? But i'm not sure that's even possible.

Can you provide some examples or test cases?

ghola commented

Sure, here's an attempt to produce a USPS barcode which fails with the above mentioned error.

$trackingCode = '9405510200864168997758';
$fiveDigitZip = '29651';
$FNC1 = chr(241);
$code = "{$FNC1}420{$fiveDigitZip}{$FNC1}{$trackingCode}";

$barcode = new C($code, -1, -40, '#000000');
$svgImage = $barcode->getSvgCode();

However changing the code from $code = "{$FNC1}420{$fiveDigitZip}{$FNC1}{$trackingCode}"; to $code = "{$FNC1}420{$fiveDigitZip}{$trackingCode}"; works.

The spec for the USPS barcode is from the Endicia documentation: http://www.endicia.com/tools-resources/harrys-hints/new-usps-tracking-barcodes . Using this generator from USPS http://generator.onbarcode.com/online-gs1-128-barcode-generator.aspx with the following code (420)29651(94)05510200864168997758 produces the correct barcode (the parans are used to delimit the AIs, but it's just a way to intrododuce the FNC1 as it goes right before the opening parans)

ghola commented

I managed to do it by hacking the code in the getCodeDataC method. It is producing the correct barcode, identical to the USPS one. Here's what i ended up with:

class TcLibGS1 extends C
{
    /**
     * Get the C code point array
     *
     * @param array  $code_data Array of codepoints to alter
     * @param string $code      Code to process
     * @param int    $len       Number of characters to process
     *
     * @retun array
     *
     * @throws BarcodeException in case of error
     */
    protected function getCodeDataC(&$code_data, $code, $len)
    {
        $codeFragments = explode(chr(241), $code);

        foreach ($codeFragments as $fragment) {
            if ((strlen($fragment) % 2) != 0) {
                throw new BarcodeException(
                    'Fragment length must be even. A fragment is the code between two FNC1 characters.'
                );
            }
        }

        $codeFragmentCount = count($codeFragments);

        for ($i = 0; $i < $codeFragmentCount; $i++) {
            $fragment = $codeFragments[$i];
            $len = strlen($fragment);

            for ($pos = 0; $pos < $len; $pos += 2) {
                $chrnum = $fragment[$pos] . $fragment[($pos + 1)];
                if (preg_match('/([0-9]{2})/', $chrnum) > 0) {
                    $code_data[] = intval($chrnum);
                } else {
                    throw new BarcodeException('Invalid character sequence');
                }
            }

            if ($codeFragmentCount != 1 && $i != $codeFragmentCount - 1) {
                $code_data[] = 102;
            }
        }
    }
}

The idea was to split the code by FNC1 and during the build of the $code_data, reinsert '102' where the FNC1 used to be (i followed this approach from the C::getCodeData method where i noticed how the first FNC1 is handled).

Thanks for reporting this issue.
I have just pushed a new version that should fix the problem.
Please let me know if this is working as expected.

ghola commented

Thanks for the quick responses. Yes, it works as expected. Will you be releasing a tagged version for it?

Yes, done:
1.15.2

ghola commented

Thank you!

Hi I am facing the following error, I am using the latest version of the library, but I don't know sometimes its working and sometimes its through the attached error. Please help me out and your help will be really apricated
screencapture-localhost-betting-bets-save-2021-01-08-15_11_11