我正在尝试仅将文字从网络应用程序打印到热敏打印机。理想情况下,此功能将位于SharePoint Online网站中以供打印收据使用。
我使用了window.print
并在Microsoft Edge和Internet Explorer (IE9 +)中进行了测试,并且所选文本的打印效果正常。
但是,在谷歌浏览器中,按下打印预览中的“打印”按钮后,热敏打印机仅显示空白纸张。
以下源代码只是关于我们如何“在新窗口中”打印文本的示例;但是,正如我所说,在谷歌浏览器中没有打印。
function printThisDocument() {
try {
var mywindow = window.open('', 'my div', 'height=600,width=1000');
mywindow.document.write('<html moznomarginboxes mozdisallowselectionprint><head><title>Sticker #1</title>');
mywindow.document.write('</head><body>');
mywindow.document.write(document.getElementById('divHidden').innerHTML.trim());
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
setTimeout(function(){ mywindow.print(); mywindow.close(); }, 500);
} catch (ex) {
alert('An error ocurred. Try again.');
console.log(ex);
}
}
我在Chrome中的打印预览中选择了各种纸张尺寸,但只有“Letter”纸张大小,热敏打印机“打印 - 实际上,使用纸张,但不打印结果显示”。
如果我选择了其他纸张尺寸,则热敏打印机根本不使用任何纸张。
任何突出显示的文字都会出现这种情况(您可以尝试选择文字,右键单击并选择“打印”)。
Chrome将文字打印为图形,或者至少在所有网站上打印 试过。它使用线条绘制来绘制文本而不是打印 实际字符
在此bug report:
[...]不幸的是,由于铬印刷的方式,它不可能:( chrome cant print plain text [...]
所以,我没有想法就跑了。
¿有什么原因/动机可以解释为什么Chrome无法打印通用文字?
我需要做的是使用热敏打印机或贴纸机打印机从SharePoint Online网站打印通用文本。
我检查了另一个关于javascipt无法与驱动程序和其他服务器对象交互的答案,但我也检查了java applet,但我不确定applet是如何工作的,如果这是唯一的解决方案 - 我虽然applet已经过时了。
答案 0 :(得分:3)
我最后使用QZ Tray 2.0.4 免费版。
调整我demo website中可用的源代码 贴纸使用浏览器 1 。
注意:如果您想使用此代码,则必须下载并安装到您的计算机“或您客户的计算机” QZ Tray 2.0.4和 根据您的目的修改此处提供的源代码。您 也可能 check the official documentation 。还有,QZ托盘 必须运行2.0.4并且用户必须接受QZ Tray 2.0.4连接到签名的localhost以检查打印机。在这种情况下, 我用findDefaultPrinter(true);获取默认打印机的功能。
<!-- Required scripts-->
<script type="text/javascript" src="/js/dependencies/rsvp-3.1.0.min.js"></script>
<script type="text/javascript" src="/js/dependencies/sha-256.min.js"></script>
<script type="text/javascript" src="/js/qz-tray.js"></script>
<script type="text/javascript">
/// This is the function I use in the "onclick" attribute.
function printStuff() {
var config = getUpdatedConfig();
var data = ['Raw Data\n', 'More Raw Data\n', 'Even More Raw Data\n'];
// Print.
qz.print(config, data).catch(function(e) { console.error(e); });
}
/// Authentication setup ///
qz.security.setCertificatePromise(function(resolve, reject) {
//Preferred method - from server
//$.ajax("assets/signing/digital-certificate.txt").then(resolve, reject);
//Alternate method 1 - anonymous
//resolve();
//Alternate method 2 - direct
resolve("-----BEGIN CERTIFICATE-----\n" +
"MIIFAzCCAuugAwIBAgICEAIwDQYJKoZIhvcNAQEFBQAwgZgxCzAJBgNVBAYTAlVT\n" +
"MQswCQYDVQQIDAJOWTEbMBkGA1UECgwSUVogSW5kdXN0cmllcywgTExDMRswGQYD\n" +
"VQQLDBJRWiBJbmR1c3RyaWVzLCBMTEMxGTAXBgNVBAMMEHF6aW5kdXN0cmllcy5j\n" +
"b20xJzAlBgkqhkiG9w0BCQEWGHN1cHBvcnRAcXppbmR1c3RyaWVzLmNvbTAeFw0x\n" +
"NTAzMTkwMjM4NDVaFw0yNTAzMTkwMjM4NDVaMHMxCzAJBgNVBAYTAkFBMRMwEQYD\n" +
"VQQIDApTb21lIFN0YXRlMQ0wCwYDVQQKDAREZW1vMQ0wCwYDVQQLDAREZW1vMRIw\n" +
"EAYDVQQDDAlsb2NhbGhvc3QxHTAbBgkqhkiG9w0BCQEWDnJvb3RAbG9jYWxob3N0\n" +
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtFzbBDRTDHHmlSV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ACmnlHdP7U9WLr5TYnmL9eoHwtb0hwENe1oFC5zClJoSX/7DRexSJfB7YBf\n" +
"vn6JA2xy4C6PqximyCPisErNp85GUcZfo33Np1aywFv9H+a83rSUcV6kpE/jAZio\n" +
"5qLpgIOisArj1HTM6goDWzKhLiR/AeG3IJvgbpr9Gr7uZmfFyQzUjvkJ9cybZRd+\n" +
"G8azmpBBotmKsbtbAU/I/LVk8saeXznshOVVpDRYtVnjZeAneso7\n" +
"-----END CERTIFICATE-----\n" +
"--START INTERMEDIATE CERT--\n" +
"-----BEGIN CERTIFICATE-----\n" +
"MIIFEjCCA/qgAwIBAgICEAAwDQYJKoZIhvcNAQELBQAwgawxCzAJBgNVBAYTAlVT\n" +
"MQswCQYDVQQIDAJOWTESMBAGA1UEBwwJQ2FuYXN0b3RhMRswGQYDVQQKDBJRWiBJ\n" +
"bmR1c3RyaWVzLCBMTEMxGzAZBgNVBAsMElFaIEluZHVzdHJpZXMsIExMQzEZMBcG\n" +
"A1UEAwwQcXppbmR1c3RyaWVzLmNvbTEnMCUGCSqGSIb3DQEJARYYc3VwcG9ydEBx\n" +
"emluZHVzdHJpZXMuY29tMB4XDTE1MDMwMjAwNTAxOFoXDTM1MDMwMjAwNTAxOFow\n" +
"gZgxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJOWTEbMBkGA1UECgwSUVogSW5kdXN0\n" +
"cmllcywgTExDMRswGQYDVQQLDBJRWiBJbmR1c3RyaWVzLCBMTEMxGTAXBgNVBAMM\n" +
"EHF6aW5kdXN0cmllcy5jb20xJzAlBgkqhkiG9w0BCQEWGHN1cHBvcnRAcXppbmR1\n" +
"c3RyaWVzLmNvbTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBANTDgNLU\n" +
"iohl/rQoZ2bTMHVEk1mA020LYhgfWjO0+GsLlbg5SvWVFWkv4ZgffuVRXLHrwz1H\n" +
"YpMyo+Zh8ksJF9ssJWCwQGO5ciM6dmoryyB0VZHGY1blewdMuxieX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Xll/R6iGw\n" +
"2ur4rDErnHsiphBgZB71C5FD4cdfSONTsYxmPmyUb5T+KLUouxZ9B0Wh28ucc1Lp\n" +
"rbO7BnjW\n" +
"-----END CERTIFICATE-----\n");
});
qz.security.setSignaturePromise(function(toSign) {
return function(resolve, reject) {
//Preferred method - from server
//$.ajax("/secure/url/for/sign-message?request=" + toSign).then(resolve, reject);
//Alternate method - unsigned
resolve();
};
});
/// Connection ///
function launchQZ() {
if (!qz.websocket.isActive()) {
window.location.assign("qz:launch");
//Retry 5 times, pausing 1 second between each attempt
startConnection({ retries: 5, delay: 1 });
}
}
function startConnection(config) {
if (!qz.websocket.isActive()) {
qz.websocket.connect(config).then(function() {
findDefaultPrinter(true); /* Instead of findVersion(); */
}).catch(handleConnectionError);
} else {
displayAlert('An active connection with QZ already exists.');
}
}
function endConnection() {
if (qz.websocket.isActive()) {
qz.websocket.disconnect().then(function() {
alert('Desconectado');
}).catch(handleConnectionError);
} else {
displayAlert('No active connection with QZ exists. Openinig a new one.');
startConnection();
}
}
// Recargar la página para iniciar la conexión.
function restartConnection() {
startConnection({ retries: 5, delay: 1 });
}
/// Detection ///
function findDefaultPrinter(set) {
qz.printers.getDefault().then(function(data) {
displayAlert("La impresora (" + data + ") ha sido encontrada.\nAhora puede proceder a imprimir stickers.");
if (set) { setPrinter(data); }
}).catch(createConsoleLog);
}
/// Raw Printers ///
function printEPL() {
var config = getUpdatedConfig();
/* Información de prueba. */
var printData = [
'\nN\n',
'q609\n',
'Q203,26\n',
'B5,26,0,1A,3,7,152,B,"1234"\n',
'A310,26,0,3,1,1,N,"SKU 00000 MFG 0000"\n',
'A310,56,0,3,1,1,N,"QZ PRINT APPLET"\n',
'A310,86,0,3,1,1,N,"TEST PRINT SUCCESSFUL"\n',
'A310,116,0,3,1,1,N,"FROM SAMPLE.HTML"\n',
'A310,146,0,3,1,1,N,"QZ.IO"\n',
'\nP1,1\n'
];
qz.print(config, printData).catch(createConsoleLog);
}
/// Page load ///
$(document).ready(function() {
window.readingWeight = false;
startConnection();
// ¡La conexión con la impresora se establece cuando la conexión sea exitosa!
alert('============================================\n' +
' You're using QZ Tray software.\n' +
'============================================\n' +
'For sticker printing, you mus install\n' +
'QZ Tray 2.0.4\n\n' +
'QZ Tray 2.0.4 will detect the default printer.\n\n' +
'Click on "Accept" and\n' +
'Wait until your printer's name is shown.');
});
qz.websocket.setClosedCallbacks(function(evt) {
console.log(evt);
if (evt.reason) {
displayAlert("Connection closed:" + evt.reason);
}
});
qz.websocket.setErrorCallbacks(handleConnectionError);
/// Helpers ///
function handleConnectionError(err) {
if (err.target != undefined) {
if (err.target.readyState >= 2) { //if CLOSING or CLOSED
createConsoleLog("Connection to QZ Tray was closed");
} else {
createConsoleLog("A connection error occurred, check log for details");
console.error(err);
}
} else {
createConsoleLog(err);
}
}
function createConsoleLog(err) {
console.error(err);
displayAlert(err);
}
function displayAlert(msg, css) {
alert(msg);
}
/// QZ Config ///
var cfg = null;
function getUpdatedConfig() {
if (cfg == null) {
cfg = qz.configs.create(null);
}
updateConfig();
return cfg
}
// Los parámetros estarán como "undefined" ya que no existen en mi código final.
function updateConfig() {
var pxlSize = null;
var pxlMargins = $("#pxlMargins").val(); // = undefined.
var jobName = "Impresión Sticker";
cfg.reconfigure({
altPrinting: $("#rawAltPrinting").prop('checked'),
encoding: $("#rawEncoding").val(),
endOfDoc: $("#rawEndOfDoc").val(),
perSpool: $("#rawPerSpool").val(),
colorType: $("#pxlColorType").val(),
copies: 1,
density: $("#pxlDensity").val(),
duplex: $("#pxlDuplex").prop('checked'),
interpolation: $("#pxlInterpolation").val(),
jobName: jobName,
margins: pxlMargins,
orientation: $("#pxlOrientation").val(),
paperThickness: $("#pxlPaperThickness").val(),
printerTray: $("#pxlPrinterTray").val(),
rasterize: $("#pxlRasterize").prop('checked'),
rotation: $("#pxlRotation").val(),
scaleContent: $("#pxlScale").prop('checked'),
size: pxlSize,
units: $("input[name='pxlUnits']:checked").val()
});
}
function setPrinter(printer) {
var cf = getUpdatedConfig();
cf.setPrinter(printer);
}
</script>
<!--FIN-->
1 在Google Chrome版本60.0.3112.90(Build oficial)(64位)中进行测试。