Skip to content

Instantly share code, notes, and snippets.

@timdw

timdw/scale-zpl.ps1

Last active Aug 16, 2018
Embed
What would you like to do?
(Experimental) Scales a Raw ZPL file from 203 DPI (dots per inch) to 300 DPI
<#
# author: Tim Walker
# date: 2016-11-11
# original author: Tres Finocchiaro
# original date: 2015-05-22
# license: Public Domain; Use as you wish.
# original source: https://gist.github.com/tresf/95f28644669a1e1970a7
#>
<#
# Scales all integers found in a designated section
#>
function scaleSection([string]$cmd, [string]$section, [double]$scaleFactor) {
[bool]$isInt = $false
[int]$toInt = 0
$parts = $section.Substring($cmd.length).Split(',')
for ($i = 0; $i -lt $parts.Count; $i++) {
$isInt = [int]::TryParse($parts[$i], [ref]$toInt)
if ($isInt) {
$eol = ''
if ($parts[$i].EndsWith("`r`n")) {
$eol = "`r`n"
}
elseif ($parts[$i].EndsWith("`n")) {
$eol = "`n"
}
elseif ($parts[$i].EndsWith("`r")) {
$eol = "`r"
}
$parts[$i] = ($scaleFactor * $toInt).ToString("000") + $eol
}
}
return $cmd + ($parts -join ',')
}
<#
# Scales text from a raw ZPL label file from 203 DPI to 300 DPI
#>
function scaleZPL([string]$inputFile, [double]$scaleFactor = 300/203) {
$sections = (Get-Content -path $inputFile -Delimiter `0).Split('^')
# ZPL commands to perform conversion on
$zplmatchcmds = @('FO', 'A[^,]*', 'LL', 'LH', 'GB', 'FB', 'B[^,]*')
$regexcmds = "^((" + ($zplmatchcmds -join ')|(') + ')).*'
# Can't do foreach here as it creates a copy of the array
for ($i = 0; $i -lt $sections.Count; $i++) {
$cmdmatch = $sections[$i] | Select-String -Pattern $regexcmds
if ($cmdmatch.Matches.Groups.Count -ne 0) {
$cmd = $cmdmatch.Matches.Groups[1].Value
$sections[$i] = scaleSection -cmd $cmd -section $sections[$i] -scaleFactor $scaleFactor
}
}
($sections -join '^')
}
# example usage
scaleZPL -inputFile 'c:\myfile.zpl'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment