faerber/pdf-to-zpl
Composer 安装命令:
composer require faerber/pdf-to-zpl
包简介
Convert a PDF file to a ZPL file
关键字:
README 文档
README
PDF to ZPL
Convert a PDF into the ZPL format. Allowing for custom images, alphabets like Hebrew, Arabic and Cyrillic (without messing with fonts on the printer) and multipage shipping labels!
Gettings Started:
composer require faerber/pdf-to-zpl
<?php use Faerber\PdfToZpl\PdfToZplConverter; use Faerber\PdfToZpl\Settings\ConverterSettings; use Faerber\PdfToZpl\Settings\ImageScale; // By default this will convert a 4x6 label $converter = new PdfToZplConverter(); // Get an array of ZPL commands (1 per page) $pages = $converter->convertFromFile("myFile.pdf"); // Or from a blob: $pages = $converter->convertFromBlob(file_get_contents("myFile.pdf")); foreach ($pages as $page) { // Each page is a single ZPL statement assert(str_starts_with($page, "^XA^GFA,")); }
You will need admin rights to setup Imagick PDF reading (essential for this library).
Linux Environment Setup:
The minimum version for this package is 8.1.
Ensure you have Imagick and GD installed using:
sudo apt install php8.3-gd
sudo apt install php8.3-imagick
(Or whatever PHP Version you are using)
Then make sure to enable them in php.ini (usually enabled by default).
Imagick Settings
You may need to enable PDF permission in your Imagick settings.
- Find your Imagick Policy File:
ls /etc/ | grep ImageMagick - Edit your Imagick Policy File:
sudo nano "/etc/ImageMagick6/policy.xml"
Find this line and ensure the rights are set to read | write:
<policy domain="coder" rights="none" pattern="PDF" />
Change to:
<policy domain="coder" rights="read | write" pattern="PDF" />
If this line doesn't exist at all, add it. You'll only run into this with tiny linux boxes like Github Actions.
Imagick has had PDF related security issues. Convert only trusted PDFs. Here's one example: CVE-2020-29599
Windows Environment Setup:
Install GhostScript with choco and Imagick and GD extensions.
choco install ghostscript
Converting Images:
<?php use Faerber\PdfToZpl\ImageToZplConverter; $converter = new ImageToZplConverter(); // Get an array of ZPL commands (1 per page) [$zpl] = $converter->convertFromFile("myFile.png"); assert(str_starts_with($zpl, "^XA^GFA,"));
Previewing Labels
The labelary API is used to generate images from ZPL allowing the label to be previewed.
This is a free API that requires no auth so it can be used with no setup. Be sure to respect their rate limits!
<?php use Faerber\PdfToZpl\LabelImage; use Faerber\PdfToZpl\Settings\LabelDirection; $zpl = "^XA_ZPL_DATA_HERE..."; $labelImage = new LabelImage( zpl: $zpl, direction: LabelDirection::Up, ); $labelImage->saveAs("my_label.png");
Settings
There are many settings you can use to configure the conversion. You can use Imagick instead of GD, rotate and resize labels etc.
See Settings for more details.
Unit Testing
Run composer test. Testing is done via PHP Unit.
If you make major changes you may need to regenerate the test data (tests pass if each file is at least 95% similar to the test data generated with PHP 8.3 on my linux box).
For example modifying scaling code where the output is correct but the test data is outdated.
Run composer generate-test-data and manually verify the images are rendered correctly.
Benchmarking
Run composer benchmark. Benchmarking is done via phpbench.
Here's some basic performance information:
- Converting a PNG to ZPL:
668.26ms - Convert a 3 page PDF label to ZPL with GD Backend:
1.8s - Convert a 3 page PDF label to ZPL with Imagick Backend:
5.4s
This was run on my workstation. (AMD Ryzen 9 7950x 16-core, 32Gib)
See phpbench output for more details.
Formatting
Run composer format. Formatting is done via php-cs-fixer.
How does this work?
- Loads the PDF and render each page as image
- Scale the image to match the DPI and aspect ratio of the label
- Convert each page into a grayscaled bitmap
- Run line encode the bitmap and marshall it into a ZPL binary representation
- Wrap the encoded data into a ZPL payload
faerber/pdf-to-zpl 适用场景与选型建议
faerber/pdf-to-zpl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22.26k 次下载、GitHub Stars 达 34, 最近一次更新时间为 2025 年 01 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「pdf」 「zpl」 「zebra-printers」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 faerber/pdf-to-zpl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 faerber/pdf-to-zpl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 faerber/pdf-to-zpl 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP ZPL builder, image conversion and a basic client for network-connected Zebra label printers.
This library is a php wrapper for the ZPL Programming Language.
A simple PHP library for building ZPL (Zebra Programming Language) label payloads.
Provides TCPDF integration for Symfony
Laravel QZ Tray is a complete silent printing solution that connects your Laravel application to desktop printers via QZ Tray. It allows you to print directly from the browser without print dialogs, with smart caching, printer memory, and automatic fallback.
A adapter for the mPDF print library
统计信息
- 总下载量: 22.26k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 35
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-06
