Avatar of Mani Pazhana
Mani Pazhana
Flag for United States of America asked on

Printing with ASP.Net hosted on IIS 7.0

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
        printDoc.Print()          

    End Sub



any idea?

Thanks
ASP.NETVisual Basic.NETC#

Avatar of undefined
Last Comment
Nasir Razzaq

8/22/2022 - Mon
Mani Pazhana

ASKER
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)
Robberbaron (robr)

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.
Mani Pazhana

ASKER
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?

Thanks
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Mani Pazhana

ASKER
I need a sample code(VB.Net/ASP.Net) that will print the report in batch..i need to print multiple reports...
Nasir Razzaq

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?
Mani Pazhana

ASKER
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)
        Try
            If item.Revised Then
                report.ReportPath = "ReqReportRevised.rdlc"
            Else
                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)))
            Export(report)

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

    End Sub
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Mani Pazhana

ASKER
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)
        fs.Close()

        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.Close()
        document = Nothing
        reader.Close()
        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}
        p.Start()
        p.WaitForInputIdle()
        p.CloseMainWindow()
        p.Kill()
    End Sub
ASKER CERTIFIED SOLUTION
Nasir Razzaq

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.