Last active
September 28, 2023 16:21
-
-
Save evagoras/cfd30035bd115dad79aa to your computer and use it in GitHub Desktop.
Printing ZPL using CFML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| net use lpt1: %1 | |
| copy %2 lpt1 | |
| net use lpt1: /d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://evagoras.com/2015/12/14/printing-zpl-using-cfml/