2010-11-17 114 views
3

我在SSRS 2005中有報告。我正在使用遠程報告。在IE中,打印按鈕顯示,但在Firefox和Chrome中,打印按鈕不顯示。Chrome和Firefox中的SSRS打印按鈕

我的報告顯示在jQuery UI對話框中,所以我不能只做一個window.print。我的報告在模態內渲染得很好。

我需要能夠發出一個打印命令給reportviewer,就像它從控件內部完成一樣,但只能在firefox和chrome中完成。

我挖掘了報表查看器的標記,並找到了這段代碼。我試圖手動將它注入到reportviewer中,但沒有成功。

<table id="reportViewer_ctl01_ctl07_ctl00_ctl00" onclick="document.getElementById(&#39;reportViewer&#39;).ClientController.LoadPrintControl();return false;" onmouseover="this.Controller.OnHover();" onmouseout="this.Controller.OnNormal();" title="Print" style="display:none;"> 
           <script type="text/javascript"> 
            document.getElementById('reportViewer_ctl01_ctl07_ctl00_ctl00').Controller = new ReportViewerHoverButton("reportViewer_ctl01_ctl07_ctl00_ctl00", false, "", "", "", "#ECE9D8", "#DDEEF7", "#99BBE2", "1px #ECE9D8 Solid", "1px #336699 Solid", "1px #336699 Solid"); 
           </script><tr> 
            <td><input type="image" name="reportViewer$ctl01$ctl07$ctl00$ctl00$ctl00" title="Print" src="/Reserved.ReportViewerWebControl.axd?OpType=Resource&amp;Version=9.0.30729.4402&amp;Name=Microsoft.Reporting.WebForms.Icons.Print.gif" alt="Print" style="height:16px;width:16px;padding:2px;" /></td> 
           </tr> 
          </table> 

任何想法?

+0

作爲一種現在的解決方法,我在我的模式上打開了一個按鈕,打開一個新的可打印版本的空窗口中的報告。我想直接從模式打印。 – Josh 2010-11-17 19:55:05

回答

3

不幸的是,IE以外的瀏覽器不支持打印按鈕。

我想你意識到這一點並做了一個解決方法,我們還沒有拿出一個體面的解決方案。雖然我們大多數用戶都喜歡直接從Excel打印,所以我們允許他們導出文件,然後進行打印。

這太問題將是一個很好的鏈接:

SQL Reporting Services - Print Button not shown in Mozilla

2

請找到具有打印圖標與Firefox和Chrome的打印功能的SSRS報告代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Report.aspx.cs" Inherits="DemoMVC.Report.Report" %> 


<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %> 


<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 

    <script src="../Scripts/jquery-1.7.1.js"></script> 

    <script type="text/javascript" lang="javascript"> 

     $(document).ready(function() { 

      if ($.browser.mozilla || $.browser.webkit) { 

       try { 

        showPrintButton(); 

       } 

       catch (e) { alert(e); } 

      } 

     }); 



     function showPrintButton() { 

      var table = $("table[title='Refresh']"); 

      var parentTable = $(table).parents('table'); 

      var parentDiv = $(parentTable).parents('div').parents('div').first(); 

      parentDiv.append('<input type="image" style="border-width: 0px; padding: 3px;margin-top:2px; height:16px; width: 16px;" alt="Print" src="/Reserved.ReportViewerWebControl.axd?OpType=Resource&amp;Version=9.0.30729.1&amp;Name=Microsoft.Reporting.WebForms.Icons.Print.gif";title="Print" onclick="PrintReport();">'); 



     } 

     // Print Report function 

     function PrintReport() { 



      //get the ReportViewer Id 

      var rv1 = $('#MyReportViewer_ctl09'); 

      var iDoc = rv1.parents('html'); 



      // Reading the report styles 

      var styles = iDoc.find("head style[id$='ReportControl_styles']").html(); 

      if ((styles == undefined) || (styles == '')) { 

       iDoc.find('head script').each(function() { 

        var cnt = $(this).html(); 

        var p1 = cnt.indexOf('ReportStyles":"'); 

        if (p1 > 0) { 

         p1 += 15; 

         var p2 = cnt.indexOf('"', p1); 

         styles = cnt.substr(p1, p2 - p1); 

        } 

       }); 

      } 

      if (styles == '') { alert("Cannot generate styles, Displaying without styles.."); } 

      styles = '<style type="text/css">' + styles + "</style>"; 



      // Reading the report html 

      var table = rv1.find("div[id$='_oReportDiv']"); 

      if (table == undefined) { 

       alert("Report source not found."); 

       return; 

      } 



      // Generating a copy of the report in a new window 

      var docType = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">'; 

      var docCnt = styles + table.parent().html(); 

      var docHead = '<head><style>body{margin:5;padding:0;}</style></head>'; 

      var winAttr = "location=yes, statusbar=no, directories=no, menubar=no, titlebar=no, toolbar=no, dependent=no, width=720, height=600, resizable=yes, screenX=200, screenY=200, personalbar=no, scrollbars=yes";; 

      var newWin = window.open("", "_blank", winAttr); 

      writeDoc = newWin.document; 

      writeDoc.open(); 

      writeDoc.write(docType + '<html>' + docHead + '<body onload="window.print();">' + docCnt + '</body></html>'); 

      writeDoc.close(); 

      newWin.focus(); 

      // uncomment to autoclose the preview window when printing is confirmed or canceled. 

      // newWin.close(); 

     }; 

    </script> 









</head> 

<body> 

    <form id="form1" runat="server"> 

    <div> 

    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" EnablePartialRendering="False" /> 

     <rsweb:ReportViewer ID="MyReportViewer" runat="server"> 

     </rsweb:ReportViewer> 

    </div> 

    </form> 

