Click here to Skip to main content
15,848,930 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
hi all,

im here because i dont know where i can find any solution for my problem. i developed ticket fare collections.and it is online (intranet company).i manage to print the ticket through thermal problem at all.

but come the problem is when i test the web page at user's place.when i click button print it is keep run til infinity.means nothing happen.. :( i assume the server cannot configure/reach the printer which we installed locally at the user's place.

so i really hope any method or way out from this problem.i really need this problem to be solve since i asked for delay the web to be live before.

here the code im using:
Public Function PreparePrintDocument() As PrintDocument
        ' Make the PrintDocument object.
        Dim print_document As New PrintDocument

        ' Install the PrintPage event handler.
        AddHandler print_document.PrintPage, AddressOf DrawStringPointF 'Print_PrintPage

        ' Return the object.
        Return print_document
    End Function

    Public Sub DrawStringPointF(ByVal sender As Object, ByVal e As PrintPageEventArgs)
        'Dim vEmplid As String = "000000"
        Dim connection As New SqlConnection(connectionString)

        Dim sqlstr As String = "select distinct a.ticketid,b.station_name as stationid,c.station_name as destination,CONVERT(VARCHAR(12),date_time,103) AS Date, CONVERT(VARCHAR(5),date_time,108) AS Time, " _
                               & " CONVERT(VARCHAR(5),tarikhrefcan,108) AS Date,d.station_name as ticket_type,e.station_name as journey_type,a.amount,a.refcanby " _
                               & " from ticketcollections as a,station_tbl as b,station_tbl as c,station_tbl as d,station_tbl as e " _
                               & " where a.ticketid = '" + TicketId.Text + "' " _
                               & " and a.refcanby = '" + HttpContext.Current.Session("Emplid") + "' " _
                               & " And a.stationid = b.staffid " _
                               & " And b.jenis = 0 " _
                               & " And a.destination = c.staffid " _
                               & " And c.jenis = 0 " _
                               & " And a.ticket_type = d.staffid " _
                               & " And d.jenis = 4 " _
                               & " And a.journey_type = e.staffid " _
                               & " and e.jenis=3 "

        Dim c As New SqlCommand(sqlstr, connection)
        c.Connection = connection
        Dim d As SqlDataReader = c.ExecuteReader()
        While d.Read()


            TicketId.Text = d.GetString(0)
            stationid.Text = d.GetString(1)
            Destination.Text = d.GetString(2)
            time.Text = d.GetString(4)
            dates.Text = d.GetString(3)
            tickettype.Text = d.GetString(6)
            journeytype.Text = d.GetString(7)
            amount.Text = Convert.ToDecimal(d(8))
            issuedby.Text = d.GetString(9)
            refdate.Text = d.GetString(5)

            ' Create string to draw.
            'Dim drawString As [String] = "Title Ticket"
            Dim drawString1 As [String] = stationid.Text & "  >  " & Destination.Text
            Dim drawString2 As [String] = Today.Date.ToString("dd/MMM/yyyy")
            Dim drawString3 As [String] = tickettype.Text & "      " & journeytype.Text & "       RM " & amount.Text
            Dim drawString4 As [String] = DateTime.Now.ToString("HH:MM")
            Dim drawString5 As [String] = TicketId.Text & " " & issuedby.Text
            Dim drawString6 As [String] = "Terms & Condition"
            Dim drawString7 As [String] = "Not valid as a ticket"
            Dim drawString8 As [String] = "Refund Receipt"
            Dim bitmap As New System.Drawing.Bitmap("C:\Logo.jpg")
            Dim width As Integer = bitmap.Width
            Dim height As Integer = bitmap.Height

            ' Create font and brush.
            Dim drawFont As New Font("Arial", 12, FontStyle.Bold)
            Dim drawFont2 As New Font("Arial", 6, FontStyle.Bold)
            Dim drawFont3 As New Font("Arial", 8, FontStyle.Bold)
            Dim drawFont4 As New Font("Arial", 18, FontStyle.Bold)
            Dim drawBrush As New SolidBrush(Color.Black)

            ' Create point for upper-left corner of drawing.
            'Dim drawPoint As New PointF(10.0F, 40.0F)
            Dim drawPoint1 As New PointF(10.0F, 83.0F)
            Dim drawPoint2 As New PointF(10.0F, 55.0F)
            Dim drawPoint3 As New PointF(10.0F, 100.0F)
            Dim drawPoint4 As New PointF(10.0F, 120.0F)
            Dim drawPoint5 As New PointF(70.0F, 170.0F)
            Dim drawPoint6 As New PointF(50.0F, 185.0F)
            Dim drawPoint7 As New PointF(90.0F, 155.0F)
            Dim drawPoint8 As New PointF(10.0F, 40.0F)

            ' Draw string to screen.
            e.Graphics.DrawImage(bitmap, 10, 15, 12.0F * width, 0.08F * height)
            'e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint)
            e.Graphics.DrawString(drawString1, drawFont, drawBrush, drawPoint1)
            e.Graphics.DrawString(drawString2, drawFont4, drawBrush, drawPoint2)
            e.Graphics.DrawString(drawString3, drawFont, drawBrush, drawPoint3)
            e.Graphics.DrawString(drawString4, drawFont4, drawBrush, drawPoint4)
            e.Graphics.DrawString(drawString5, drawFont3, drawBrush, drawPoint5)
            e.Graphics.DrawString(drawString6, drawFont2, drawBrush, drawPoint6)
            e.Graphics.DrawString(drawString7, drawFont3, drawBrush, drawPoint7)
            e.Graphics.DrawString(drawString8, drawFont3, drawBrush, drawPoint8)

        End While
    End Sub
    Protected Sub Print_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Print.Click
        If HttpContext.Current.Session("Emplid") = "" Then
            Exit Sub
        End If

        ' Make a PrintDocument object.
        Dim print_document As PrintDocument
        'For x = 1 To 1
        print_document = PreparePrintDocument()

        ' Print immediately.

    End Sub

