Click here to Skip to main content
15,908,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to print a portion of webpage. I'm using the following javascript function to print:

XML
<script language="javascript">
         function PrintPartOfTheWebPage()
         {   
            var prtContent = document.getElementById("print");
            var windowUrl = 'about:blank';
             
            var WinPrint =  window.open('','','letf=0,top=0,width=212,height=337,location=top,scrollbars=0,status=0');
            WinPrint.document.write(prtContent.innerHTML);
            WinPrint.document.close();
            WinPrint.focus();
            WinPrint.print();
            WinPrint.close();
            prtContent.innerHTML = "";
          }
        </script>



I want to print it in the top left corner without any margins or header&footer. Also, while printing background color is not coming in the printout.

So can I convert the div portion into an image or pdf format and send to printer?
How can I do it?

Thank you.
Posted
Comments
Karwa_Vivek 12-Sep-12 2:07am    
Why dont you Use CSS to Achieve this

 
Share this answer
 
try like this
function PrintContent() {
    var DocumentContainer = document.getElementById('divPrint');
    var WindowObject = window.open('', 'PrintWindow', 'width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes');
    var strHtml = "<html>\n<head>\n<link rel=\"stylesheet\" type=\"text/css\"  href=\"print.css\">\n</head><body><h4>\n<div style=\"testStyle \">\n" + DocumentContainer.innerHTML + "\n</div>\n</body>\n</html>";
    WindowObject.document.writeln(strHtml);
    WindowObject.document.close();
    WindowObject.focus();
    //WindowObject.print();
    // WindowObject.close();
    }

where print.css is the stylesheet containing your style for the page.Modify your stylesheet accordingly
Try this
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900