Mehdi Souregi
Mehdi Souregi

Reputation: 3265

Print on two different pages using javascript

I have two texts and i want to print them on two different pages, this is what i did so far :

The button print :

<button onclick="javascript : CallPrint('idText1','idText2')" type="button">Print</button>

callPrint JavaScript method :

<script language="javascript" type="text/javascript">
    function CallPrint(firstText,secondText) {
        var firstContent = document.getElementById(firstText);
        var secondContent = document.getElementById(secondText);
        var WinPrint = window.open('', '', 'letf=0,top=0,width=1200,height=750,toolbar=0,scrollbars=0,status=0,dir=ltr');
        WinPrint.document.write(firstContent.innerHTML);
        WinPrint.document.write(secondContent.innerHTML);
        WinPrint.document.close();
        WinPrint.focus();
        WinPrint.print();
        WinPrint.close();
    }
</script>

The problem is, they are printed one after the other. How is it possible to separate them on two different pages (without using br tags). If it is not possible, i wanna know if it is possible to show two print windows instead of one.

Upvotes: 1

Views: 328

Answers (2)

Lukasz Wiktor
Lukasz Wiktor

Reputation: 20412

Add an empty paragraph with page-break-after: always style between the 2 texts:

WinPrint.document.write(firstContent.innerHTML);
WinPrint.document.write('<p style="page-break-after:always;"></p>');
WinPrint.document.write(secondContent.innerHTML);

Upvotes: 1

samar
samar

Reputation: 188

You have to open two windows in the function in order to seperate the contents in two files.

function CallPrint(firstText,secondText) {
        var firstContent = document.getElementById(firstText);
        var secondContent = document.getElementById(secondText);
        var WinPrint = window.open('', '', 'letf=0,top=0,width=1200,height=750,toolbar=0,scrollbars=0,status=0,dir=ltr');
        WinPrint.document.write(firstContent.innerHTML);
        var WinPrint2 = window.open('', '', 'letf=0,top=0,width=1200,height=750,toolbar=0,scrollbars=0,status=0,dir=ltr');
        WinPrint2.document.write(secondContent.innerHTML);

Upvotes: 0

Related Questions