I am using a javascript code to print an image with some text as well. I have used a div area in my html page named ‘divPrintArea’. The content of that div will be printed through calling the javascript function.
Images are coming fine while printing from all the browsers except Chrome(i.e. latest versions of ie, firefox, opera, safari). But in case of Chrome it is missing sometimes.
My second issue is I couldn’t set the default page layout to ‘Landscape’. Do you have any clue? Please give some solutions in detail.
TIA.
function printdata() {
var printHtml = "";
printHtml = printHtml + "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
printHtml = printHtml + "<html xmlns='http://www.w3.org/1999/xhtml'>";
printHtml = printHtml + "<head>";
printHtml = printHtml + "<meta http-equiv='cache-control' content='no-cache, must-revalidate'>";
printHtml = printHtml + "<meta http-equiv='refresh' content='0; http://www.MyWebsite.com/' />";
printHtml = printHtml + "<meta http-equiv='expires' content='-1'>";
printHtml = printHtml + "<meta http-equiv='pragma' content='no-cache, must-revalidate'>";
printHtml = printHtml + "<title>www.MyWebsite.com</title>";
printHtml = printHtml + "<style type=\"text/css\" media=\"print\">td{border:0px solid #000; padding:2px 5px;}";
printHtml = printHtml + "body{background:#fff;";
printHtml = printHtml + "width='100%';";
printHtml = printHtml + "height='100%';";
printHtml = printHtml + "filter:progid:DXImageTransform.Microsoft.BasicImage(Rotation=3);";
printHtml = printHtml + "}";
printHtml = printHtml + "</style></head>";
printHtml = printHtml + "<body>";
printHtml = printHtml + "<div style=\"text-align:left; border-bottom:1px dashed #2D86C5; border-top:1px dashed #2D86C5; font-family:'GillSansMTRegular', Arial;\">";
printHtml = printHtml + document.getElementById("divPrintArea").innerHTML;
printHtml = printHtml + "</div>";
printHtml = printHtml + "</body>";
printHtml = printHtml + "</html>";
win = window.open("http://www.MyWebsite.com/", "My");
win.document.write(printHtml);
win.document.close();
win.focus();
win.onload = win.print();
win.close();
return false;
}
There’re problems in your code:
This is only supported by IE, and you should use CSS3’s
transforminstead.We do not use
=sign in CSS. We use:. Maybe this is the cause of the problem.Sidenotes
Well, in your code, you should not really do all this thing. You should use
createElement, and change all theprintHtml = printHtml +toAlso, using
document.writeis outdated. UseappendChildinstead. [MDN]