-
Notifications
You must be signed in to change notification settings - Fork 101
2.0 Raw Printing
###Compatibility
- ✅ 2.0 | ⛔ 1.9 | ...
###Contents
The following code can be used for raw printing only. If you are unsure what raw printing is, please refer to What is Raw Printing?
- Advanced Print Spooling
- Special Characters
- Combining Techniques
- Custom Job Name
- Unix/Linux Alternate Printing
- Chaining Print Requests
Boilerplate code to send raw commands to any printer. See also ZPL, EPL, ESC/P for specific examples. If your raw language is missing, please contact support.
function printStuff() {
var config = qz.configs.create("Printer Name");
var data = [
'Raw Data\n',
'More Raw Data\n',
'Even More Raw Data\n'
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
Note: The raw commands provided may not work with your printer. Please reference your printer's programming guide for more information.
- Albeit obvious, each example needs a trigger, such as a button, .e.g.
<input type="button" onclick="printStuff()" value="Print"></input>
Example for sending raw ZPL commands to a printer
Note:
language: "foo"
is only required if providing a raw image type in the data stream.
function printZPL() {
var config = qz.configs.create("Printer Name");
var data = [
'^XA\n',
'^FO50,50^ADN,36,20^FDPRINTED USING QZ PRINT PLUGIN\n',
{
type: 'raw', format: 'image', data: 'assets/img/image_sample_bw.png',
options: { language: "ZPL" }
},
'^FS\n',
'^XZ\n'
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
Example for sending EPL commands to a printer.
Note:
language: "foo"
is only required if providing a raw image type in the data stream.
function printEPL() {
var config = qz.configs.create("Printer Name");
var data = [
'\nN\n',
{ type: 'raw', format: 'image', data: 'assets/img/image_sample_bw.png', options: { language: "EPL", x: 20, y: 0 } },
'\nP1,1\n'
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
function printESCP() {
var config = qz.configs.create("Printer Name");
var data = [
{ type: 'raw', format: 'image', data: 'assets/img/image_sample_bw.png', options: { language: "escp", dotDensity: 'double' } },
'\nPrinted using qz-print plugin.\n\n\n\n\n\n'
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
With this function, you can send base64 encoded characters/raw commands to qz using "base64". This will automatically convert provided base64 encoded text into text/ascii/bytes, etc.
function printBase64() {
var config = qz.configs.create("Printer Name");
var data = [
{
type: 'raw',
format: 'base64',
data: 'Ck4KcTYwOQpRMjAzLDI2CkI1LDI2LDAsMUEsMyw3LDE1MixCLCIxMjM0IgpBMzEwLDI2LDAsMywx' +
'LDEsTiwiU0tVIDAwMDAwIE1GRyAwMDAwIgpBMzEwLDU2LDAsMywxLDEsTiwiUVogUFJJTlQgQVBQ' +
'TEVUIgpBMzEwLDg2LDAsMywxLDEsTiwiVEVTVCBQUklOVCBTVUNDRVNTRlVMIgpBMzEwLDExNiww'
}
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
This feature allows a raw file to be spooled directly to the printer.
function printFile() {
var config = qz.configs.create("Printer Name");
var data = [
{
type: 'raw',
format: 'file',
data: 'assets/raw-data.txt'
}
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
The ability to read the contents of an XML file containing Base64 encoded commands and send these commands to the printer.
function printXML() {
var config = qz.configs.create("Printer Name");
var data = [
{
type: 'raw',
format: 'xml',
data: 'assets/zpl_sample.xml',
options: { xmlTag: 'v7:Image' }
}
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
function printToFile() {
var config = qz.configs.create({ file: "/path/to/output.txt" }); // or "C:\\path\\to\\output.text", etc
var data = [
'Raw Data\n',
'More Raw Data\n',
'Even More Raw Data\n'
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
function printToUNC() {
var config = qz.configs.create({ file: "\\\\server\\printer" });
var data = [
'Raw Data\n',
'More Raw Data\n',
'Even More Raw Data\n'
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
Leverage printing API for writing to a Win32 Device Namespace physical device such as a Bematech/Logic Controls Pole Display mapped as \\.\LCLD9
function printToDevice() {
var config = qz.configs.create({ file: "\\\\.\\LCLD9\\dummy" });
var data = [
'"\r\nMMM... COFFEE! :)\r\n"'
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
Spools jobs between specified endOfDoc
delimiter. Useful for avoiding buffer overflows on unusually large quantity print jobs by spooling each raw job individually. Defaults to 1
job per spool, but can be overridden using perSpool
option.
function advancedSpooling() {
var config = qz.configs.create("Printer Name", { endOfDoc : '\n' });
var data = [
'First\n',
'Second\n',
'Third\n'
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
function printHex() {
var config = qz.configs.create("Printer Name");
var data = [
{ type: 'raw', format: 'hex', data: 'x1Bx00' } // or data: '1B00'
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
function printDecimal() {
var config = qz.configs.create("Printer Name");
var data = [
String.fromCharCode(27) + String.fromCharCode(0) // e.g. CHR(27) CHR(0) or Hex x1Bx00
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
function printCombined() {
var config = qz.configs.create("Printer Name", { language: "escp" });
// Example only, not valid ESCP
var data = [
"My Raw Commands",
String.fromCharCode(27) + String.fromCharCode(0),
{ type: 'raw', format: 'hex', data: 'x1Bx00' },
{ type: 'raw', format: 'image', data: 'assets/img/image_sample_bw.png', options: { dotDensity: 'double' } },
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
A config
parameter copies
can be provided to send the specified raw commands multiple times. This is a convenience function as most raw languages already have an internal copies command.
function copies() {
var config = qz.configs.create("Printer Name", { copies: 4 });
var data = [
'Raw Data\n',
'More Raw Data\n',
'Even More Raw Data\n'
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
A config
parameter jobName
can be provided to change the name listed in the print queue.
function customJobName() {
var config = qz.configs.create("Printer Name", { jobName: "Receipt #123456" });
var data = [
'Raw Data\n',
'More Raw Data\n',
'Even More Raw Data\n'
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
When using CUPS (Linux/Unix/Mac), the printer driver can be bypassed completely by executing a command-line option
lpr -o raw my-raw-file.txt
This may be useful when troubleshooting issues with non-raw printer drivers. QZ Tray can substitute the standard printing behavior with this technique by adding a config
parameter altPrinting
. This parameter has no effect in a Windows environment.
function alternatePrint() {
var config = qz.configs.create("Printer Name", { altPrinting: true });
var data = [
'Raw Data\n',
'More Raw Data\n',
'Even More Raw Data\n'
];
qz.print(config, data).catch(function(e) { console.error(e); });
}
Print requests can be chained together to print several raw items at once, or to print to several different printers.
var config = qz.configs.create();
var data = [{ type: 'raw', data: null, }];
data.data = 'Some Raw Data #1'; ////// First raw item
config.setPrinter('First Printer'); ////// First printer
qz.print(config, data)
.then(function() {
data.data = 'Some Raw Data #2'; ////// Second raw item
config.setPrinter('Second Printer'); ////// First printer
return qz.print(config, data);
})
.then(function() {
data.data = 'Some Raw Data #3'; ////// Third raw item
config.setPrinter('Third Printer'); ////// Third printer
return qz.print(config, data);
})
.catch(function(e) {
console.error(e); // Exceptions throw all the way up the stack
});