Wednesday, November 5, 2008

EGL: Edit page details while printing web page

What you do when you want to print a web page? Answer is obvious; use window.print() event. What you do when you want to print a web page which requires formatting like removing some text, page break, modifying some content etc. just before sending to printer? Answer could be you will create one more page and make the changes required for printing with window.print() event on page load function. Then forward to this page on clicking print button. Is there any other better solution? Remember the original page could be the result of lot of business which need to take care even when you are forwarding to other page taking up printing responsibility.
Well in that case I would prefer to advise you to use javascript and css. This approach is not only fast but also easy and accurate.
Now how to do that. Well answer is right below:


  1. Surround the portion with a div in the jsp page you want to print . Donot think about the changes now.

  2. Call a javascript function say printPage(divPrintId). divPrintId is the id of the div.

  3. Retrieve the entire text within the div as str=document.getElementById(divPrintId).innerHTML

  4. Now do the business changes on the innerHTML text.

  5. For page break you can create a javascript inside printPage javascript like below:
    newwin.document.write('pageBreakDetails();\n')
    newwin.document.write('function pageBreakDetails(){\n')
    newwin.document.write('var details = document.getElementById("form1:tableId").style.pageBreakBefore="always";\n ')
    newwin.document.write('}\n')

Let me know if someone finds any difficulty implementing this


Regards


Monu

0 comments: