Chrome 和 Firefox 中的 SSRS 打印按钮

发布于 2024-10-03 04:42:21 字数 1468 浏览 4 评论 0原文

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

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

我需要能够以与在控件内完成的方式相同的方式向报表查看器发出打印命令,但仅限于 Firefox 和 Chrome。

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

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

有什么想法吗?

I have reports in SSRS 2005. I am using remote reports. In IE, the print button shows, but in Firefox and Chrome, the print button does not show.

My reports are shown in jquery UI dialogs, so I cannot just do a window.print. My reports render just fine within the modals.

I need to be able to issue a print command to the reportviewer the same way it is done from within the control, but only in firefox and chrome.

I dug into the markup for the report viewer, and found this code. I tried to manually inject it into the reportviewer with no success.

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

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

感受沵的脚步 2024-10-10 04:42:21

以下是我创建一个伪打印按钮的方法,该按钮将 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&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。

通过使用 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 后立即打印它。

结束语

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

Here's what I did to create a pseudo print button which emulates the print function of Report Viewer in Internet Explorer to other browsers.

Please take note that the solution below requires JQuery. No ActiveX installation is necessary.

Here are the steps.

Step 1. Add the print button in your page where the report viewer is situated.

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

Be sure to change the Version number to your RS version. If you're having trouble with the html code, you can open the page with internet explorer and inspect the print element and copy it.

Step 2. Add a div where your PDF will be rendered.

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

Step 3. Add the script.

$(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();
            })
        }

});

Code explanation :

First we created a variable which detects if the browser is IE or not.

By using the _getInternalViewer() method in Reserved.ReportViewerWebControl.axd we can request a PDF version of the report as a request which is originally retrieved upon clicking the export button.

We then assigned a contentDisposition variable as 'AlwaysInline' because we want to request the report as a PDF, not as an attachment but as a PDF that we can render in an html element.
https://msdn.microsoft.com/en-us/library/microsoft.reporting.webforms.reportviewer.exportcontentdisposition.aspx

The src_new variable replaces the default EXPORT button content disposition request (which is set to AlwaysAttachment by default) with our new request 'AlwaysInline'.

Next, we then set the src of the iframe to our new url which when loaded, will reveal our report from reportviewer as PDF.

The chained commands in the iframe includes hiding the pdf element, rendering it and printing it right away after it finishes loading the pdf.

Ending remarks

I hope that someone will find this code useful because I had a hard time finding a decent solution online and this is what I came up with after doing some research.

终难遇 2024-10-10 04:42:21

不幸的是,除 IE 之外的浏览器不支持打印按钮。

我认为您已经意识到这一点并采取了解决方法,但我们还没有拿出一个像样的解决方案。虽然我们的大多数用户更喜欢直接从 Excel 打印,所以我们允许他们导出文件,然后进行打印。

这个问题将是一个很好的链接:

SQL Reporting Services - Print Mozilla 中未显示按钮

Unfortunately the print button is not supported in browsers other than IE.

I think you realise this and did a workaround, we have yet to come up with a decent solution as well. Although most of our users prefer printing from Excel directly, so we allow them to export the file which they then do the printing.

This SO question would be a good link:

SQL Reporting Services - Print Button not shown in Mozilla

落在眉间の轻吻 2024-10-10 04:42:21

请查找 SSRS 报告的代码,该报告具有适用于 Firefox 和 Chrome 的打印图标和打印功能:

<%@ 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&Version=9.0.30729.1&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>

Please find the code for SSRS report that has Print Icon with Printing functionality for Firefox and Chrome:

<%@ 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&Version=9.0.30729.1&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>
幻梦 2024-10-10 04:42:21

我对上述内容做了一些更改,它在 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();
        }
    }
}

I made some changes with regards to the above and it works well for me in both Chrome and 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();
        }
    }
}
吃颗糖壮壮胆 2024-10-10 04:42:21

我对上述内容做了一些更改,它在 Chrome 和 Edge 中都很适合我。

function printReport() {
    var reportViewerName = 'ReportViewer2'; //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 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(src_new, "", winAttr);

     newWin.print();
 }

I made some changes with regards to the above and it works well for me in both Chrome and Edge.

function printReport() {
    var reportViewerName = 'ReportViewer2'; //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 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(src_new, "", winAttr);

     newWin.print();
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文