【发布时间】:2017-07-27 11:28:27
【问题描述】:
我在字节数组中有 pdf 的内容。我想要一种使用 js 将此 pdf 打印到本地打印机的客户端方式。 有可能吗?
【问题讨论】:
我在字节数组中有 pdf 的内容。我想要一种使用 js 将此 pdf 打印到本地打印机的客户端方式。 有可能吗?
【问题讨论】:
$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')
});
【讨论】:
您需要先在浏览器窗口中通过 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()
【讨论】: