Skip to content

Instantly share code, notes, and snippets.

@tresf
Last active July 12, 2024 05:39
Show Gist options
  • Save tresf/95f28644669a1e1970a7 to your computer and use it in GitHub Desktop.
Save tresf/95f28644669a1e1970a7 to your computer and use it in GitHub Desktop.
(Experimental) Scales a Raw ZPL file from 203 DPI (dots per inch) to 300 DPI
/*
* author: Tres Finocchiaro
* date: 2015-05-22
* license: Public Domain; Use as you wish.
* source: http://qz.io
*/
/*
* Scales text from a raw ZPL label from 203 DPI to 300 DPI
*/
function scaleZPL(rawCommands, scaleFactor) {
if (!scaleFactor) {
scaleFactor = 300/203;
}
var sections = rawCommands.split('^');
// ZPL commands to perform conversion on
var cmds = ['FO', 'A0', 'A@', 'LL', 'LH', 'GB', 'FB', 'BY', 'B3', 'FT'];
var output = '';
for (var i in cmds) {
for (var j in sections) {
if (sections[j].indexOf(cmds[i]) === 0) {
sections[j] = scaleSection(cmds[i], sections[j], scaleFactor);
}
}
}
return sections.join('^');
}
/*
* Scales all integers found in a designated section
*/
function scaleSection(cmd, section, scaleFactor) {
section = section.slice(cmd.length, section.length);
parts = section.split(',');
for (var p in parts) {
if (isInt(parts[p])) {
parts[p] = Math.round(scaleFactor * parts[p]);
}
}
return cmd + parts.join();
}
/*
* Checks if a string is an integer
*/
function isInt(value) {
return !isNaN(value) &&
parseInt(Number(value)) == value &&
!isNaN(parseInt(value, 10));
}
@timdw
Copy link

timdw commented Nov 11, 2016

Hi,

I used this as the basis for a powershell version - see https://gist.github.com/timdw/391ecb62efd1dde9f6af017ead208ec9

Thanks!

Tim

@alethea
Copy link

alethea commented May 10, 2017

I added some additional scaling functionality for barcodes: https://gist.github.com/alethea/3e409b0b1206099bee88eefac7fa7cbd

@isatufan
Copy link

C# .NET version at isatufan/ZPLHandler.cs

@enovision
Copy link

I added a PHP version at: gist.

I added the 'FT' tag to the cmds array.

@deexno
Copy link

deexno commented Feb 13, 2023

I added a Python version of the Zebra ZPL Rescaler at https://github.com/deexno/Zebra-ZPL-rescaler.

I've made the input more user-friendly, allowing the user to convert their ZPL file from any resolution to any other resolution without touching the code.

@tresf
Copy link
Author

tresf commented Feb 13, 2023

@deexno thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment