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.
When I print that ticket using
window.print()
or Ctrl+P, 2 pages get printed even though my ticket content is only single page.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();
}
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.
When I print that ticket using
window.print()
or Ctrl+P, 2 pages get printed even though my ticket content is only single page.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();
}
Share
Improve this question
edited Sep 26, 2013 at 13:58
Amol Chakane
asked Sep 26, 2013 at 12:33
Amol ChakaneAmol Chakane
1,5112 gold badges22 silver badges44 bronze badges
1 Answer
Reset to default 10When 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();
}