Unable to Print to Network printer from Web Page

Posted by: tim_artz on 10 October 2019, 2:06 am EST

  • Posted 10 October 2019, 2:06 am EST

    I am currently working on a project that will allow reports to be sent to a network printer after they have completed a specific transaction on our web site. We generate the report using Active Reports version 9 and I use the below code on our server side to print the report. I am able to accomplish this when I test from my workstation (using my local IIS) but when I try to do the same action once the site has been updated on our web server the print doesn't work and I don't receive any errors. Has anyone had any experience trying to do this?

    Public Sub SendToPrinter(ByVal rpt As SectionReport, ByVal printerName As String, ByVal numberOfCopies As Short)
    Try
    rpt.Document.Printer.PrinterSettings.Copies = numberOfCopies
    rpt.Document.Printer.PrinterName = printerName
    rpt.Document.Print(False, False, False)
    Catch ex As Exception
    Throw
    End Try
    End Sub
  • Replied 10 October 2019, 3:57 pm EST

    Hello,

    This occurs when webserver does not have a access to the installed printer. Could you please check in web server printer driver, is print job created on clicking on the print button. If yes, then there must be a problem with the permission issue of the printer. Also, you can also check with the Microsoft PrintDocument. Is issue also reproducible with MS PrintDocument?

    Thanks,
    Mohit
  • Replied 11 October 2019, 1:22 am EST

    The printer is not installed locally on the web server. The print works when I run the web site from my computer, when the web site is running from the web server it does not work (NOTE: print driver is not installed on my computer)
  • Marked as Answer

    Replied 13 October 2019, 4:45 pm EST

    Hello,

    Could you please check with the following code and see if the print document successfully prints from the WebServer

    Private Sub btn_Print_Click()
    Dim pd As PrintDocument = New PrintDocument()
    Dim printControl = New Printing.StandardPrintController()
    pd.PrintPage += AddressOf PrintPage
    pd.PrintController = printControl
    Dim pdi As PrintDialog = New PrintDialog()
    pdi.Document = pd

    If pdi.ShowDialog() = DialogResult.OK Then
    pd.Print()
    End If
    End Sub

    Private Sub PrintPage(ByVal o As Object, ByVal e As PrintPageEventArgs)
    Dim img As System.Drawing.Image = System.Drawing.Image.FromFile("cat.jpeg")
    Dim loc As Point = New Point(100, 100)
    e.Graphics.DrawString("text", New Font("Arial", 10), Brushes.Blue, 100, 100)
    End Sub


    Thanks,
    Mohit

  • Replied 16 October 2019, 4:42 am EST

    I was able to resolve the issue by using memory stream to the network printer (see code below)

    ''' <summary>
    ''' Will send report to network printer using memory stream
    ''' </summary>
    ''' <param name="rpt">Executed Active Report</param>
    ''' <param name="printer">Name of printer and port to send the data. Printer name and port should be separated by colon</param>
    ''' <param name="numberOfCopies">Number of copies to print</param>
    Public Sub SendToPrinter(ByVal rpt As SectionReport, ByVal printer As String, ByVal numberOfCopies As Short)
    Try
    Using pdf As New PdfExport
    Using memStream As New IO.MemoryStream
    pdf.Export(rpt.Document, memStream)
    Using clientSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    Dim printerName = printer.Split(":"c)(0)
    Dim printerPort = CType(printer.Split(":"c)(1), Integer)
    Dim hostInfo = Dns.GetHostEntry(printerName)
    If hostInfo.AddressList.Count = 0 Then
    Throw New Exception(String.Format("Unable to find printer {0}", printerName))
    Else
    Dim ipep = New IPEndPoint(hostInfo.AddressList(0), printerPort)
    clientSocket.Connect(ipep)
    For x = 0 To numberOfCopies - 1
    clientSocket.Send(memStream.ToArray)
    Next
    End If
    End Using
    End Using
    End Using
    Catch ex As Exception
    Throw
    End Try
    End Sub
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels