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
LVL 8
mani_saiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mani_saiAuthor Commented:
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)
0
Robberbaron (robr)Commented:
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.
0
mani_saiAuthor Commented:
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
0
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

mani_saiAuthor Commented:
I need a sample code(VB.Net/ASP.Net) that will print the report in batch..i need to print multiple reports...
0
CodeCruiserCommented:
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?
0
mani_saiAuthor Commented:
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
0
mani_saiAuthor Commented:
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
0
CodeCruiserCommented:
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?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.