【问题标题】:How can using JS to print a Byte array to local machine printer?如何使用 JS 将字节数组打印到本地机器打印机?
【发布时间】:2017-07-27 11:28:27
【问题描述】:

我在字节数组中有 pdf 的内容。我想要一种使用 js 将此 pdf 打印到本地打印机的客户端方式。 有可能吗?

【问题讨论】:

    标签: pdf printing client


    【解决方案1】:
    $http({
        url: yourUrl,
        method: 'GET',
        headers: {
            'Content-type': 'application/pdf'
        },
        responseType: 'arraybuffer'
    }).success(function (data, status, headers, config) {
        var pdfFile = new Blob([data], {
            type: 'application/pdf'
        });
        var pdfUrl = URL.createObjectURL(pdfFile);
        var printwWindow = $window.open(pdfUrl);
        printwWindow.print();
    }).error(function (data, status, headers, config) {
        alert('Sorry, something went wrong')
    });
    

    【讨论】:

      【解决方案2】:

      您需要先在浏览器窗口中通过 Blob 打开它并调用打印函数
      我还没有测试过这段代码,但希望它能工作

      var byteArray = yourDocumentBytes;
      var ua = window.navigator.userAgent;
      var msie = ua.indexOf("MSIE ");
      // This will check if the browser is IE
      if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))
      { 
       var blob = new Blob(byteArray, { type: 'application/pdf' });
       window.navigator.msSaveBlob(blob, documentName);
      } else // If another browser
      { 
        var element = document.createElement('a');
        element.setAttribute('href', 'data:application/pdf;base64,' + encodeURIComponent(getBase64(byteArray)));
        element.setAttribute('download', documentName);
        element.style.display = 'none';
        document.body.appendChild(element);
        element.click();
        document.body.removeChild(element);
      }
      // and call print function like this
      window.print()
      

      【讨论】:

        猜你喜欢
        • 2020-04-01
        • 2011-03-29
        • 1970-01-01
        • 1970-01-01
        • 2018-02-28
        • 1970-01-01
        • 1970-01-01
        • 2015-05-30
        • 1970-01-01
        相关资源
        最近更新 更多