Eugeniusz Rink Posted February 28 Share Posted February 28 Hi. I have a problem running JS. script1:= ' function WebSocketPrinter(options) { '+ ' var defaults = { '+ ' url: "ws://127.0.0.1:12212/printer", '+ ' onConnect: function () { '+ ' }, '+ ' onDisconnect: function () { '+ ' }, '+ ' onUpdate: function () { '+ ' }, '+ ' }; '+ ' var settings = Object.assign({}, defaults, options); '+ ' var websocket; '+ ' var connected = false; '+ ' var onMessage = function (evt) { '+ ' settings.onUpdate(evt.data); '+ ' }; '+ ' var onConnect = function () { '+ ' connected = true; '+ ' settings.onConnect(); '+ ' }; '+ ' var onDisconnect = function () { '+ ' connected = false; '+ ' settings.onDisconnect(); '+ ' reconnect(); '+ ' }; '+ ' var connect = function () { '+ ' websocket = new WebSocket(settings.url); '+ ' websocket.onopen = onConnect; '+ ' websocket.onclose = onDisconnect; '+ ' websocket.onmessage = onMessage; '+ ' }; '+ ' var reconnect = function () { '+ ' connect(); '+ ' }; '+ ' this.submit = function (data) { '+ ' if (Array.isArray(data)) { '+ ' data.forEach(function (element) { '+ ' websocket.send(JSON.stringify(element)); '+ ' }); '+ ' } else { '+ ' websocket.send(JSON.stringify(data)); '+ ' } '+ ' }; '+ ' this.isConnected = function () { '+ ' return connected; '+ ' }; '+ ' connect(); '+ '} ' + ' var printService = new WebSocketPrinter(); '+ ' function printPDFBase64(typ,pdfbase64) { '+ ' printService.submit({ '+ ' ''type'': typ, '+ ' ''url'': ''tuxmed.pdf'', '+ ' ''file_content'': pdfbase64 '+ ' }); '+ ' } '; UniSession.AddJS(script1); I trigger a function with parameters: UniSession.AddJS('printPDFBase64("'+UniApplication.Parameters.Values['gabinet']+'","'+pdf_base64+'")'); I get this error: Quote Link to comment Share on other sites More sharing options...
Hayri ASLAN Posted February 28 Share Posted February 28 You need to make sure you are connected to websocket before sending the message Quote Link to comment Share on other sites More sharing options...
Eugeniusz Rink Posted February 28 Author Share Posted February 28 10 minutes ago, Hayri ASLAN said: You need to make sure you are connected to websocket before sending the message If I add a script definition in UniServerModule: CustomFiles:='files/rs/js/websocket-printer.js' run the function: UniSession.AddJS('printPDFBase64("'+UniApplication.Parameters.Values['office']+'","'+pdf_base64+'")'); It works properly and without errors... But this does not solve the problem, because I have to change the server port for each logging user. I need to change this entry in the JS script: url: "ws://127.0.0.1:12212/printer". Quote Link to comment Share on other sites More sharing options...
Eugeniusz Rink Posted February 29 Author Share Posted February 29 Problem solved. Below I show how I did it. This is a way to silently print PDF documents. I used the project: https://github.com/imTigger/webapp-hardware-bridge You install the Windows client: https://github.com/imTigger/webapp-hardware-bridge/releases/tag/0.14.0 In the Delphi code: function LoadJavaScriptFromFile(const FileName: string): string; var FileStream: TFileStream; StreamSize: Int64; Buffer: AnsiString; begin Result := ''; FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone); try StreamSize := FileStream.Size; SetLength(Buffer, StreamSize); FileStream.Read(Buffer[1], StreamSize); Result := String(Buffer); finally FileStream.Free; end; end; [...] var JavaScriptCode: String; [...] JavaScriptCode := LoadJavaScriptFromFile(UniServerModule.StartPath+'files\res\js\websocket-printer.js'); JavaScriptCode := StringReplace(JavaScriptCode, '$url$', '"ws://127.0.0.1:12213/printer"', [rfReplaceAll]); JavaScriptCode := StringReplace(JavaScriptCode, '$typ$', ''''+UniApplication.Parameters.Values['gabinet']+'''', [rfReplaceAll]); JavaScriptCode := StringReplace(JavaScriptCode, '$pdfbase64$', ''''+pdf_base64+'''', [rfReplaceAll]); UniSession.AddJS(JavaScriptCode); Of course, adapt the variables to your needs. The PDF file must be converted to Base64 without line-ending enters. websocket-printer.js 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.