jquery - Chrome 和 Firefox 中的 SSRS 打印按钮

标签 jquery printing reportviewer reportingservices-2005

我在 SSRS 2005 中有报告。我正在使用远程报告。在 IE 中,会显示打印按钮,但在 Firefox 和 Chrome 中,不会显示打印按钮。

我的报告显示在 jquery UI 对话框中,因此我不能只执行 window.print。我的报告在模态中渲染得很好。

我需要能够向报表查看器发出打印命令,就像在控件中执行该命令一样,但仅限于 Firefox 和 Chrome。

我深入研究了报告查看器的标记,并找到了这段代码。我尝试手动将其注入(inject)到报表查看器中,但没有成功。

<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>

有什么想法吗?

最佳答案

以下是我创建一个伪打印按钮的方法,该按钮将 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 步。添加将在其中呈现 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。

通过使用 Reserve.ReportViewerWebControl.axd 中的 _getInternalViewer() 方法,我们可以请求 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,加载后,将以 PDF 形式显示来自 reportviewer 的报告。

iframe 中的链接命令包括隐藏 pdf 元素、渲染它以及在加载完 pdf 后立即打印它。

结束语

我希望有人会发现这段代码有用,因为我很难在网上找到一个像样的解决方案,这是我在做了一些研究后想到的。

关于jquery - Chrome 和 Firefox 中的 SSRS 打印按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4208457/

相关文章:

javascript - 如何优化此函数以使任意数量的 div 具有相同的高度?

javascript - 从 jquery 中删除最后一个子元素

python - 使用python根据变量正确打印字符

java - 我如何检测 JasperViewer 处理报告时间

pdf - 使用 SSRS/ReportViewer 创建 PDF 时字体替换到/从 PDF 的规则?

jquery - 获取 Javascript 以滚动下个月的日期

javascript - 如何使用jquery以不区分大小写的方式获取URL参数

batch-file - Windows 10 从命令行打印为 PDF

python - Python中嵌套数据结构中的四舍五入小数

asp.net - 在VS 2010下将参数传递给RDLC中的子报表