Hoang Vinh
Hoang Vinh

Reputation: 203

Automatic print pdf file on chrome or firefox (both lastest version) using javascript

I am doing a function to auto print the pdf file, the file is returned from the server. It is currently showing a print window, but I want the file to be printed on the printer at all times without have to press the print button.

current:

current

I read a lot of articles on stackoverflow, and other pages but it's failed.

Please help me! Thanks alot

Upvotes: 5

Views: 13280

Answers (1)

Hoang Vinh
Hoang Vinh

Reputation: 203

I have found the solution to the problem

In Chrome:

  • step 1:

config run chrome silent printing on mode --kiosk-printing edit target on chrome shortcut properties Ex:"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --kiosk-printing "http://localhost:8080/this"

  • step 2:

code

var iframe = document.createElement('iframe');
            document.body.appendChild(iframe);
            iframe.style.display = 'none';
            iframe.onload = function() {
                setTimeout(function() {
                    iframe.focus();
                    iframe.contentWindow.print();
                }, 0);
            };
            iframe.src = _blobUrl;

In Firefox:

  • step1: config printing silent

goto about:config create new Boolean preference name="print.always_print_silent" Value=true

  • step 2: code

    var myWindow = window.open(_blobUrl, '_blank', 'width=800,height=600'); myWindow.focus(); myWindow.print(); myWindow.close();

Upvotes: 6

Related Questions