Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adjust print options through code?

I have a ticket booking system in Joomla.

When user clicks on ticket link a ticket is shown on the site.

I am using a barcode.php file to generate the barcode image for ticket number.

Now there are 2 scenarios I used to print ticket.

  1. When I print that ticket using window.print() or Ctrl+P, 2 pages get printed even though my ticket content is only single page.

  2. When I use following javascript code to print specific part of the page, barcode image is not generated.

    function print_specific_div_content(){
        var content = "<html>";
        content += document.getElementById("divToPrint").innerHTML ;
        content += "</body>";
        content += "</html>";
    
        var printWin = window.open('','','left=0,top=0,width=552,height=477,toolbar=0,scrollbars=0,status =0');
        printWin.document.write(content);
        printWin.document.close();
        printWin.focus();
        printWin.print();
        printWin.close();
    }
    

My requirements are:

Ticket should be printed only on single page.

Any help or suggestion will be appreciated.

Thanks.

EDIT 1 :

I modified my function as follow, but unfortunately it shows new window but no print dialog. :(

function print_specific_div_content(){
    var win = window.open('','','left=0,top=0,width=552,height=477,toolbar=0,scrollbars=0,status =0');
    var handler = function() {
      win.print();
      win.close();
    };
    if(win.addEventListener)
        win.addEventListener('load', handler, false);
    else if(win.attachEvent)
        win.attachEvent('onload', handler, false);

    var content = "<html>";
    content += document.getElementById("divToPrint").innerHTML ;
    content += "</body>";
    content += "</html>";
    win.document.write(content);
    win.document.close();
}
like image 246
Amol Chakane Avatar asked Oct 22 '25 09:10

Amol Chakane


1 Answers

When printing just the content, you need to wait for the document to load before printing so that the images will be loaded:

function print_specific_div_content(){
    var win = window.open('','','left=0,top=0,width=552,height=477,toolbar=0,scrollbars=0,status =0');

    var content = "<html>";
    content += "<body onload=\"window.print(); window.close();\">";
    content += document.getElementById("divToPrint").innerHTML ;
    content += "</body>";
    content += "</html>";
    win.document.write(content);
    win.document.close();
}
like image 56
Dark Falcon Avatar answered Oct 25 '25 00:10

Dark Falcon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!