Davide Cavallini
Davide Cavallini

Reputation: 234

Is there a way to print directly from Chrome?

I found this selector:

document.querySelector('#print-header > div > button.print.default').click();

Is there a method with this selector to print directly from Chrome?

I tried with timeout too, but isn't working.

Upvotes: 1

Views: 3491

Answers (2)

user2226755
user2226755

Reputation: 13223

(This solution needs user's confirmation)

Use window.print(); function or see this answer : Bill Paetzke's answser of Print the contents of a DIV.

function PrintElem(elem)
{
    Popup($(elem).html());
}

function Popup(data) 
{
    var mywindow = window.open('', 'my div', 'height=400,width=600');
    mywindow.document.write('<html><head><title>my div</title>');
    /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
    mywindow.document.write('</head><body >');
    mywindow.document.write(data);
    mywindow.document.write('</body></html>');

    mywindow.document.close(); // necessary for IE >= 10
    mywindow.focus(); // necessary for IE >= 10

    mywindow.print();
    mywindow.close();

    return true;
}

Upvotes: 2

Tiberiu Popescu
Tiberiu Popescu

Reputation: 4524

You cannot print without user confirmation. It's a security thing, you are not allowed to control user PC from the browser.

When you open the print dialog in chrome it's like a new Tab, you have no control there.

Upvotes: 1

Related Questions