https://github.com/benfaerber/pdf-to-zpl
Convert PDF files and images into ZPL for Zebra Label Generation
https://github.com/benfaerber/pdf-to-zpl
pdf-converter php zebra-printer zpl zpl-code
Last synced: 3 months ago
JSON representation
Convert PDF files and images into ZPL for Zebra Label Generation
- Host: GitHub
- URL: https://github.com/benfaerber/pdf-to-zpl
- Owner: benfaerber
- Created: 2025-01-06T20:46:56.000Z (4 months ago)
- Default Branch: master
- Last Pushed: 2025-02-21T19:59:40.000Z (3 months ago)
- Last Synced: 2025-02-21T20:30:57.288Z (3 months ago)
- Topics: pdf-converter, php, zebra-printer, zpl, zpl-code
- Language: PHP
- Homepage:
- Size: 3.02 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PDF to ZPL [](https://packagist.org/packages/faerber/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
convertFromFile("myFile.pdf");
// Or from a blob:
$pages = $converter->convertFromBlob(file_get_contents("myFile.pdf"));foreach ($pages as $index => $page) {
// Each page is a single ZPL statement
assert(str_starts_with($page, "^XA^GFA,"));
}
```## Environment Setup:
Ensure you have Imagick and GD installed using:
```
sudo apt install php8.4-gdsudo apt install php8.4-imagick
```
Then make sure to enable them in `php.ini`.This library can work with only Imagick but GD is recommended because it's a lot faster (see [benchmarks](./.phpbench/html/index.html) for more details)!
If you would like to only use Imagick use these settings:
```php```
Change to:
``````
I've only tested this library on Linux and Mac so if you get it working on windows feel free to open a PR!
## Converting Images:
```php
convertFromFile("myFile.png");
assert(str_starts_with($zpl, "^XA^GFA,"));
```## Previewing Labels
The [`labelary`](https://labelary.com/) 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
saveAs("my_label.png");
```## Unit Testing
Testing is done via PHP Unit. Run `composer test`.If you make major changes you may need to regenerate the test data.
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
Benchmarking is done via `phpbench`. Run `composer benchmark`.## Formatting
Formatting is done via `php-cs-fixer`. Run `composer format`.## How does this work?
1. Loads the PDF and render each page as image
1. Scale the image to match the DPI and aspect ratio of the label
1. Convert each page into a grayscaled bitmap
1. Run line encode the bitmap and marshall it into a ZPL binary representation
1. Wrap the encoded data into a ZPL payload