0

I have found good information on how to automatically download a file to the client advertised as a solution of how to print using code (https://forums.asp.net/t/1233841.aspx?How+do+I+print+from+Reporting+Services+AUTOMATICALLY+in+VB+Net+web+app+) but what I need to do is have the code print the document without the user interacting.

From what I have found it appears this can not be done as one might casually think. ReportViewer for example does not have a 'print' method. The only two solutions appears to be to use ProcessStart (which then means saving the file to the file system before printing which I dont want to do) or maybe (will be researching this today) create a subscription using code and then delete it later.

  • Maybe you are trying to do something like this https://stackoverflow.com/questions/731049/how-to-print-a-reportviewers-report-without-showing-a-form – tgolisch May 26 '17 at 15:14
  • when it comes to printing unfortunately webforms and winforms are a universe apart. – user3074761 May 26 '17 at 16:17
  • Oh. I'm pretty sure you will not be able to get a web-form to auto-print. It is a browser security thing. Think about what a mischievous person could do. You go to a web page and it auto-prints a couple thousand pages to your printer. However, if your server is in the same building as your users, (like an intranet) that is possible. – tgolisch May 26 '17 at 16:47
  • This is an intranet solution but the code doesnt appear to exist to do what we are talking about. Now when the user hits print I might be able to bitstream the file, then save it on the file system, then run Process.Start (because that approach needs a physical file not a bitstream) but that all seems horribly inefficient just because of methods where not created (such as running Process.Start against a bitstream instead of a physical file). So we are at a loss. – user3074761 May 30 '17 at 13:08
  • You might be thinking about this the wrong way. Instead of printing from the client (browser), the server sends the print job to the printer. You would use the same code as a desktop app. Since it will only run on the server, you don't use a screen, or buttons. Otherwise, the print code and logic are the same.. – tgolisch May 30 '17 at 13:28

1 Answers1

0

You are not able to print a report directly from your asp.net page. The reason for this is security. If it allowed you to send a file through the network and onto the clients computer, and then search the computer for a printer, this could cause major security issues. The report viewer does have a print icon, but this disappears when you deploy the project and run the page remotely. I have faced the same issue in the past and found it best to just export the report to say PDF and allow the user to Download it. I have used the below code to accomplish this task in the past:

  Private Sub CreatePDFMatrix(fileName As String)
    '  ReportViewer1.LocalReport.DataSources.Clear()
    Dim adapter As New ReportDataSetTableAdapters.vwPrintTagsTableAdapter
    Dim table As New ReportDataSet.vwPrintTagsDataTable
    Dim month = MonthName(Date.Today.Month)
    Dim year = Date.Today.Year
    Dim p(1) As ReportParameter
    Dim warnings() As Warning
    Dim streamIds As String()
    Dim mimeType As String = String.Empty
    Dim encoding As String = String.Empty
    Dim extension As String = String.Empty
    Dim adpt2 As New ReportDataSetTableAdapters.vwPrintTagsTableAdapter


        adapter.FillForMainReport(table, DropDownList1.SelectedValue, g_Company, g_Division)


    Me.ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("DataSet1", CType(table, DataTable))) 'Add(New ReportDataSource("ReportingData", CType(table, DataTable)))
    Me.ReportViewer1.DataBind()
    Dim viewer = ReportViewer1
    viewer.ProcessingMode = ProcessingMode.Local
    viewer.LocalReport.ReportPath = "Report1.rdlc"
    p(0) = New ReportParameter("MonthYear", month & "-" & year)
    Dim check = DropDownList1.SelectedValue
    ReportViewer1.LocalReport.SetParameters(p(0))
    p(1) = New ReportParameter("Location", DropDownList1.SelectedValue)
    ReportViewer1.LocalReport.SetParameters(p(1))

    Try
        Dim bytes As Byte() = viewer.LocalReport.Render("PDF", Nothing, mimeType, encoding, ".pdf", streamIds, warnings)

        Response.Buffer = True
        Response.Clear()
        Response.ContentType = mimeType
        Response.AddHeader("content-disposition", Convert.ToString((Convert.ToString("attachment; filename=") & fileName) + ".") & extension)
        Response.BinaryWrite(bytes)
        ' create the file
        Response.Flush()
    Catch ex As Exception
        Debug.Print(ex.ToString)
    End Try

End Sub
ag93
  • 343
  • 4
  • 17