[PDFE] Add select printer and print using the system dialog

This commit is contained in:
Alexey Koshelev
2025-03-27 15:31:46 +03:00
parent 2af7b91812
commit 40973dc4fe
2 changed files with 200 additions and 37 deletions

View File

@ -43,7 +43,11 @@ define([
this.adjPrintParams = new Asc.asc_CAdjustPrint();
this._state = {
lock_doc: false,
firstPrintPage: 0
firstPrintPage: 0,
pgorient: true,
isPrinterInfoLoad: false,
currentPrinter: null,
printersList: []
};
this._navigationPreview = {
@ -75,7 +79,8 @@ define([
onAfterRender: function(view) {
var me = this;
this.printSettings.menu.on('menu:hide', _.bind(this.onHidePrintMenu, this));
this.printSettings.btnPrint.on('click', _.bind(this.onBtnPrint, this, true));
this.printSettings.btnPrintSystemDialog.on('click', _.bind(this.onBtnPrint, this, true, true));
this.printSettings.btnPrint.on('click', _.bind(this.onBtnPrint, this, true, false));
this.printSettings.btnPrintPdf.on('click', _.bind(this.onBtnPrint, this, false));
this.printSettings.btnPrevPage.on('click', _.bind(this.onChangePreviewPage, this, false));
this.printSettings.btnNextPage.on('click', _.bind(this.onChangePreviewPage, this, true));
@ -129,6 +134,10 @@ define([
return me.txtPrintRangeInvalid;
};
if(this._state.isPrinterInfoLoad) {
this.printSettings.updateCmbPrinter(this._state.currentPrinter, this._state.printersList);
}
Common.NotificationCenter.on('window:resize', _.bind(function () {
if (this._isPreviewVisible) {
this.api.asc_drawPrintPreview(this._navigationPreview.currentPreviewPage);
@ -398,6 +407,15 @@ define([
Common.NotificationCenter.trigger('edit:complete');
},
setPrinterInfo: function(currentPrinter, list) {
this._state.isPrinterInfoLoad = true;
this._state.currentPrinter = currentPrinter;
this._state.printersList = list;
if(this.printSettings) {
this.printSettings.updateCmbPrinter(this._state.currentPrinter, this._state.printersList);
}
},
checkPageSize: function(width, height, left, right, top, bottom) {
var section = this.api.asc_GetSectionProps();
(width===undefined) && (width = parseFloat(section.get_W().toFixed(4)));
@ -512,7 +530,7 @@ define([
this.printSettings.btnNextPage.setDisabled(curPage > pageCount - 2);
},
onBtnPrint: function(print) {
onBtnPrint: function(print, useSystemDialog) {
this._isPrint = print;
if (this.printSettings.cmbRange.getValue()===-1 && this.printSettings.inputPages.checkValidate() !== true) {
this.printSettings.inputPages.focus();
@ -525,7 +543,10 @@ define([
this._state.firstPrintPage = this._navigationPreview.currentPage;
var size = this.api.asc_getPageSize(this._state.firstPrintPage);
var printerOption = this.printSettings.cmbPrinter.getSelectedRecord();
this.adjPrintParams.asc_setNativeOptions({
usesystemdialog: useSystemDialog,
printer: printerOption ? printerOption.value : null,
pages: this.printSettings.cmbRange.getValue()===-1 ? this.printSettings.inputPages.getValue() : this.printSettings.cmbRange.getValue(),
paperSize: {
w: size ? size['W'] : undefined,

View File

@ -2315,22 +2315,25 @@ define([], function () {
'<div class="main-header"><%= scope.txtPrint %></div>',
'<table style="width: 100%;">',
'<tbody>',
'<tr><td><label class="header"><%= scope.txtPrintRange %></label></td></tr>',
'<tr><td><label class="font-weight-bold"><%= scope.txtPrinter %></label></td></tr>',
'<tr><td class="padding-large"><div id="print-combo-printer" style="width: 248px;"></div></td></tr>',
'<tr><td><label class="font-weight-bold"><%= scope.txtPrintRange %></label></td></tr>',
'<tr><td class="padding-large"><div id="print-combo-range" style="width: 248px;"></div></td></tr>',
'<tr><td class="padding-large">',
'<table style="width: 100%;"><tbody>',
'<tr><td class="padding-large"><%= scope.txtPages %>:</td><td class="padding-large" style="width: 100%;"><div id="print-txt-pages" style="width: 100%;"></div></td></tr>',
'<tr><td><%= scope.txtCopies %>:</td><td style="width: 100%;"><div id="print-txt-copies" style="width: 60px;"></div></td></tr>',
'<tr><td class="padding-large"><%= scope.txtPages %>:</td><td class="padding-large" style="width: 100%;"><div id="print-txt-pages" class="padding-left-5" style="width: 100%;"></div></td></tr>',
'<tr><td><%= scope.txtCopies %>:</td><td style="width: 100%;"><div id="print-txt-copies" class="padding-left-5" style="width: 60px;"></div></td></tr>',
'</tbody></table>',
'</td></tr>',
'<tr><td><label class="header"><%= scope.txtPrintSides %></label></td></tr>',
'<tr><td><label class="font-weight-bold"><%= scope.txtPrintSides %></label></td></tr>',
'<tr><td class="padding-large"><div id="print-combo-sides" style="width: 248px;"></div></td></tr>',
'<tr><td><label class="header"><%= scope.txtPageSize %></label></td></tr>',
'<tr><td><label class="font-weight-bold"><%= scope.txtPageSize %></label></td></tr>',
'<tr><td class="padding-large"><div id="print-combo-pages" style="width: 248px;"></div></td></tr>',
'<tr><td><label class="header"><%= scope.txtPageOrientation %></label></td></tr>',
'<tr><td><label class="font-weight-bold"><%= scope.txtPageOrientation %></label></td></tr>',
'<tr><td class="padding-large"><div id="print-combo-orient" style="width: 150px;"></div></td></tr>',
'<tr><td><label class="header"><%= scope.txtMargins %></label></td></tr>',
'<tr><td><label class="font-weight-bold"><%= scope.txtMargins %></label></td></tr>',
'<tr><td class="padding-large"><div id="print-combo-margins" style="width: 248px;"></div></td></tr>',
'<tr><td class="padding-large"><label class="link" id="print-btn-system-dialog" data-hint="2" data-hint-direction="bottom" data-hint-offset="medium"><%= scope.txtPrintUsingSystemDialog %></label></td></tr>',
'<tr class="fms-btn-apply"><td>',
'<div class="footer justify">',
'<button id="print-btn-print" class="btn normal dlg-btn primary margin-right-8" result="print" style="width: 96px;" data-hint="2" data-hint-direction="bottom" data-hint-offset="big"><%= scope.txtPrint %></button>',
@ -2367,6 +2370,26 @@ define([], function () {
this.menu = options.menu;
this._defaultPaperSizeList = [
{ value: 0, displayValue: ['US Letter', '21,59', '27,94', 'cm'], caption: 'US Letter', size: [215.9, 279.4]},
{ value: 1, displayValue: ['US Legal', '21,59', '35,56', 'cm'], caption: 'US Legal', size: [215.9, 355.6]},
{ value: 2, displayValue: ['A4', '21', '29,7', 'cm'], caption: 'A4', size: [210, 297]},
{ value: 3, displayValue: ['A5', '14,8', '21', 'cm'], caption: 'A5', size: [148, 210]},
{ value: 4, displayValue: ['B5', '17,6', '25', 'cm'], caption: 'B5', size: [176, 250]},
{ value: 5, displayValue: ['Envelope #10', '10,48', '24,13', 'cm'], caption: 'Envelope #10', size: [104.8, 241.3]},
{ value: 6, displayValue: ['Envelope DL', '11', '22', 'cm'], caption: 'Envelope DL', size: [110, 220]},
{ value: 7, displayValue: ['Tabloid', '27,94', '43,18', 'cm'], caption: 'Tabloid', size: [279.4, 431.8]},
{ value: 8, displayValue: ['A3', '29,7', '42', 'cm'], caption: 'A3', size: [297, 420]},
{ value: 9, displayValue: ['Tabloid Oversize', '29,69', '45,72', 'cm'], caption: 'Tabloid Oversize', size: [296.9, 457.2]},
{ value: 10, displayValue: ['ROC 16K', '19,68', '27,3', 'cm'], caption: 'ROC 16K', size: [196.8, 273]},
{ value: 11, displayValue: ['Envelope Choukei 3', '12', '23,5', 'cm'], caption: 'Envelope Choukei 3', size: [120, 235]},
{ value: 12, displayValue: ['Super B/A3', '30,5', '48,7', 'cm'], caption: 'Super B/A3', size: [305, 487]},
{ value: 13, displayValue: ['A4', '84,1', '118,9', 'cm'], caption: 'A0', size: [841, 1189]},
{ value: 14, displayValue: ['A4', '59,4', '84,1', 'cm'], caption: 'A1', size: [594, 841]},
{ value: 16, displayValue: ['A4', '42', '59,4', 'cm'], caption: 'A2', size: [420, 594]},
{ value: 17, displayValue: ['A4', '10,5', '14,8', 'cm'], caption: 'A6', size: [105, 148]}
];
this._initSettings = true;
},
@ -2375,6 +2398,29 @@ define([], function () {
var $markup = $(this.template({scope: this, isRTL: Common.UI.isRTL()}));
this.cmbPrinter = new Common.UI.ComboBox({
el: $markup.findById('#print-combo-printer'),
menuStyle: 'min-width: 248px;max-height: 280px;',
editable: false,
takeFocusOnClose: true,
cls: 'input-group-nr',
placeHolder: this.txtPrinterNotSelected,
itemsTemplate: _.template([
'<% if (items.length > 0) { %>',
'<% _.each(items, function(item) { %>',
'<li id="<%= item.id %>" data-value="<%= item.value %>"><a tabindex="-1" type="menuitem" <% if (typeof(item.checked) !== "undefined" && item.checked) { %> class="checked" <% } %> ><%= scope.getDisplayValue(item) %></a></li>',
'<% }); %>',
'<% } else { %>',
'<li><a style="background:none; cursor: default;" onclick="event.stopPropagation();">' + this.txtPrintersNotFound + '</a></li>',
'<% } %>'
].join('')),
data: [],
dataHint: '2',
dataHintDirection: 'bottom',
dataHintOffset: 'big'
});
this.cmbPrinter.on('selected', _.bind(this.onPrinterSelected, this));
this.cmbRange = new Common.UI.ComboBox({
el: $markup.findById('#print-combo-range'),
menuStyle: 'min-width: 248px;max-height: 280px;',
@ -2424,11 +2470,7 @@ define([], function () {
editable: false,
takeFocusOnClose: true,
cls : 'input-group-nr',
data : [
{ value: 'one', displayValue: this.txtOneSide, descValue: this.txtOneSideDesc },
{ value: 'both-long', displayValue: this.txtBothSides, descValue: this.txtBothSidesLongDesc },
{ value: 'both-short', displayValue: this.txtBothSides, descValue: this.txtBothSidesShortDesc }
],
data : [],
itemsTemplate: _.template([
'<% _.each(items, function(item) { %>',
'<li id="<%= item.id %>" data-value="<%- item.value %>"><a tabindex="-1" type="menuitem" style ="display: flex; flex-direction: column;">',
@ -2488,26 +2530,7 @@ define([], function () {
takeFocusOnClose: true,
template: paperSizeTemplate,
itemsTemplate: paperSizeItemsTemplate,
data: [
{ value: 0, displayValue: ['US Letter', '21,59', '27,94', 'cm'], caption: 'US Letter', size: [215.9, 279.4]},
{ value: 1, displayValue: ['US Legal', '21,59', '35,56', 'cm'], caption: 'US Legal', size: [215.9, 355.6]},
{ value: 2, displayValue: ['A4', '21', '29,7', 'cm'], caption: 'A4', size: [210, 297]},
{ value: 3, displayValue: ['A5', '14,8', '21', 'cm'], caption: 'A5', size: [148, 210]},
{ value: 4, displayValue: ['B5', '17,6', '25', 'cm'], caption: 'B5', size: [176, 250]},
{ value: 5, displayValue: ['Envelope #10', '10,48', '24,13', 'cm'], caption: 'Envelope #10', size: [104.8, 241.3]},
{ value: 6, displayValue: ['Envelope DL', '11', '22', 'cm'], caption: 'Envelope DL', size: [110, 220]},
{ value: 7, displayValue: ['Tabloid', '27,94', '43,18', 'cm'], caption: 'Tabloid', size: [279.4, 431.8]},
{ value: 8, displayValue: ['A3', '29,7', '42', 'cm'], caption: 'A3', size: [297, 420]},
{ value: 9, displayValue: ['Tabloid Oversize', '29,69', '45,72', 'cm'], caption: 'Tabloid Oversize', size: [296.9, 457.2]},
{ value: 10, displayValue: ['ROC 16K', '19,68', '27,3', 'cm'], caption: 'ROC 16K', size: [196.8, 273]},
{ value: 11, displayValue: ['Envelope Choukei 3', '12', '23,5', 'cm'], caption: 'Envelope Choukei 3', size: [120, 235]},
{ value: 12, displayValue: ['Super B/A3', '30,5', '48,7', 'cm'], caption: 'Super B/A3', size: [305, 487]},
{ value: 13, displayValue: ['A4', '84,1', '118,9', 'cm'], caption: 'A0', size: [841, 1189]},
{ value: 14, displayValue: ['A4', '59,4', '84,1', 'cm'], caption: 'A1', size: [594, 841]},
{ value: 16, displayValue: ['A4', '42', '59,4', 'cm'], caption: 'A2', size: [420, 594]},
{ value: 17, displayValue: ['A4', '10,5', '14,8', 'cm'], caption: 'A6', size: [105, 148]},
{ value: -1, displayValue: this.txtCustom, caption: this.txtCustom, size: []}
],
data: [],
dataHint: '2',
dataHintDirection: 'bottom',
dataHintOffset: 'big',
@ -2581,8 +2604,10 @@ define([], function () {
this.pnlTable = $(this.pnlSettings.find('table')[0]);
this.trApply = $markup.find('.fms-btn-apply');
this.btnPrintSystemDialog = $markup.findById('#print-btn-system-dialog');
this.btnPrint = new Common.UI.Button({
el: $markup.findById('#print-btn-print')
el: $markup.findById('#print-btn-print'),
disabled: true
});
this.btnPrintPdf = new Common.UI.Button({
el: $markup.findById('#print-btn-print-pdf')
@ -2658,10 +2683,113 @@ define([], function () {
this.updateMetricUnit();
this._initSettings = false;
}
this.updateSettings();
this.updateScroller();
this.fireEvent('show', this);
},
updateSettings: function() {
var selectedPrinter = this.cmbPrinter.getSelectedRecord();
this.setCmbSidesOptions(selectedPrinter ? selectedPrinter.isDuplexSupported : true);
this.setCmbPaperSizeOptions(selectedPrinter ? selectedPrinter.paperSupported : null);
},
updateCmbPrinter: function(currentPrinter, printers) {
var cmbPrinterOptions = [];
printers = printers || [];
//If the current printer is not in the list of printers, add it
if(currentPrinter && !_.some(printers, function(printer) { return printer.name == currentPrinter })) {
printers.push({
name: currentPrinter,
duplex_supported: true,
paperSupported: this._defaultPaperSizeList
});
}
cmbPrinterOptions = printers.map(function(printer) {
return {
value: printer.name,
displayValue: printer.name,
paperSupported: printer.paper_supported,
isDuplexSupported: printer.duplex_supported
}
});
this.cmbPrinter.setData(cmbPrinterOptions);
this.cmbPrinter.setValue(currentPrinter);
this.cmbPrinter.trigger('selected', this, this.cmbPrinter.getSelectedRecord());
},
setCmbSidesOptions: function(isDuplexSupported) {
var cmbValue = this.cmbSides.getValue();
var list = [{ value: 'one', displayValue: this.txtOneSide, descValue: this.txtOneSideDesc }];
if(isDuplexSupported) {
list.push(
{ value: 'both-long', displayValue: this.txtBothSides, descValue: this.txtBothSidesLongDesc },
{ value: 'both-short', displayValue: this.txtBothSides, descValue: this.txtBothSidesShortDesc }
);
} else if(cmbValue != 'one') {
cmbValue = 'one';
}
this.cmbSides.setData(list);
this.cmbSides.setValue(cmbValue);
},
setCmbPaperSizeOptions(paperSizeList) {
var resultList = [];
if(paperSizeList && paperSizeList.length > 0) {
resultList = paperSizeList.map(function(item, index) {
return {
value: index,
displayValue: [item.name, item.width / 10, item.height / 10, 'cm'],
caption: item.name,
size: [item.width, item.height]
}
});
} else {
resultList = [].concat(this._defaultPaperSizeList);
}
resultList.push({ value: -1, displayValue: this.txtCustom, caption: this.txtCustom, size: []});
var prevSelectedOption = this.cmbPaperSize.store.findWhere({
value: this.cmbPaperSize.getValue()
});
var newSelectedOption = null;
function findOptionBySize(list, width, height) {
return _.find(list, function(option) {
return Math.abs(option.size[0] - width) < 0.1 && Math.abs(option.size[1] - height) < 0.1;
});
}
// If a previously selected option exists, search for a matching one in resultList
if (prevSelectedOption) {
newSelectedOption = findOptionBySize(
resultList,
Math.round(prevSelectedOption.get('size')[0]),
Math.round(prevSelectedOption.get('size')[1])
);
}
// If no matching option is found, look for the default size 210x297 (A4)
if (!newSelectedOption) {
newSelectedOption = findOptionBySize(resultList, 210, 297);
}
if (!newSelectedOption && resultList[0]) {
newSelectedOption = resultList[0];
}
this.cmbPaperSize.setData(resultList);
this.updatePaperSizeMetricUnit();
this.cmbPaperSize.setValue(newSelectedOption ? newSelectedOption.value : null);
if(!this.cmbPaperSize.isDisabled){
this.cmbPaperSize.trigger('selected', this, this.cmbPaperSize.getSelectedRecord());
}
},
updateScroller: function() {
if (this.scroller) {
Common.UI.Menu.Manager.hideAll();
@ -2679,7 +2807,18 @@ define([], function () {
},
onPrinterSelected: function(combo, record) {
this.setCmbSidesOptions(record ? record.isDuplexSupported : true);
this.setCmbPaperSizeOptions(record ? record.paperSupported : null);
this.btnPrint.setDisabled(!record);
},
updateMetricUnit: function() {
this.updatePaperSizeMetricUnit();
this.cmbPaperMargins.onResetItems();
},
updatePaperSizeMetricUnit: function() {
if (!this.cmbPaperSize) return;
var store = this.cmbPaperSize.store;
for (var i=0; i<store.length-1; i++) {
@ -2694,7 +2833,6 @@ define([], function () {
Common.Utils.Metric.getCurrentMetricName()]);
}
this.cmbPaperSize.onResetItems();
this.cmbPaperMargins.onResetItems();
},
isVisible: function() {
@ -2722,6 +2860,10 @@ define([], function () {
txtPrint: 'Print',
txtPrintPdf: 'Print to PDF',
txtPrinter: 'Printer',
txtPrinterNotSelected: 'Printer not selected',
txtPrintersNotFound: 'Printers not found',
txtPrintUsingSystemDialog: 'Print using the system dialog',
txtPrintRange: 'Print range',
txtCurrentPage: 'Current page',
txtAllPages: 'All pages',