locked
Scaling printer friendly SP2007 page viewer web parts in IE7/8 RRS feed

  • Question

  • I have been searching the internet in vain for a solution to this simple problem. I am printing the contents of a webpart (a web page in the web page viewer webpart) and have been experiencing the IE scaletofit problem where printouts (and in the print preview window) have scroll bars. This can be overcome by changing the scale manually. I have tested this on IE7 on XPSP3 and IE8 on Windows 7 and the problem exists on both.

    I have reused some code to take the contents of the webpart and display it in a new window. Does anyone have any suggestions on how I can get this to work?

    Thanks in advance!

    Code for the print button:

    <INPUT onclick=javascript:void(PrintWebPart()) value="Print Page" type=button>
    <SCRIPT language=JavaScript>
    //Controls which Web Part or zone to print
    var WebPartElementID = "WebPartWPQ3";

    //Function to print Web Part
    function PrintWebPart()
    {
     var bolWebPartFound = false;
     if (document.getElementById != null)
     {
      //Create html to print in new window
      var PrintingHTML = '<HTML>\n<HEAD>\n';
      //Take data from Head Tag
      if (document.getElementsByTagName != null)
       {
       var HeadData= document.getElementsByTagName("HEAD");
       if (HeadData.length > 0)
        PrintingHTML += HeadData[0].innerHTML;
       }
      PrintingHTML += '\n</HEAD>\n<BODY>\n';
      var WebPartData = document.getElementById(WebPartElementID);
      if (WebPartData != null)
      {
       PrintingHTML += WebPartData.innerHTML;
       bolWebPartFound = true;
      }
      else
      {
       bolWebPartFound = false;
       alert ('Cannot Find Web Part');
      }
     }
     PrintingHTML += '\n</BODY>\n</HTML>';
     //Open new window to print
     if (bolWebPartFound)
     {
    var PrintingWindow = window.open("","PrintWebPart", "toolbar,width=1024,height=768,toolbar=no,directories=no,status=nomenubar=no,scrollbars=no,resizable=no");
      // PrintingWindow.document.open();
      PrintingWindow.document.write(PrintingHTML);
      // Open Print Window
      PrintingWindow.print();
     }
    }
    </SCRIPT>


    Thursday, August 18, 2011 4:13 PM

Answers

  • Ok glad you said that, this works for me in IE

    <script type='text/javascript'>
    
    function PrintPage()
    {
    var iframeSrc = $("#MSOPageViewerWebPart_WebPartWPQ4").attr("src");
    var popup = window.open(iframeSrc,"popup");
    popup.document.close();
    if(!popup.focus())
    {
     popup.focus();
    }
    popup.print();
    }
    
    </script>
    
    
    
    <input type=button value='Print' onClick='javascript:PrintPage();'/>
    
    
    




    -tom daly
    • Marked as answer by randallc1 Thursday, August 25, 2011 8:36 AM
    Tuesday, August 23, 2011 5:56 PM
  • You could host the js file in any document library. Or you could use a content deployment network, this would link to a jquery library external to your environment.

    http://docs.jquery.com/Downloading_jQuery#CDN_Hosted_jQuery


    -tom daly
    • Marked as answer by randallc1 Thursday, August 25, 2011 8:36 AM
    Wednesday, August 24, 2011 3:37 PM
  • This can go in the Masterpage or in a Content Editor Webpart. You must have your refrence to the jQuery library already on the page as its needed for that code.

    and change this: var iframeSrc = $("#MSOPageViewerWebPart_WebPartWPQ4").attr("src");

    more specifically:  MSOPageViewerWebPart_WebPartWPQ4

    to the ID of the iframe element that you're looking to print. Use the IE DEV / Firebug toolbars to determine what the ID is of the iframe.

    As far as I know this would only work with pages / sites in the same domain.


    -tom daly
    • Marked as answer by randallc1 Thursday, August 25, 2011 8:36 AM
    Tuesday, August 23, 2011 7:02 PM

All replies

  • *BUMP*

    Over 100 views but no suggestions.

    Monday, August 22, 2011 2:24 PM
  • The only solution that I have found is to use document.execCommand('print', false, null) instead which prints the whole page but not the web part.
    Monday, August 22, 2011 2:50 PM
  • I was looking into some methods to do this but just wondering, does the content inside your iframe belong in the same domain or are you print some external website?

     


    -tom daly
    Tuesday, August 23, 2011 3:06 AM
  • Yes the content is hosted in a document library on the SP site collection - they are HTML pages.
    Tuesday, August 23, 2011 6:51 AM
  • Ok glad you said that, this works for me in IE

    <script type='text/javascript'>
    
    function PrintPage()
    {
    var iframeSrc = $("#MSOPageViewerWebPart_WebPartWPQ4").attr("src");
    var popup = window.open(iframeSrc,"popup");
    popup.document.close();
    if(!popup.focus())
    {
     popup.focus();
    }
    popup.print();
    }
    
    </script>
    
    
    
    <input type=button value='Print' onClick='javascript:PrintPage();'/>
    
    
    




    -tom daly
    • Marked as answer by randallc1 Thursday, August 25, 2011 8:36 AM
    Tuesday, August 23, 2011 5:56 PM
  • Thanks Tom.

    I'm sure others like me have been dealing with this issue too.

    Where does the script go? Are any customizations needed?

    Best wishes,

    Eric

    Tuesday, August 23, 2011 6:58 PM
  • This can go in the Masterpage or in a Content Editor Webpart. You must have your refrence to the jQuery library already on the page as its needed for that code.

    and change this: var iframeSrc = $("#MSOPageViewerWebPart_WebPartWPQ4").attr("src");

    more specifically:  MSOPageViewerWebPart_WebPartWPQ4

    to the ID of the iframe element that you're looking to print. Use the IE DEV / Firebug toolbars to determine what the ID is of the iframe.

    As far as I know this would only work with pages / sites in the same domain.


    -tom daly
    • Marked as answer by randallc1 Thursday, August 25, 2011 8:36 AM
    Tuesday, August 23, 2011 7:02 PM
  • Thanks Tom.

    I have tried to get the code to work (I've double checked the Page Viewer Web Part Name is correct) but i'm coming up with object expected error on this line -

    var iframeSrc = $("MSOPageViewerWebPart_WebPartWPQ3").attr("src");

     

     

     

     

    Wednesday, August 24, 2011 8:59 AM
  • Oh, that usually means you don't have the jQuery library. the $ = jQuery so it won't work without the base library.

    I guess I forgot to mention that above all that code you have to have <script type='text/javascript' src='/path to jquery library'></script>

    Go to the jQuery site and download the latest minimal library for production and post that on your site and link to that.


    -tom daly
    Wednesday, August 24, 2011 3:17 PM
  • Ahh I see.

    It's going to be hard to get this put on the production environment here unfortunately as my client has 1000s of users and is currently looking to migrate to SP2010.

    I will suggest this is looked at when they migrate next year.

    I'll give it a go on my home instance though :)

    Wednesday, August 24, 2011 3:34 PM
  • You could host the js file in any document library. Or you could use a content deployment network, this would link to a jquery library external to your environment.

    http://docs.jquery.com/Downloading_jQuery#CDN_Hosted_jQuery


    -tom daly
    • Marked as answer by randallc1 Thursday, August 25, 2011 8:36 AM
    Wednesday, August 24, 2011 3:37 PM
  • Managed to get it to work using the google hosted jquery lib. It's really quick at loading the page and bringing up the print dialogue box compared to the old code.

    Thanks a lot Tom!

    Thursday, August 25, 2011 8:36 AM