Skip to content

Instantly share code, notes, and snippets.

@evagoras
Last active September 28, 2023 16:21
Show Gist options
  • Select an option

  • Save evagoras/cfd30035bd115dad79aa to your computer and use it in GitHub Desktop.

Select an option

Save evagoras/cfd30035bd115dad79aa to your computer and use it in GitHub Desktop.
Printing ZPL using CFML
<cffunction name="_createBatchFile" access="private" returntype="string" output="false">
<cfset var LOCAL = structNew() />
<cfset LOCAL.bFileExists = false />
<cfset LOCAL.cAbsoluteFilePath = expandPath( "/print-zpl.bat" ) />
<cfset LOCAL.cFileContents = "" />
<cfif fileExists( LOCAL.cAbsoluteFilePath )>
<cfset LOCAL.bFileExists = true />
</cfif>
<cfif NOT LOCAL.bFileExists>
<cfsavecontent variable="LOCAL.cFileContents"><cfoutput>net use lpt1: %1#chr(13) & chr(10)#
copy %2 lpt1#chr(13) & chr(10)#
net use lpt1: /d</cfoutput></cfsavecontent>
<cflock timeout="15" scope="request" type="exclusive">
<cffile action="write" file="#LOCAL.cAbsoluteFilePath#" output="#LOCAL.cFileContents#" />
</cflock>
</cfif>
<cfreturn LOCAL.cAbsoluteFilePath />
</cffunction>
<cffunction name="_writeToZplFile" access="private" returntype="string" output="false">
<cfargument name="zpldata" type="string" required="true" />
<cfset var LOCAL = structNew() />
<cfset LOCAL.cAbsoluteFilePath = expandPath( "/print-zpl.txt" ) />
<cflock timeout="15" scope="request" type="exclusive">
<cffile action="write" file="#LOCAL.cAbsoluteFilePath#" output="#ARGUMENTS.zpldata#" />
</cflock>
<cfreturn LOCAL.cAbsoluteFilePath />
</cffunction>
net use lpt1: %1
copy %2 lpt1
net use lpt1: /d
public any function printToConnectedPrinter
(
required string uncPath,
string zpl = ""
)
{
var zplBatchAbsoluteFilePath = _createBatchFile();
var zplDataAbsoluteFilePath = _writeToZplFile( arguments.zpl );
var exeArgs = [];
var errorMsg = "";
try {
exeArgs[1] = arguments.uncPath;
exeArgs[2] = zplDataAbsoluteFilePath;
execute {
variable="batchScriptOutput",
name="#LOCAL.zplBatchAbsoluteFilePath#",
arguments="#LOCAL.aArguments#",
timeout="15",
errorVariable="batchScriptError",
}
if ( len( batchScriptError ) )
errorMsg = batchScriptError;
catch {
rethrow();
}
}
return errorMsg;
}
public any function printToNetworkPrinter
(
required string ip,
required numeric port,
string zpl=""
)
{
// open and init java socket connection
var sock = createObject( "java", "java.net.Socket" );
sock.init( arguments.ip, arguments.port );
// socket is open and ready to receive
var connected = sock.isConnected();
if ( connected ) {
// create an output stream buffer for writing out the ZPL
var streamOut = sock.getOutputStream();
var output = createObject("java", "java.io.DataOutputStream").init( streamOut );
streamOut.flush();
// send the ZPL by writing to the output buffer
output.writeBytes( arguments.zpl );
streamOut.flush();
}
// close down and clean up the socket connection
sock.shutdownOutput();
sock.close();
return connected;
}
<!--- start of label format --->
^XA
<!--- label home position --->
^LH0,0
<!--- label length --->
^LL1600
<!--- STOCKCODE header --->
^FO52,70 ^A0N,36 ^FDSTOCKCODE ^FS
<!--- underline --->
^FO42,110 ^GB748,0,2 ^FS
<!--- barcode --->
^FO52,160 ^BY1,1.0 ^B3N,,200,N ^FD#trim(stockcode)# ^FS
<!--- actual stockcode --->
^FO52,380 ^A0N,36 ^FB728,2 ^FD#trim(stockcode)# ^FS
<!--- BIN header --->
^FO52,550 ^A0N,36 ^FDBIN ^FS
<!--- underline --->
^FO42,590 ^GB748,0,2 ^FS
<!--- barcode --->
^FO52,640 ^BY1,1.0 ^B3N,,200,N ^FD#trim(bin)# ^FS
<!--- actual bin --->
^FO52,860 ^A0N,36 ^FD#trim(bin)# ^FS
<!--- print quantity --->
^PQ#quantity#
<!--- print rate per second --->
^PR6
<!--- end of label format --->
^XZ
@evagoras

Copy link
Copy Markdown
Author

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