我相信这是一个简单的问题,但我很难理解我在这里遗漏了什么。跳到底部查看实际问题。
背景我正在修改Wordpress POS插件,以使用QZ托盘直接打印POS的收据,而无需打印对话。当前,现有插件中的打印功能如下所示:
function posPrintReceipt(url, gift_receipt) {
gift_receipt = gift_receipt || false;
var find = \'&\';
var re = new RegExp(find, \'g\');
url = url.replace(re, \'&\');
url = url + \'&gift_receipt=\' + gift_receipt;
if (wc_pos_params.tabs_management) {
url = url + \'&tab_id=\' + jQuery(\'.woocommerce_order_items_wrapper .tab.active\').data(\'tab_number\');
}
openModal(\'modal-printing-receipt\');
if (jQuery(\'#printable\').length)
jQuery(\'#printable\').html(\'\');
var newHTML = jQuery(\'<div></div>\');
//var container = ;
newHTML.load(url + \'#pos_receipt\', function () {
newHTML.find(\'title, meta\').remove();
jQuery(\'#printable\').append(newHTML.html());
if (jQuery(\'#print_barcode img\').length) {
var src = jQuery(\'#print_barcode img\').attr(\'src\');
if (src != \'\') {
jQuery("<img>").load(function () {
window.print();
closeModal(\'modal-printing-receipt\');
wp.heartbeat.connectNow();
if (change_user && typeof APP_auth_show != \'undefined\') {
APP.user_logout();
}
}).attr(\'src\', src);
} else {
window.print();
closeModal(\'modal-printing-receipt\');
wp.heartbeat.connectNow();
if (change_user && typeof APP_auth_show != \'undefined\') {
APP.user_logout();
}
}
}
else if (jQuery(\'#print_receipt_logo\').length) {
var src = jQuery(\'#print_receipt_logo\').attr(\'src\');
if (src != \'\') {
jQuery("<img>").load(function () {
window.print();
closeModal(\'modal-printing-receipt\');
wp.heartbeat.connectNow();
if (change_user && typeof APP_auth_show != \'undefined\') {
APP.user_logout();
}
}).attr(\'src\', src);
} else {
window.print();
closeModal(\'modal-printing-receipt\');
wp.heartbeat.connectNow();
if (change_user && typeof APP_auth_show != \'undefined\') {
APP.user_logout();
}
}
}
else {
window.print();
closeModal(\'modal-printing-receipt\');
wp.heartbeat.connectNow();
if (change_user && typeof APP_auth_show != \'undefined\') {
APP.user_logout();
}
}
});
}
你会注意到有5扇窗户。打印();命令在那里。我发现一些成功的方法是,将承诺链的打印命令切换到QZ托盘上,就像他们在wiki中所展示的那样。
我为每个窗口替换的命令。print()位于下面。我们的目标是一次将两张单独的收据发送给两台单独的打印机,因此这条长链几乎是重复的:
qz.websocket.connect().then(function() { // Start of WriteDrive Modification - 1 of 5
return qz.printers.find("Star"); // Pass the Star printer name into the next Promise
}).then(function(printer) {
var config = qz.configs.create(printer); // Create a default config for the found printer
var colA = \'<h2>* Star HTML Printing *</h2>\' +
\'<span style="color: #F00;">Find, Config & Print in 1 Function</span><br/>\' +
\'<span style="color: #F00;">Testing for new POS</span>\';
var colB = \'<img src="https://www.domain.com/logo.png">\';
var printData = [
{
type: \'html\',
format: \'plain\',
data: \'<html>\' +
\' <table style="font-family: monospace; border: 1px;">\' +
\' <tr style="height: 6cm;">\' +
\' <td valign="top">\' + colA + \'</td>\' +
\' <td valign="top">\' + colB + \'</td>\' +
\' </tr>\' +
\' </table>\' +
\'</html>\'
}
]; // Raw HTML Output
return qz.print(config, printData);
}).then(function() {
return qz.printers.find("Boca"); // Pass the Boca printer name into the next Promise
}).then(function(printer2) {
var config = qz.configs.create(printer2); // Create a default config for the found printer
var colA = \'<h2>* Boca HTML Printing *</h2>\' +
\'<span style="color: #F00;">Find, Config & Print in 1 Function</span><br/>\' +
\'<span style="color: #F00;">Testing for new POS</span>\';
var colB = \'<img src="https://domain.com/logo2.png">\';
var printTicketData = [
{
type: \'html\',
format: \'plain\',
data: \'<html>\' +
\' <table style="font-family: monospace; border: 1px;">\' +
\' <tr style="height: 6cm;">\' +
\' <td valign="top">\' + colA + \'</td>\' +
\' <td valign="top">\' + colB + \'</td>\' +
\' </tr>\' +
\' </table>\' +
\'</html>\'
}
]; // Raw HTML Output
return qz.print(config, printTicketData);
}).then(function () {qz.websocket.disconnect()}).catch(function(e) { console.error(e); });
所有这些测试都很好-在交换打印时。window()命令用于QZ链,测试html打印到每个打印机。
问题首先,我对这里的设置感到困惑。从初始posPrintReceipt函数中,我是否尝试打印newHTML变量中的内容?还是别的什么?
第二,我不确定我是否在HTML中正确地布局了数据。到目前为止,我主要在其中一个打印链中尝试了以下内容:
function(printer) {
var config = qz.configs.create(printer); // Create a default config for the found printer
var printable = newHTML;
var printData = [
{
type: \'html\',
format: \'plain\',
data: \'<html>\' +
\' <table style="font-family: monospace; border: 1px;">\' +
\' <tr style="height: 6cm;">\' +
\' <td valign="top">\' + printable + \'</td>\' +
\' </tr>\' +
\' </table>\' +
\'</html>\'
}
]; // Raw HTML Output
return qz.print(config, printData);
将“printable”变量替换为一些想法。到目前为止,所有内容在最终打印中只打印出一行[对象对象]。我在该变量中加入了其他一些想法:
JSON.stringify(data); // \'data\' is from another function, seems to hold all of the order\'s data from WooCommerce
jQuery(\'#printable\');
jQuery(\'#pos_receipt\');
在将JQuery对象转换为可打印的HTML时,我是否缺少了一些简单的东西?或者在QZ托盘printData区域内有没有一种方法可以简单地从其内部的函数中提取信息?
任何帮助都将不胜感激。非常感谢。