Create Barcodes in PHP

A quick drop-in solution to create barcodes on the server with PHP is Picqers PHP Barcode Generator. It's a free and easy way to create most linear barcode symbologies. Simply install via Composer like so:

composer require picqer/php-barcode-generator

The library will output codes as raster (PNG etc.) or vector (SVG only, no PDF) which you can then write to file or serve with a web page.

However, this PHP barcode generator won't solve all barcode problems:

A more comprehensive alternative is the McAPI Barcode API which addresses most shortcomings: 2D barcodes, more important linear barcode symbologies and widely configurable human readable text for the codes plus additional text (for pricing, article names etc.) next to the barcode. Another plus is the support for generating scalable PDFs for best output quality.

The following PHP sample code demonstrates how to create barcodes with the McAPI Barcode REST API service. The sample uses PHP 7 but the code will also work with older versions. The sample uses the modules curl and json which should be available with a standard PHP installation (check with phpinfo()).

Requirements: A RapidAPI account. Replace YOUR_API_KEY in the code snippets below with your own api key.

The sample works with the free tier of the API, see RapidAPI McAPI Barcode Listing for available plans.

See the overview page for a reference that lists all available parameters and error codes.

Create a Barcode using PHP

Shown is the creation of a Code 128 (code type: 7) with PHP using the cURL-module. All relevant parameters are in the POSTFIELDS of the request. The barcode will encode the text "123456". The module width is set to 0.5mm, the height is set to 20mm, the size of the human readable text under the barcode is set to 10 points. The code will be created and returned as a base64 encoded PDF.

The source code:

// Listing: Create barcodes in PHP
  
$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "https://mcapi-barcode.p.rapidapi.com/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{
    \"data\": \"123456\",
    \"type\": 7,
    \"moduleWidth\": 0.5,
    \"moduleHeight\": 20,
    \"hrSize\": 10,
    \"format\": \"pdf\",     
}",
  CURLOPT_HTTPHEADER => [
    "content-type: application/json",
    "x-rapidapi-host: mcapi-barcode.p.rapidapi.com",
    "x-rapidapi-key: YOUR_API_KEY"
  ],
]);

$response = curl_exec($curl);
curl_close($curl);

The data will be delivered as JSON in $response e.g.:

{
  "service":"McAPI Barcode Generator, https://mcapi.io",
  "version":"V1",
  "format":"pdf",
  "code":"data:application/pdf;base64,JVBE ... Cg=="
}

McAPI Barcode API - Sample barcode with PHP

The returned code from the PHP request:

Image of Sample Barcode PHP

This is just an introductory example. We provide more PHP sample code for barcode creation here.