Showing posts with label PrintButton. Show all posts
Showing posts with label PrintButton. Show all posts

Saturday, December 16, 2017

Microsoft Report Viewer Print in Other browser as well as in different OS

As report viewer need ActiveX control to print the report which was possible in Windows IE only.

But after a long search on google. I have found 2 Solution which will allow you to print in Chrome and FireFox as well.

Solution 1: (Render content in HTML and call Print Command through JavaScript)

Put this code in Head of the page.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        // Linking the print function to the print button
        $(document).ready(function () {
            $('#printreport').click(function () {
                //alert('Hi');
                printReport('ReportViewer1');
            });
            function printReport(report_ID) {
                var rv1 = $('#' + report_ID);
                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 = '<style>body{margin:0;padding:0; line-height: 0.5em;} @page { size: A4; margin: 0;} @media print { html, body {  width: 210mm; height: 297mm; }} @media print { footer { page-break-after: always; }} td {padding:0;}</style>';
                var winAttr = "location=yes, statusbar=no, directories=no, menubar=no, titlebar=no, toolbar=no, dependent=no, width=900px, height=700px, resizable=yes, screenX=100, screenY=100, 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();

                // The print event will fire as soon as the window loads
                newWin.focus();
                // uncomment to autoclose the preview window when printing is confirmed or canceled.
                // newWin.close();
            };

        });
    </script>
 Control on Page
<input id="printreport" type="button" value="Print" />
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana"            Font-Size="8pt" InteractiveDeviceInfos="(Collection)" ShowExportControls="true"            WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Height="600px" Width="820px" ShowPrintButton="true">
<LocalReport ReportPath="report.rdlc">  </LocalReport> 
</rsweb:ReportViewer>

Solution 2 (Render report content in PDF and call print function.)

Required itextsharp.dll version 5.0

<asp:ImageButton ID="btnPrint" runat="server" OnClick="btnPrint_Click" ImageUrl="https://cdn3.iconfinder.com/data/icons/black-easy/512/535129-print_512x512.png" Height="50px"  /> 
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana"            Font-Size="8pt" InteractiveDeviceInfos="(Collection)" ShowExportControls="true"            WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Height="600px" Width="820px" ShowPrintButton="true">
<LocalReport ReportPath="report.rdlc">  </LocalReport> 
</rsweb:ReportViewer>
 using System.IO; 
using iTextSharp.text.pdf;
using iTextSharp.text; 
using Microsoft.Reporting.WebForms; 
protected void btnPrint_Click(object sender, ImageClickEventArgs e)
        {
            Warning[] warnings;
            string[] streamids;
            string mimeType;
            string encoding;
            string extension;

            byte[] bytes = ReportViewer1.LocalReport.Render("PDF", null, out mimeType,
                           out encoding, out extension, out streamids, out warnings);

            FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("output.pdf"), FileMode.Create);
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();

            //Open exsisting pdf
            Document document = new Document(PageSize.A4);
            PdfReader reader = new PdfReader(HttpContext.Current.Server.MapPath("output.pdf"));
            //Getting a instance of new pdf wrtiter
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(
               HttpContext.Current.Server.MapPath("Print.pdf"), FileMode.Create));
            document.Open();
            PdfContentByte cb = writer.DirectContent;

            int i = 0;
            int p = 0;
            int n = reader.NumberOfPages;
            Rectangle psize = reader.GetPageSize(1);

            float width = psize.Width;
            float height = psize.Height;

            //Add Page to new document
            while (i < n)
            {
                document.NewPage();
                p++;                i++;
                PdfImportedPage page1 = writer.GetImportedPage(reader, i);
                cb.AddTemplate(page1, 0, 0);
            }
            //Attach javascript to the document
            PdfAction jAction = PdfAction.JavaScript("this.print(true);\r", writer);
            writer.AddJavaScript(jAction);
            document.Close();

            //Attach pdf to the iframe
            frmPrint.Attributes["src"] = "Print.pdf";
        }
This solution has been checked in Windows, Mac & Ubuntu with Chrome & FireFox Browser

Friday, August 22, 2014

Microsoft Report Viewer Print Button not Working / Visible in IE 10 & 11


I had created report in on of my client website using "Microsoft Report Viewer Control" but a very known problem with Report Viewer is Printing is not possible in other browser instead of Internet Explorer.

Today I have faced a new problem that Print Button is not visible in IE 11 when Client updated their  Windows 8 to Windows 8.1. 



Then I started goggling and Finally I found  that it's happens because of Active X. And I followed these steps give below.

  •  Start Windows Internet Explorer.
  • On the Tools menu, click Internet Options.
  • On the Security tab, click Local Intranet, and then click Sites
  • Click Advanced.
  • Type the URL of your site in the Add this website to the zone box, and then click Add.  (in development mode it will be http://localhost)
  • Click Close to close the Local Intranet dialog box, and then click OK.
  • On the Security tab, click Custom Level.
  • Click Enable for each component that is listed under ActiveX controls and plugins.
  • Click OK to close the Internet Options dialog box.
  • Restart Internet Explorer
  • Try printing the report.

It's working fine now !!!.

Hope it will help you. :)