问题
I have a requirement that by clicking on a button, the printer dialog will open with the number of copies set to 2 instead of the default number 1.
回答1:
No it is not possible using pure javascript with window.print().
However, if you are open to using Java Applet, take a look at qz-print aka jzebra. I have personally used this in some past projects for some advance printing requirements and the result is very satisfying. Take a look at their printHTML() method
***************************************************************************
* Prototype function for printing plain HTML 1.0 to a PostScript capable
* printer. Not to be used in combination with raw printers.
* Usage:
* qz.appendHTML('<h1>Hello world!</h1>');
* qz.printPS();
***************************************************************************/
function printHTML() {
if (notReady()) { return; }
// Preserve formatting for white spaces, etc.
var colA = fixHTML('<h2>* QZ Print Plugin HTML Printing *</h2>');
colA = colA + '<color=red>Version:</color> ' + qz.getVersion() + '<br />';
colA = colA + '<color=red>Visit:</color> http://code.google.com/p/jzebra';
// HTML image
var colB = '<img src="' + getPath() + 'img/image_sample.png">';
//qz.setCopies(3);
qz.setCopies(parseInt(document.getElementById("copies").value));
// Append our image (only one image can be appended per print)
qz.appendHTML('<html><table face="monospace" border="1px"><tr height="6cm">' +
'<td valign="top">' + colA + '</td>' +
'<td valign="top">' + colB + '</td>' +
'</tr></table></html>');
qz.printHTML();
}
Of course if you just want to print 2 copies, this may be way too over killed and too complicated to implement. But I'm not aware of any other way that can help you interfere with the browser's printing.
来源:https://stackoverflow.com/questions/29815389/window-print-sets-default-copies-to-2