Search code examples
phpbarcodecode128

Creating a code128 barcode in PHP, using a font instead of rendering it as an image


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.


Solution

  • 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.