Library / SDK
milon/barcode avatar
milon/barcode

milon/barcode for PHP and Laravel Barcode Generation

Barcode Generator for PHP and Laravel

1,480 stars329 forksPHPNOASSERTION

At a glance

What is it?
milon/barcode wraps TCPDF barcode engines for PHP and Laravel, producing SVG, HTML, PNG, JPEG, and file-based output. It suits application developers who need barcode rendering through a small API, but requires ext-gd and careful handling of dimensions, padding, and output format.
Who is it for?
Adopt milon/barcode when a PHP or Laravel application needs direct barcode generation and can accept its ext-gd dependency, output API, and custom license. Do not choose it when you need a scanner, barcode validation service, or a rendering backend not documented here.
Can I use it commercially?
Check first. The repository uses a licence we do not classify automatically, so read its LICENSE file before any commercial use.
Is it still maintained?
Yes. The repository last received commits 20 days ago.
What is it written in?
Mainly PHP, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on October 23, 2026, and from our analysis. They are not legal advice.

Editorial analysis

Why PHP and Laravel developers use DNS1D and DNS2D

milon/barcode addresses a narrow but common application need: generating a barcode inside a PHP or Laravel application without assembling image output from TCPDF components in application code. The package exposes separate APIs for linear barcodes and 2D formats. DNS1D handles 1D symbologies such as CODE 39, CODE 128, EAN, and UPC. DNS2D handles QR, Data Matrix, and PDF417 according to the package documentation.

The target users are PHP developers and Laravel developers who need barcodes in HTML pages, Blade templates, labels, product pages, or other application views. The package can return markup for immediate display, base64 image data for embedding in HTML, or files written under Laravel's configured storage path. That range is useful because screen output and print output have different practical requirements.

The library does not scan barcodes, decode barcodes, validate submitted identifiers, or act as a remote barcode service. The documented API is a renderer. A developer still needs to choose the correct barcode type and input data. For a retail product label, for example, the package's EAN and UPC layout support matters. For a QR code placed on a screen, module dimensions, background color, and quiet-zone padding are the relevant choices.

The API is deliberately small in the README examples. That is a strength for straightforward generation, but it also means the package leaves format selection and production rendering decisions to the caller. The documentation gives examples rather than a separate high-level workflow for batches, caching, or error handling. Those concerns would need to be handled by the surrounding application.

The TCPDF-backed rendering path inside milon/barcode

The repository description says that milon/barcode wraps TCPDF barcode engines behind DNS1D and DNS2D. The package therefore provides a PHP-facing API while the underlying barcode generation machinery comes from TCPDF. The visible output methods are strings or files, which means the library is suitable for synchronous rendering inside a request, although the README does not document queueing or batch-generation behavior.

DNS1D and DNS2D use different dimension semantics. For DNS1D, `$w` is the width of a single bar in pixels, while `$h` is the total barcode height. A typical starting point for a 1D label is `$w = 2` or `$3` and `$h = 50` or `$80`. For DNS2D, `$w` and `$h` are the width and height of a module in pixels. A QR code intended for a screen commonly starts around `$w = $h = 4` to `$8`, while print output commonly starts around `$8` to `$12`.

The output methods make the data flow straightforward. `getBarcodeSVG(...)` returns SVG markup. `getBarcodeHTML(...)` returns HTML markup. `getBarcodePNG(...)` returns raw base64-encoded PNG data, not a complete data URI. `getBarcodeJPG(...)` returns base64 JPEG data for 1D output. The path helpers write PNG files, and `getBarcodeJPGPath(...)` writes JPEG files, with the README documenting path support for 1D JPEG output.

The package also exposes settings that affect the generated result. Colors can be represented as RGB arrays for raster output, while SVG and HTML use CSS color strings. A null background defaults to transparency. Padding can be added around the barcode, and SVG output uses `shape-rendering="crispEdges"` to reduce print-time anti-alias blur. These are rendering controls rather than higher-level document or label features.

Installing milon/barcode and rendering the first Laravel example

The package is installed with Composer. Laravel 6 and later automatically discover the service provider and the `DNS1D` and `DNS2D` facades. The README also lists package constraints for Laravel versions from 4.x through 13.*, so the application must select the package release line compatible with its Laravel version.

Install the dependency in a PHP project with:

shell
composer require milon/barcode

For Laravel, publishing configuration is optional. If the application needs the package's store path to use a custom location, run the documented provider-specific publish command:

shell
php artisan vendor:publish --provider="Milon\Barcode\BarcodeServiceProvider"

The default store path is the Laravel `storage` directory. The path helpers write files there, so the directory must be writable. This matters when a container, web server, or deployment platform runs PHP with filesystem permissions that differ from the process used during installation.

A first Blade example can render HTML directly. The README specifically instructs users to use `{!! !!}` because the helper returns markup that should not be escaped:

blade
{!! DNS1D::getBarcodeHTML('5901234123457', 'EAN13') !!}

The result is HTML for an EAN-13 barcode containing the supplied code. For PNG output in an image element, the raw base64 value must be placed inside a data URI:

blade
<img
  src="data:image/png;base64,{{ DNS2D::getBarcodePNG($url, 'QRCODE', 6, 6, [0,0,0], [255,255,255]) }}"
  alt="QR code"
>

The README warns that PNG helpers return raw base64, so omitting the `data:image/png;base64,` prefix would not produce the documented image source. The package requires the PHP GD extension. Installation therefore needs to include `ext-gd` in the runtime environment.

File output, quiet zones, and logo overlays for printed codes

milon/barcode provides a separate file-oriented path for applications that need stored images rather than inline markup. The PNG path helper writes under the configured store path and returns a relative path. Its documented signature accepts the code, barcode type, dimensions, colors, and an optional custom filename.

