This post is a reference for printing PDF417 2-dimensional codes to a receipt printer, using the escpos-php.
I’ve got an older post about printing QR codes with escpos-php, which follows the same format and has some more background and links if you haven’t printed receipts from PHP before.
Straight from the documentation, the syntax for the command that I’m demonstrating is:
pdf417Code($content, $width, $heightMultiplier, $dataColumnCount, $ec, $options)
Print a two-dimensional data code using the PDF417 standard.
- string $content: Text or numbers to store in the code
- number $width: Width of a module (pixel) in the printed code. Default is 3 dots.
- number $heightMultiplier: Multiplier for height of a module. Default is 3 times the width.
- number $dataColumnCount: Number of data columns to use. 0 (default) is to auto-calculate. Smaller numbers will result in a narrower code, making larger pixel sizes possible. Larger numbers require smaller pixel sizes.
- real $ec: Error correction ratio, from 0.01 to 4.00. Default is 0.10 (10%).
- number $options: Standard code Printer::PDF417_STANDARD with start/end bars, or truncated code Printer::PDF417_TRUNCATED with start bars only.
These PDF417 snippets above appear in the examples of escpos-php.
Simple example
A basic code that just says ‘testing 123’, and a demonstration of a narrower code that has been aligned:
// Most simple example
title($printer, "PDF417 code demo\n");
$testStr = "Testing 123";
$printer -> pdf417Code($testStr);
$printer -> text("Most simple example\n");
$printer -> feed();
// Demo that alignment is the same as text
$printer -> setJustification(Printer::JUSTIFY_CENTER);
$printer -> pdf417Code($testStr, 3, 3, 2);
$printer -> text("Same content, narrow and centred\n");
$printer -> setJustification();
$printer -> feed();
Error correction
This implementation accepts an error correction ratio as a percentage. The minimum is 1%, the highest is 400%, expressed as a decimal (0.01 – 4.00).
Higher error correction settings create lager codes that are more resilient to scanning errors due to damage.
Pixel size (width)
The same example string, with some different module widths. Note that the blocks in the code scale in bot directions when the width is changed.
Larger print is easier for a scanner to read, but takes up more paper.
// Change size
title($printer, "Pixel size\n");
$testStr = "Testing 123";
$sizes = array(
2 => "(minimum)",
3 => "(default)",
4 => "",
8 => "(maximum)");
foreach ($sizes as $size => $label) {
$printer -> pdf417Code($testStr, $size);
$printer -> text("Module width $size dots $label\n");
$printer -> feed();
}
Height multiplier
The height of the modules in the code can also be changed, stretching it do a different degree. PDF417 that are too vertically squashy are more prone to scanning errors.
// Change height
title($printer, "Height multiplier\n");
$testStr = "Testing 123";
$sizes = array(
2 => "(minimum)",
3 => "(default)",
4 => "",
8 => "(maximum)");
foreach ($sizes as $size => $label) {
$printer -> pdf417Code($testStr, 3, $size);
$printer -> text("Height multiplier $size $label\n");
$printer -> feed();
}
Data column count
The of data columns to print in the code can be customised to produce a narrower code. But beware, if you request a code that’s too big for the paper, nothing will be printed!
// Change data column count
title($printer, "Data column count\n");
$testStr = "Testing 123";
$columnCounts = array(
0 => "(auto, default)",
1 => "",
2 => "",
3 => "",
4 => "",
5 => "",
30 => "(maximum, doesnt fit!)");
foreach ($columnCounts as $columnCount => $label) {
$printer -> pdf417Code($testStr, 3, 3, $columnCount);
$printer -> text("Column count $columnCount $label\n");
$printer -> feed();
}
Truncated code option
Use this setting to select the alternative, ‘truncated’ code format.
// Change options
title($printer, "Options\n");
$testStr = "Testing 123";
$models = array(
Printer::PDF417_STANDARD => "Standard",
Printer::PDF417_TRUNCATED => "Truncated");
foreach ($models as $model => $name) {
$printer -> pdf417Code($testStr, 3, 3, 0, 0.10, $model);
$printer -> text("$name\n");
$printer -> feed();
}
Notes
To run the snippets, you need to initialise the printer, and define a title() function to print headings, like so:
<?php
/* Demonstration of available options on the qrCode() command */
require __DIR__ . '/autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
$connector = new FilePrintConnector("/dev/usb/lp0");
$printer = new Printer($connector);
// ....
// Cut & close
$printer -> cut();
$printer -> close();
function title(Escpos $printer, $str) {
$printer -> selectPrintMode(Printer::MODE_DOUBLE_HEIGHT | Printer::MODE_DOUBLE_WIDTH);
$printer -> text($str);
$printer -> selectPrintMode();
}
In the QR code post, I posted a fallback which used software rendering. As I don’t have a PHP-based PDF417 code library, you will need a printer which supports them to be blue to use these examples.
Good luck!
Hi!
Thank you for the library, works like a charm :)
But… i’m having an issue with the pdf417 codes. I can’t read every single code that i generate. Could be something with my printer?
Regards!!
Maybe! Please post on the issue tracker, including a code snippet that shows what you’re printing, and a picture to show how it renders.
https://github.com/mike42/escpos-php/issues
The above output was generated on an Epson TM-T20, and verified with zxing for Android.
https://github.com/zxing/zxing
Hello I have a webpage in a hosting and a pc with an usb printer Zebra GK420t, how can I do to print a label.
I installed a XAMPP in the pc and locally the label print perfect, But I don’t know how I can do through a hosting.