jquery Chrome和Firefox中的SSRS打印按钮

acruukt9  于 2023-01-16  发布在  jQuery
关注(0)|答案(4)|浏览(135)

我在SSRS 2005中有报告,我正在使用远程报告,在IE中,打印按钮显示,但在Firefox和Chrome中,打印按钮不显示。
我的报告显示在jquery UI对话框中,所以我不能只执行window.print,我的报告在模态中呈现得很好。
我需要能够向reportviewer发出打印命令,就像在控件内部一样,但只能在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>

有什么想法吗?

chhkpiq4

chhkpiq41#

下面是我创建一个伪打印按钮的过程,该按钮模拟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&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.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,当加载该url时,将从reportviewer显示PDF格式的报告。
iframe中的链式命令包括隐藏pdf元素、呈现它以及在加载完pdf后立即打印它。

    • 结束语**

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

x6yk4ghg

x6yk4ghg2#

遗憾的是,打印按钮在IE以外的浏览器中不受支持。
我想你意识到这一点,并做了变通办法,我们还没有拿出一个体面的解决方案,以及。虽然我们的大多数用户喜欢直接从Excel打印,所以我们允许他们导出文件,然后他们做打印。
这个SO问题是一个很好的链接:
SQL Reporting Services - Print Button not shown in Mozilla

oiopk7p5

oiopk7p53#

请查找具有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>
kmbjn2e3

kmbjn2e34#

我做了一些修改,关于上述和它的工作很好,我在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();
        }
    }
}

相关问题