neodynamic / JSPrintManager

Advanced Client-side Printing & Scanning Solution for Javascript
https://www.neodynamic.com/products/printing/js-print-manager/
260 stars 136 forks source link

Auto select printer and paper #57

Open marvinnq opened 1 year ago

marvinnq commented 1 year ago

Hi, i have a problem with auto select.

I use this sample code, and Please help me. How to select my printer (not first on list) after loda sprinter list? And next i need select Paper Size.

`

`

marvinnq commented 1 year ago
<script src="jsprint/JSPrintManager.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>

<div>
    <div>
        <input type="text" name="txtPdfFile" id="txtPdfFile" value="etykieta2.pdf" />
        <select name="lstPrinters" id="lstPrinters" onchange="showSelectedPrinterInfo();" ></select>
        <select name="lstPrinterPapers" id="lstPrinterPapers" ></select>
    </div>
</div>

<button type="button" onclick="print();">Print Now...</button>

<script>

    //Check JSPM WebSocket status
    function jspmWSStatus() {
        if (JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Open)
            return true;
        else if (JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Closed) {
            alert('JSPrintManager (JSPM) is not installed or not running! Download JSPM Client App from https://neodynamic.com/downloads/jspm');
            return false;
        }
        else if (JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Blocked) {
            alert('JSPM has blocked this website!');
            return false;
        }
    }

    var clientPrinters = null;
    var _this = this;

    //WebSocket settings
    JSPM.JSPrintManager.auto_reconnect = true;
    JSPM.JSPrintManager.start();
    JSPM.JSPrintManager.WS.onStatusChanged = function () {
        if (jspmWSStatus()) {
            //get client installed printers
            JSPM.JSPrintManager.getPrintersInfo().then(function (printersList) {
                clientPrinters = printersList;
                var options = '';
                for (var i = 0; i < clientPrinters.length; i++) {

                    options += '<option>' + clientPrinters[i].name + '</option>';

                }
                $('#lstPrinters').html(options);
                _this.showSelectedPrinterInfo();
            });
        }
    };

    function showSelectedPrinterInfo() {

        // get selected printer index
        var idx = $("#lstPrinters")[0].selectedIndex;
        // get supported papers
        options = '';
        for (var i = 0; i < clientPrinters[idx].papers.length; i++) {

            options += '<option>' + clientPrinters[idx].papers[i] + '</option>';

        }
        $('#lstPrinterPapers').html(options);

    }

    //Do printing...
    function print() {

        if (jspmWSStatus()) {

            //Create a ClientPrintJob
            var cpj = new JSPM.ClientPrintJob();

            //Set Printer info
            var myPrinter = new JSPM.InstalledPrinter($('#lstPrinters').val());
            myPrinter.paperName = $('#lstPrinterPapers').val();

            cpj.clientPrinter = myPrinter;

            //Set PDF file
            var my_file = new JSPM.PrintFilePDF($('#txtPdfFile').val(), JSPM.FileSourceType.URL, 'MyFile.pdf', 1);

            cpj.files.push(my_file);

            //Send print job to printer!
            cpj.sendToClient();

        }
    }

</script>