We help IT Professionals succeed at work.

Printing with ASP.Net hosted on IIS 7.0

Mani Pazhana
Mani Pazhana asked
Hello Experts,
I am using ASP.NET /VB.Net web application.

I am trying to print the report, it work locally when tested from visual studio 2010... but not from the Server when deployed to IIS.

I tried different options, it is not finding the printer when the application deployed to IIS on the Network server...

here is some snippet I used with no luck...

 Private Function DefaultPrinterName() As String
        Dim settings As Drawing.Printing.PrinterSettings = New Drawing.Printing.PrinterSettings()
        Return settings.PrinterName
    End Function

 Private Sub Print()
        If m_streams Is Nothing OrElse m_streams.Count = 0 Then
            Throw New Exception("Error: no stream to print.")
        End If
        Dim printDoc As New PrintDocument()
        printDoc.PrinterSettings.PrinterName = DefaultPrinterName()
        AddHandler printDoc.PrintPage, AddressOf PrintPage
        m_currentPageIndex = 0

    End Sub

any idea?

Watch Question


From the event log, it is not finding the printer

Here is error I see from event log on the server:

Event code: 3005
Event message: An unhandled exception has occurred.
Event time: 3/25/2015 12:28:08 PM
Event time (UTC): 3/25/2015 5:28:08 PM
Event ID: aabdefc27938450d876f3271cd3481d0
Event sequence: 42
Event occurrence: 1
Event detail code: 0
Application information:
    Application domain: /LM/W3SVC/2/ROOT-3-130717780815649192
    Trust level: Full
    Application Virtual Path: /
    Application Path: C:\ReqTracker\
    Machine name: CPSTAGING
Process information:
    Process ID: 4416
    Process name: w3wp.exe
    Account name: IIS APPPOOL\ReqTrackerStaging
Exception information:
    Exception type: InvalidPrinterException
    Exception message: No printers are installed.
   at PrintObject.PrintReport(Int32 reqId)
   at Requisitions.btnBatchPrint_Click(Object sender, EventArgs e)
   at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
the code is running on the server, so it is only able to access printer defined on the server.

thats why most apps provide a 'preview' open that the user prints locally.


i am seeing the network printer installed on the server.

When I logged in , the print function work.  If I am not logged in to the server , it is not working

Any idea?



I need a sample code(VB.Net/ASP.Net) that will print the report in batch..i need to print multiple reports...
Most Valuable Expert 2012
Top Expert 2014

Its not a report is it? You are printing rendering text yourself into printer driver. That is more suitable for a windows based application rather than a web application. Have you considered using a report component like crystal reports?


I am having a report build like this:

Not able to print when I access the report  outside the server.  it prints locally and also when I logged into the server...but not outside the server...

 Public Sub PrintReport(ByVal reqId As Integer)
        Dim printObject As New PrintObject
        Dim report As New Microsoft.Reporting.WebForms.LocalReport()
        Dim item As Requisition = RequisitionManager.getReqbyReqid(reqId)
            If item.Revised Then
                report.ReportPath = "ReqReportRevised.rdlc"
                report.ReportPath = "ReqReport.rdlc"
            End If
            report.DataSources.Add(New Microsoft.Reporting.WebForms.ReportDataSource("DataSet1_getPartList", printObject.GetPartListReport(reqId)))
            report.DataSources.Add(New Microsoft.Reporting.WebForms.ReportDataSource("DataSet1_getReqbyReqid", printObject.GetReqbyReqId(reqId)))

        Catch ex As Exception
            Throw (ex)
            item = Nothing
            report = Nothing
        End Try

    End Sub


remaning   code:

  Private Sub Export(ByVal report As Microsoft.Reporting.WebForms.LocalReport)

        Dim warnings As Microsoft.Reporting.WebForms.Warning() = Nothing
        Dim streamids() As String = Nothing
        Dim mimeType As String = String.Empty
        Dim encoding As String = String.Empty
        Dim extension As String = String.Empty

        Dim _r As Random = New Random
        Dim uniqueNumber As String = _r.Next().ToString()

        Dim bytes() As Byte = report.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)
        Dim fs As FileStream = New FileStream(HttpContext.Current.Server.MapPath("Output_" + uniqueNumber + ".pdf"), FileMode.Create)
        fs.Write(bytes, 0, bytes.Length)

        Dim document As iTextSharp.text.Document = New iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER)
        Dim reader As iTextSharp.text.pdf.PdfReader = New iTextSharp.text.pdf.PdfReader(HttpContext.Current.Server.MapPath("Output_" + uniqueNumber + ".pdf"))
        PrintPDFfiles(HttpContext.Current.Server.MapPath("Output_" + uniqueNumber + ".pdf"))

        document = Nothing
        reader = Nothing

        FileCleanUpOnServer(HttpContext.Current.Server.MapPath("Output_" + uniqueNumber + ".pdf"))
    End Sub

    Private Sub PrintPDFfiles(ByVal fileName As String)
        Dim p As New Process()
        p.StartInfo = New ProcessStartInfo() With {.CreateNoWindow = True, .Verb = "print", .FileName = fileName}
    End Sub
Most Valuable Expert 2012
Top Expert 2014
Printers are installed for users. When no user is logged in, there is no printer. What happens if you run your application pool under your own windows account?