Jos*_*osh 3 printing jquery reportviewer reportingservices-2005
我在SSRS 2005中有报告。我正在使用远程报告。在IE中,显示“打印”按钮,但在Firefox和Chrome中,不显示“打印”按钮。
我的报告显示在jquery UI对话框中,所以我不能只执行window.print。我的报告在模态中很好地呈现。
我需要能够以与在控件内相同的方式向reportviewer发出打印命令,但只能在Firefox和chrome中使用。
我研究了报表查看器的标记,并找到了此代码。我尝试将其手动注入到reportviewer中,但没有成功。
<table id="reportViewer_ctl01_ctl07_ctl00_ctl00" onclick="document.getElementById('reportViewer').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&Version=9.0.30729.4402&Name=Microsoft.Reporting.WebForms.Icons.Print.gif" alt="Print" style="height:16px;width:16px;padding:2px;" /></td>
                                </tr>
                            </table>
有任何想法吗?
这是我创建一个伪打印按钮所做的操作,该按钮可模拟Internet Explorer中Report Viewer到其他浏览器的打印功能。
请注意,以下解决方案需要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&Version=11.0.3442.2&Name=Microsoft.Reporting.WebForms.Icons.Print.gif" />
确保将版本号更改为您的RS版本。如果您在使用html代码时遇到问题,则可以使用Internet Explorer打开页面并检查打印元素并进行复制。
第2步。添加将在其中渲染PDF的div。
<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.axd中使用_getInternalViewer()方法,我们可以请求报告的PDF版本作为请求,该请求最初是在单击导出按钮时检索到的。
然后,我们将contentDisposition变量分配为“ AlwaysInline”,因为我们希望将报告以PDF而不是附件的形式请求,而是以可以在html元素中呈现的PDF的形式请求。 https://msdn.microsoft.com/zh-cn/library/microsoft.reporting.webforms.reportviewer.exportcontentdisposition.aspx
src_new变量用我们的新请求“ AlwaysInline”替换了默认的EXPORT按钮内容处置请求(默认情况下设置为AlwaysAttachment)。
接下来,我们将iframe的src设置为新的网址,该网址在加载后将以PDF格式显示来自reportviewer的报告。
iframe中的链接命令包括隐藏pdf元素,渲染并在完成pdf加载后立即打印。
结束语
我希望有人会觉得这段代码有用,因为我很难在网上找到一个不错的解决方案,而这是我做一些研究后想到的。