</body> 

</html> 
+1

雖然這可能會顯示圖標,但打印功能是ActiveX,所以它仍然不起作用... – 2014-08-28 08:37:51

4

這裏是我曾創建一個僞打印按鈕,它模擬Internet Explorer中的報告查看器的打印功能到其他瀏覽器。

請注意下面的解決方案需要JQuery。沒有ActiveX安裝是必要的。

以下是這些步驟。

第1步。在報表查看器所在的頁面中添加打印按鈕。

<input id="PrintButton" title="Print" style="width: 16px; height: 16px;" type="image" alt="Print" runat="server" src="~/Reserved.ReportViewerWebControl.axd?OpType=Resource&amp;Version=11.0.3442.2&amp;Name=Microsoft.Reporting.WebForms.Icons.Print.gif" /> 

請務必將版本號更改爲您的RS版本。如果您在使用html代碼時遇到問題,可以使用Internet Explorer打開該頁面並檢查打印元素並將其複製。

第2步。添加一個div來顯示您的PDF。

<div class="pdf"> 
    </div> 

步驟3.添加腳本。

$(document).ready(function() { 
// Check if the current browser is IE (MSIE is not used since IE 11) 
     var isIE = /MSIE/i.test(navigator.userAgent) || /rv:11.0/i.test(navigator.userAgent); 

function printReport() { 

      var reportViewerName = 'ReportViewer'; //Name attribute of report viewer control. 
      var src_url = $find(reportViewerName)._getInternalViewer().ExportUrlBase + 'PDF'; 

      var contentDisposition = 'AlwaysInline'; //Content Disposition instructs the server to either return the PDF being requested as an attachment or a viewable report. 
      var src_new = src_url.replace(/(ContentDisposition=).*?(&)/, '$1' + contentDisposition + '$2'); 

      var iframe = $('<iframe>', { 
       src: src_new, 
       id: 'pdfDocument', 
       frameborder: 0, 
       scrolling: 'no' 
      }).hide().load(function() { 
       var PDF = document.getElementById('pdfDocument'); 
       PDF.focus(); 
       try { 
        PDF.contentWindow.print(); 
       } 
       catch (ex) { 
        //If all else fails, we want to inform the user that it is impossible to directly print the document with the current browser. 
        //Instead, let's give them the option to export the pdf so that they can print it themselves with a 3rd party PDF reader application. 

        if (confirm("ActiveX and PDF Native Print support is not supported in your browser. The system is unable to print your document directly. Would you like to download the PDF version instead? You may print the document by opening the PDF using your PDF reader application.")) { 
         window.open($find(reportViewerName)._getInternalViewer().ExportUrlBase + 'PDF'); 
        } 
       } 

      }) 

      //Bind the iframe we created to an invisible div. 
      $('.pdf').html(iframe); 


     } 

// 2. Add Print button for non-IE browsers 
     if (!isIE) { 

      $('#PrintButton').click(function (e) { 
       e.preventDefault(); 
       printReport(); 
      }) 
     } 

}); 

代碼解釋:

首先,我們創建了檢測,如果瀏覽器是IE或不變量。

通過在Reserved.ReportViewerWebControl中使用_getInternalViewer()方法。axd我們可以請求報告的PDF版本作爲最初通過點擊導出按鈕檢索到的請求。

然後,我們將contentDisposition變量指定爲「AlwaysInline」,因爲我們想要將報告作爲PDF請求,而不是作爲附件,而是作爲我們可以在HTML元素中呈現的PDF。 https://msdn.microsoft.com/en-us/library/microsoft.reporting.webforms.reportviewer.exportcontentdisposition.aspx

src_new變量將我們的新請求'AlwaysInline'替換爲默認的EXPORT按鈕內容處置請求(默認設置爲AlwaysAttachment)。

接下來,我們將iframe的src設置爲我們新加載的url,它會將reportviewer中的報告顯示爲PDF。

iframe中的鏈接命令包括隱藏PDF元素,在完成加載PDF後立即渲染並打印它。

結束講話

我希望有人能發現此代碼有用的,因爲我有一個很難的在線找到一個體面的解決辦法,這就是我想出了之後做一些研究。

+0

這對我來說不起作用 - FF v46.0。該程序不會跳轉到printReport();呼叫。它通過它。 – Fandango68 2016-12-08 06:51:02

+0

這不是工作夥計。 – 2017-05-11 17:35:50

0

我對上述內容做了一些修改,對Chrome和Firefox都適用。

function PrintReport() {   
    var reportViewerName = 'ReportViewer1'; 
    var src_url = $find(reportViewerName)._getInternalViewer().ExportUrlBase + 'PDF'; 

    var contentDisposition = 'AlwaysInline'; 
    var src_new = src_url.replace(/(ContentDisposition=).*?(&)/, '$1' + contentDisposition + '$2'); 

    var iframe = $('<iframe>', { 
     src: src_new, 
     id: 'iframePDF', 
     frameborder: 0, 
     scrolling: 'no' 
    }); 

    $('#pdfPrint').html(iframe); //There should be a div named "pdfPrint" 

    if (iframe != undefined && iframe.length > 0) { 
     var frame = iframe[0]; 

     if (frame != null || frame != undefined) { 
      var contentView = iframe[0].contentWindow; 

      contentView.focus(); 
      contentView.print(); 
     } 
    } 
} 
+0

這也不起作用 – 2017-05-11 18:09:41

+0

你能分享你的代碼嗎? – 2017-05-15 16:19:44

+0

http://stackoverflow.com/a/43953958/870561這是爲我工作 – 2017-05-16 05:46:21