【问题标题】:Silent printing using PrintNode JS API使用 PrintNode JS API 进行静默打印
【发布时间】:2018-06-16 15:39:32
【问题描述】:

我正在尝试使用PrintNode-JS 在浏览器中使用javascript 和PrintNode 远程打印服务静默打印。但我无法正确地做到这一点。所以我需要一些适当的 javascript 代码。我正在尝试以下代码: https://www.printnode.com/

<script type='text/javascript'>

    var API_KEY = '<your_api_key_here>';

    // got websocket
    if (!PrintNode.WebSocket.isSupported()) {

        // once a websocket is authenticated you can register
        function authenticated (authData) {
            if (authData.error) {
                // most likely not authenticated, see authData.error for more detail
                return;
            }
            // authData will contain information about accountId, permissions, and maxSubscriptions

            // ok, now make some requests to the server to get data you're interested in
            // pass in optional second argument to have this called when server publishes a event
            // but you can also use the event emitter
            this.getScales({}, function (measurement) {
                // this is only meaningful if the websocket is running on the same machine
                // as the running PrintNode client
                console.log("scales data by subscription callback", measurement);
                console.log("scales latency %dms", measurement.getLatency());
            });

        }

        // error callback fired if anything goes wrong including
        //  - exceptions thrown by subscription callbacks
        //  - network issues which cause socket to fail or any timeouts
        //  - errors in printnode server or client libs
        function errorCallback (err, data) {
            console.log("Error!", err, data);
        }

        // instantiate a new
        var ws = new PrintNode.WebSocket(
            {apiKey: API_KEY},
            authenticated,
            errorCallback
        );

        // subscribe to all computer events
        ws.subscribe("computer", function computerEvent (something, info) {
            // lots of things could come in on this event
            // for now we're only interested in scalesMeasurements
            if (something instanceof PrintNode.ScalesMeasurement) {
                console.log("scales data by computer subscription", something);
            }
        });

        // subscribe to all scales events
        ws.subscribe("scales", function computerEvent (measurement) {
            console.log("scales data by scales subscription", measurement);
        });

        // debugging
        ws.subscribe('system.state', function stateChange (newState) {
            console.log("newState", newState);
        });

    // TODO fallback to polling
    } else {

        var httpOptions = {
            success: function (responseBody, repsonse) {
                console.log("success", response);
            },
            error: function (responseBody, repsonse) {
                console.log(response);
            },
            complete: function (response) {
                console.log(
                    "%d %s %s returned %db in %dms",
                    response.xhr.status,
                    response.reqMethod,
                    response.reqUrl,
                    response.xhr.responseText.length,
                    response.getDuration()
                );
            },
            timeout: function (url, duration) {
                console.log("%s timeout %d", url, duration);
            }
        };

        var http = new PrintNode.HTTP(
            new PrintNode.HTTP.ApiKey(API_KEY),
            httpOptions
        );

        http.scales(
            {success: function (response) {
                console.log("success from callback", response);
            }},
            {computerId: 0}
        ).then(
            function (response, info ) {
                console.log("success from promise", response);
            },
            function (err) {
                throw "simulated exception in a promise callback";
            }
        // If your promise callbacks start throw[ing] you can 'catch' this in the returned
        // promise from .then() but this is getting pretty meta right here.
        // Callbacks really shouldn't throw exceptions in async code like this.
        ).then(null, console.log.bind(console, "promise callbacks threw error;"));

    }

    </script> 

【问题讨论】:

    标签: javascript printing silent


    【解决方案1】:

    问题是示例代码中有拼写错误。

    在 websocket 示例中用“response”查找并替换“repsonse”

    另外,在 HTTP 客户端示例中,要打印的函数应该是

    api.createPrintjob(options, printJobPayload)
    

    而不是

    api.printjob ...
    

    【讨论】:

    • 我知道回复这篇文章已经很晚了。即使我更正了错别字,它也不起作用。根据我的要求,PrintNode.js 对我来说不是很有用,所以我不再使用它了。但我想在这里留下一个对其他用户有用的重要评论。所有这些 JS 打印方法都将文件发送到他们的服务器,然后将命令发送到客户端打印机进行打印,可能像任何桌面应用程序一样通过一些打印桥,所以这些会消耗大量时间。想想一次打印 5000 个文件,然后想象一下延迟时间。
    • 我想在上面的@AbhishekSharma 评论中添加内容,并说 PrintNode 确实有本地拉取设置。如果您给他们一个 HTTP URL 而不是要打印的文件,本地打印客户端将在打印时将文件直接获取到机器。如果您正在处理更大的文件,它会提高速度。它仅在您已经在服务器上生成文件时才有效,如果您在浏览器中制作标签/PDF,则必须将它们上传到外部以便打印客户端获取它们。您不能通过 localhost 发送它们。
    猜你喜欢
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-17
    • 2019-05-09
    • 2020-12-22
    • 2014-01-02
    相关资源
    最近更新 更多