Generating and reading barcode

Generating and reading barcode

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Generating and Reading Barcodes in a Web-Only Environment

Building an e-shopping infrastructure for multinational offices introduces specific logistical challenges, particularly when physical inventory needs to be tracked digitally across borders. The core requirement here is straightforward: generate a barcode before shipping and validate the receipt of goods upon arrival using only a web browser and a USB-connected barcode scanner. This setup eliminates the need for installing local applications on every worker's machine, which simplifies maintenance and deployment significantly.

Generating Barcodes via PHP

The first step is generating the barcode image itself. You do not need a complex backend service; standard PHP libraries are sufficient. The most robust and widely adopted solution is compozr/barcode or the legacy but still functional php-barcode-detected. For modern Laravel applications, packages like intervention/image combined with specific barcode generators offer flexibility, but sticking to dedicated barcode libraries ensures better compliance with ISO standards (like Code 128 or QR).

Here is a practical approach using the compozr/barcode package within a PHP context:

composer require compozr/barcode

In your controller or service layer, you would generate the image as a binary stream rather than saving it to disk immediately. This keeps your server storage clean and allows you to serve the image directly via HTTP.

use Compозr\Barcode;

// Generate a unique tracking ID (e.g., UUID or Snowflake ID)
$trackingId = (string) uuid(); 

$barcode = new Barcode();
$barcode->setCode(Barcode::CODE_128); // Code 128 is dense and supports alphanumeric data
$barcode->setText($trackingId);
$barcode->setHeight(50);
$barcode->setSizeRatio(0.3);

// Generate output as image/png
$output = $barcode->getPngString();

// Send the PNG response directly to the frontend
return response($output, 200)
    ->header('Content-Type', 'image/png')
    ->header('Cache-Control', 'public, max-age=3600'); // Cache aggressively as barcodes are static

This approach ensures that when your system sends the "package ready" notification to a country office, the barcode image is embedded in the email or accessible via a secure URL. The worker simply needs to download it and print it on a label.

Reading Barcodes with a USB Scanner

The second phase involves inputting data from the physical package into your web application without installing drivers or software. This is where the concept of a "keyboard wedge" scanner becomes critical. Most industrial barcode scanners (USB or RS-232) do not act as traditional mice; they emulate a keyboard. When a worker scans a barcode, the device sends the raw text string followed by an Enter key press.

To make this work with your web interface, you need to handle two specific scenarios:

  1. Auto-Focus Input: The scanner will paste data into whatever field currently has focus.
  2. Submission Logic: You must ensure that scanning a barcode triggers the "Approve" action automatically.

In your frontend (JavaScript), listen for the keydown event or rely on standard form submission triggered by the Enter key. If using a specific scanner protocol like HID, the device will send the characters exactly as if typed.

document.getElementById('receive-form').addEventListener('submit', async function(e) {
    e.preventDefault(); // Prevent default post only if you want custom logic
    
    const trackingId = document.querySelector('[data-scan-target]').value;
    
    try {
        await approveReceipt(trackingId);
        
        // Show success message immediately after approval
        alert('Item received successfully!');
        document.getElementById('receive-form').reset();
    } catch (error) {
        console.error('Approval failed:', error);
    }
});

Critical Implementation Details

When implementing this workflow, several pitfalls must be avoided. First, ensure your barcode generation logic is deterministic. If the same item generates different barcodes upon regeneration, your approval system will fail because it expects a specific ID matching the one on the label. Store the trackingId in your database immediately after generation to link the physical item to its digital record.

Second, consider the scanner's buffer size. Some scanners output multiple lines if the input is long or if they are configured to send line breaks. Ensure your HTML form handles the specific string sent by the scanner correctly. If a scanner sends ITEM-123<Enter>, your form field must capture that exact value.

Finally, handle network timeouts. Since country offices might have unstable internet connections, consider implementing an exponential backoff retry mechanism if the "approve" request fails during submission. However, be careful not to let the scanner hang indefinitely; some scanners require a specific timeout before they send new data.

Conclusion

By leveraging PHP barcode libraries and standard USB keyboard-wedge behavior, you can build a seamless, application-free workflow for global inventory management. The key is treating the barcode scanner as an input device that triggers form submissions, allowing your existing web stack to handle all the logic without requiring local installs on worker devices.

Our network