Jump to content

JS problem


Eugeniusz Rink

Recommended Posts

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:

 

image.png.e06d9aa60201c19ce3e5c910393132cd.png

 

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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.

 

image.png.368d747312dc9b8fc717dad5c33fb294.png

websocket-printer.js

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...