For example, the following writes a PNG with a custom name. According to the README, the extension is optional and path segments are stripped from the supplied filename.

php
DNS2D::getBarcodePNGPath($url, 'QRCODE', 6, 6, [0, 0, 0], [255, 255, 255], 'product-42');

The documented destination is `storage/.../product-42.png` in the default Laravel configuration. This can be convenient for labels or generated assets, but the caller must ensure that the returned path is served or processed correctly and that generated files do not accumulate without a retention policy. The README does not document cache invalidation, cleanup, or file locking.

A quieter design choice is the quiet-zone setting. Barcode readers can fail when bars sit against a page edge or other graphics. The README suggests about ten times the bar width as a common 1D rule of thumb and shows a padding of `20` pixels when `$w = 2`:

php
DNS1D::setPadding(20);
echo DNS1D::getBarcodeSVG($code, 'C128', 2, 80);

DNS1D::setPadding(0);

Padding is persistent on instances and facades until changed, so resetting it matters when the same process later renders a different label. For QR codes, the documentation uses four modules of padding as an example, calculated as `4 * 6` when module width is six pixels.

PNG QR and Data Matrix output can also receive a centered logo through GD. The README recommends high error correction and a logo occupying roughly 15 to 25 percent of the QR width:

php
DNS2D::setLogo(storage_path('app/logo.png'), 0.2);
echo DNS2D::getBarcodePNG($url, 'QRCODE,H', 6, 6, [0, 0, 0], [255, 255, 255]);
DNS2D::setLogo(null);

Those settings improve the presentation path, but they also introduce a production constraint. The package does not guarantee scanner readability for every logo, input value, dimension, or scanner. Logo overlay should therefore be checked with the intended scanning environment.

ext-gd, format choices, and the limits of the documented workflow

The clearest limitation is the explicit dependency on the PHP GD extension. The package requires `ext-gd`, and the README says that logo overlays on PNG output use GD. An application that runs in a minimal PHP image without GD cannot use the documented installation without changing its runtime.

Another limitation is the gap between generating a barcode and proving that a particular scanner will read it. The README supplies recommendations for dimensions, padding, backgrounds, and QR logo size, but it does not document automated scanner testing, checksum validation, or a fallback workflow. The developer must select the symbology and ensure that the encoded value matches the receiving system's rules. A barcode that renders successfully can still be unsuitable if it lacks the required quiet zone, has inappropriate contrast, or uses the wrong format.

The API also has format-specific differences. `getBarcodeJPG(...)` is documented for 1D output, and `getBarcodeJPGPath(...)` is likewise listed for 1D. SVG and HTML are returned as markup, while PNG and JPEG helpers return base64 data. Transparent output is convenient for dark or mixed backgrounds, but the README explicitly recommends a white or light background for dark themes and email clients. That choice affects file size, visual contrast, and rendering behavior in the target channel.

Finally, the README does not describe rollback, batch generation, asynchronous rendering, scanner integration, or production caching. The absence is significant for an adoption decision, although it does not mean the features are absent from the package. It means the available documentation does not establish them as supported workflows. Applications requiring those behaviors need to verify the implementation and compatibility before depending on them.

milon/barcode compared with direct TCPDF integration

The documented alternative is to work with TCPDF directly rather than adopt the milon/barcode wrapper. TCPDF supplies the underlying barcode engines identified by the repository description. Direct integration instead requires application code to work with TCPDF itself.

milon/barcode reduces that setup to methods such as `DNS1D::getBarcodePNG(...)` and `DNS2D::getBarcodeSVG(...)`. Its README provides PHP, Blade, Laravel compatibility, output-format, color, padding, and logo examples. That makes it the more direct choice when the requirement is simply to render a barcode inside a PHP or Laravel view.

milon/barcode is narrower in scope. Its advantage is the Laravel-oriented façade and a smaller barcode-specific interface. Its limitation is that adopting it still relies on the wrapped engines and its PHP rendering dependencies, without adding scanning or barcode-data management.

Maintenance history is relevant to the choice. The repository is not archived, and the last push was on 2026-10-03. The listed releases include `v13.5.1` on 2026-09-25, `v13.5` on 2026-09-12, and `v13.1` on 2026-02-24. The repository contains a custom `LICENSE.TXT`, which is marked as a license that GitHub cannot classify. The README does not summarize its permissions or restrictions, so license review should start with that file.

Editorial conclusion

Adopt milon/barcode when a PHP or Laravel application needs direct barcode generation and can accept its ext-gd dependency, output API, and custom license. Do not choose it when you need a scanner, barcode validation service, or a rendering backend not documented here. Verify first that your supported Laravel version, PHP environment, storage permissions, and required barcode output format match the package configuration.

Frequently asked questions

How do I install milon/barcode for PHP or Laravel?

Run `composer require milon/barcode`. Laravel 6 and later automatically discover the service provider and facades, and the PHP runtime must include the required GD extension.

What output formats does milon/barcode support?

The documented methods return SVG markup, HTML markup, base64-encoded PNG, or base64-encoded JPEG. PNG path helpers write files under the configured store path, which defaults to Laravel's `storage` directory.

How do I add padding or a logo to a milon/barcode image?

Use `setPadding()` to create a quiet zone around the code. For QR or Data Matrix PNG output, `setLogo()` accepts a logo path and a size ratio, with the README recommending high error correction and a modest logo area.

Official sources

  1. Issues
  2. milon/barcode on GitHub
  3. Project website
  4. README
  5. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/milon-barcode.svg)](https://hysenlabs.com/projects/milon-barcode)

Was this useful?