if there are other ways please tell me

thanks in advance,
Updated 4-May-13 4:06am

1 solution

I'm not sure I understand all the details here, so if I'm wrong about any of this, please correct me.

1.) The printer connected to your server is not the printer connected to the remote user's computer. And your remote user is not on the same network... is he/she? If this is the case, then this will just never work. If you want to control printing from the server, then the printer needs to be connected to the server (through the network), and configured on the server.

2.) This is really just a bad idea. You should write your application so that it will print to your remote user's default printer instead, and then just configure the printer you like as the default printer.

3.) One way you could do this the way you're trying to do it is to set up a VPN. You mentioned that the remote user has a printer you set up. I assume he/she's a trusted colleague. So configure a vpn on your business network, and allow your remote user to connect to it. Your remote user should connect to the network your server is on. Then configure the remote printer on the server.

I didn't even look at your code. I don't think there's anything wrong with it... I think you're just assuming remote web users will be able to print to their local printers, when you have the application written to print to a printer connected to a local server... and that just won't work.

Hope this helps,
- Pete
Share this answer
musiw 8-May-13 0:32am    
i understand your answer.if u can help me give me any solutions...?my system is need to print tickets.if window.print() all the url at header and footer will be print also.and important thing the ticket must be print 1 by 1 not 5 straightly(if customer buy 5 tickets).the ticket will be long...
pdoxtader 8-May-13 15:32pm    
I think you should do whay sites like Fandango do - open another window (maybe a modal window just during the printing) that is formatted the way you need it to be, and only has the elements you need to print on it (ie: no footer or header). Javascript will be a huge help here.

Here's a great free modal window script:

I happen to like TinyBox2, but of course you can also look at what JQuery has to offer. I would build the print page dynamically with Javascript and just pass it to TinyBox 2. Have it kick off your window.print() on load, and kill then kill the modal window after print. It should happen fairly quickly, and not interrupt your user's workflow too badly.

Hope I was able to help you out.
- Pete

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900