'How to Print a .rdlc in ASP.net when button click

I have an web application that when an user clicks on a button it prints an .rdlc directly to the printer without the Print Dialog box. This works fine when I run it local, but when I uploaded it to the intranet is not working at all. Can someone explain how to make this work in the intranet?

UPDATE

        private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
    {
        Stream stream = new MemoryStream();
        m_streams.Add(stream);
        return stream;
    }

    private void Export(LocalReport report)
    {
        string deviceInfo =
          "<DeviceInfo>" +
          "  <OutputFormat>EMF</OutputFormat>" +
          "  <PageWidth>8.5in</PageWidth>" +
          "  <PageHeight>11in</PageHeight>" +
          "  <MarginTop>0.25in</MarginTop>" +
          "  <MarginLeft>0.25in</MarginLeft>" +
          "  <MarginRight>0.25in</MarginRight>" +
          "  <MarginBottom>0.25in</MarginBottom>" +
          "</DeviceInfo>";
        Warning[] warnings;
        m_streams = new List<Stream>();
        report.Render("Image", deviceInfo, CreateStream, out warnings);
        foreach (Stream stream in m_streams)
        {
            stream.Position = 0;
        }
    }
    private void PrintPage(object sender, PrintPageEventArgs ev)
    {
        Metafile pageImage = new Metafile(m_streams[currentPageIndex]);
        ev.Graphics.DrawImage(pageImage,ev.PageBounds);
        currentPageIndex++;
        ev.HasMorePages = (currentPageIndex < m_streams.Count);
    }
    private void Print_Ticket()
    {

        const string printerName = "HPLaser"
        if (m_streams == null || m_streams.Count == 0)
            return;
        PrintDocument printDoc = new PrintDocument();
        printDoc.PrinterSettings.PrinterName = printerName;
        if (!printDoc.PrinterSettings.IsValid)
        {
            string msg = String.Format("Can't find printer \"{0}\".", printerName);
            Console.WriteLine(msg);
            return;
        }
        printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
        printDoc.Print(); //Prints Document
    }
    public void WriteTicket(string ticket_number, string queue)
    {
        LocalReport report = new LocalReport();
        //Displays ticket letter and number in ticket
        report.ReportPath = "PrintTicket.rdlc";
        ReportParameter ticket_parameter = new ReportParameter();
        ticket_parameter.Name = "Ticket";
        ticket_parameter.Values.Add(ticket_number);
        report.SetParameters(new ReportParameter[] { ticket_parameter });

        //Displays date and time in ticket
        ReportParameter date = new ReportParameter();
        date.Name = "Date_Time";
        date.Values.Add(System.DateTime.Now.ToString());
        report.SetParameters(new ReportParameter[] { date });

        //Displays branch location in ticket
        ReportParameter location_parameter = new ReportParameter();
        location_parameter.Name = "Location";
        location_parameter.Values.Add(queue);
        report.SetParameters(new ReportParameter[] { location_parameter });

        Export(report);
        currentPageIndex = 0;
        Print_Ticket();
    }
    //Clears stream
    public new void Dispose()
    {
        if (m_streams != null)
        {
            foreach (Stream stream in m_streams)
                stream.Close();
        }
    }


Solution 1:[1]

Looks like your IIS application is the one that's responsible for launching print jobs. The user has no control over printing.

While this is doable, you need to first perform these steps on the IIS server:

  1. Create a local user account on the IIS server
  2. Log in with the new user account, and set up the printer (make sure it's set up as default printer)
  3. Print out a test page (if it doesn't work, check what ports the server uses to communicate with the printer and make sure those ports are open)
  4. Setup the IIS application pool to run with the credentials of the new user account you created (it has to be an interactive account--don't run the site under networkservice, or localservice for instance.)
  5. Try printing from your aspx page

Solution 2:[2]

You need to export the report to PDF, then print using iTextSharp. Also client needs to install the PDF reader as well.

Create hidden iFrame as follows:

<iframe id="frmPrint" name="IframeName" width="500" 
  height="200" runat="server" 
  style="display: none" runat="server"></iframe>

Add an ASP.NET button:

<asp:ImageButton ID="btnPrint" runat="server" OnClick="btnPrint_Click"  />

Add the following references:

using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;

Add the following code to button click event:

Warning[] warnings;              
string[] streamids;
string mimeType;
string encoding;
string extension;

byte[] bytes = View.ReportViewer.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 existing PDF
Document document = new Document(PageSize.LETTER);
PdfReader reader = new PdfReader(HttpContext.Current.Server.MapPath("output.pdf"));
//Getting a instance of new PDF writer
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";

`

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Dilipan Rajendran