i leave a sample to print from WEB in RAW format without call the print dialog, only work with browser with websocket
Descargar ejecutable / Download exe
Ejecutar desde consola / run from console
test NONE
con ESC sale del programa / exit with ESC
abrir la siguiente url / open the url
http://danielgarciagil.com/print (no funciona si el ejecutable no esta corriendo / no work if exe is not running )
PRG
Code: Select all
#include "hbclass.ch"
#include "FileIO.ch"
#include "hbwin.ch"
#define CRLF Chr(13)+Chr(10)
#xcommand REPEAT => while .t.
#xcommand DO => while .t.
#xcommand UNTIL <uExpr> => if <uExpr>; exit; end; end
#xcommand DEFAULT <uVar1> := <uVal1> ;
[, <uVarN> := <uValN> ] => ;
If( <uVar1> == nil, <uVar1> := <uVal1>, ) ;;
[ If( <uVarN> == nil, <uVarN> := <uValN>, ); ]
function main( cMode )
LOCAL oSrv
LOCAL oPrn
DEFAULT cMode := "S"
oSrv := WebSocketServer():New( "8080", cMode, {| oServer | OnInit( oServer ) } )
if ! oSrv:lBackground
oSrv:Activate()
endif
return nil
//-----------------------------------------//
function OnInit( oSrv )
oSrv:bOnNewClient = {| oClient | NewClient( oSrv, oClient ) }
oSrv:bOnHandleEvent = {| nEvent, wParam, lParam, cParam, oClient | HandleEvent( nEvent, wParam, lParam, cParam, oClient ) }
return nil
//-----------------------------------------//
function printTexto( hBuffer )
local oPrn
local hLine
local cBuffer := ""
local cPrinter
oPrn := TRawPrint():New()
for each hLine in hBuffer[ "texto" ]
oPrn:say( hLine["row"], hLine["col"], hLine["text"] )
next
oPrn:Print( hBuffer[ "printer" ] )
return nil
//-----------------------------------------//
function getPrinterList( oClient )
local aPrinter := Win_PrinterList()
local hJson := { => }
local nLen, cJSon
hJSon[ "ACTION" ] = "PRINTERS"
hJson[ "PARAMETERS" ] = { aPrinter, WIN_PrinterGetDefault() }
cJSon := hb_JSonEncode( hJson, @nlen, .T. )
oClient:SendData( cJSon )
return nil
//-----------------------------------------//
function HandleEvent( nEvent, wParam, lParam, cParam, oClient )
local hJSon, nTipo
local nJson
nJSon = hb_jsonDecode( cParam, @hJSon )
switch nEvent
case 1
getPrinterList( oClient )
exit
case 2
printTexto( hJSon )
exit
endswitch
return nil
//-----------------------------------------//
function NewClient( oSrv, oClient )
local nLen
local nPos
local o
//accept only one connection (only for test)
//if there are a new conection, will kill old
nLen = Len( oSrv:hClients )
if nLen > 1
nPos := hb_HScan( oSrv:hClients, {|k| k != oClient:nID } )
if nPos > 0
o = hb_HValueAt( oSrv:hClients, nPos )
oSrv:KillClient( o )
endif
endif
RETURN NIL
#xtranslate nTrim( <n>, <i>, <d> ) => AllTrim( Str( <n>, <i>, <d> ) )
//----------------------------------------------------------------------------//
CLASS TRawPrint
DATA hRow, hCol HIDDEN
DATA hDC, nRow, nCol, nLeftMargin, nTopMargin AS NUMERIC
DATA lFile AS LOGICAL
DATA onNewPage
DATA nPage AS NUMERIC
DATA onEndPage
DATA onClose
DATA aBuffer AS ARRAY
DATA lAnsiToOem AS LOGICAL
METHOD New( lFile, onNewPage, onEndPage, onClose ) CONSTRUCTOR
METHOD Print( cPrinter )
METHOD Say( nRow, nCol, cText, lInsert )
ENDCLASS
//----------------------------------------------------------------------------//
METHOD New( lFile, onNewPage, onEndPage, onClose ) CLASS TRawPrint
DEFAULT lFile := .F.
::nLeftMargin = 0
::nTopMargin = 0
::nRow = 0
::nCol = 0
::lAnsiToOem = .T.
::hDC = -1
::lFile = lFile
::onNewPage = onNewPage
::onEndPage = onEndPage
::onClose = onClose
::nPage = 1
::aBuffer = {}
RETURN Self
//----------------------------------------------------------------------------//
METHOD Print( cPrinter ) CLASS TRawPrint
local cBuffer := ""
local cLine
DEFAULT cPrinter := WIN_PrinterGetDefault()
for each cLine in ::aBuffer
cBuffer += cLine + CRLF
next
WIN_PrintBufferRaw( cPrinter , cBuffer )
RETURN NIL
//----------------------------------------------------------------------------//
METHOD Say( nRow, nCol, cText, lInsert ) CLASS TRawPrint
local nLen := Len( ::aBuffer )
local n
local nLenStr, nLenSub
local cStr, cSubstr
local cNewLine := ""
DEFAULT lInsert := .F.
if nRow > nLen
for n = nLen + 1 to nRow
AAdd( ::aBuffer, "" )
next
endif
cStr = ::aBuffer[ nRow ]
cSubstr = PadR( cStr, nCol, " " )
if lInsert
cNewLine = Stuff( cSubstr, nCol, 0, cText )
else
cNewLine = Stuff( cSubstr, nCol, Len( cText ), cText )
endif
::aBuffer[ nRow ] = cNewLine
RETURN NIL