16 April 2015

SSRS: Mencetak Langsung Ke Printer


Pembuatan report biasanya secara default menyediakan halaman preview sebelum kita melakukan printing. Namun di beberapa kasus kita ingin mencetak (print) langsung tanpa harus preview menampilkan report terlebih dahulu. Klo report nya pake SQL Reporting yang file rdl nya ditaro di sever, gimana cara nya? Hal ini juga sempet bikin puyeng, karena waktu googling source nya minim banget. Alhamdulillah tapi nya bisa dapet dan jalan. Berikut contoh dengan menggunakan code VB.NET. Report yang akan dicetak:

Buat UI seperti gambar di bawah ini, hanya form dengan button. Report Viewer akan kita buat secara runtime, dan tersembunyi.
Kemudian kita buat kode berikut:
Imports System.Drawing.Printing 
Imports System.Drawing.Imaging 
Imports System.IO 
Imports Microsoft.Reporting.WinForms

Public Class frmPrintReport
    Dim pages As New List(Of Metafile)
   
Dim pageIndex As Integer = 0
   
Dim doc As New Printing.PrintDocument()
   
Dim ReportViewer1 As New ReportViewer

    Private Sub frmPrintReport_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
         

        Button1.Text = "Print"
        With ReportViewer1
            .Visible = False
            .ProcessingMode = ProcessingMode.Remote
            .ServerReport.ReportPath = "/HRD/Attendance Report"
            .ServerReport.ReportServerUrl = New _
                     Uri("http://localhost/ReportServer")
        End With
        Me.Controls.Add(ReportViewer1)

    End Sub

    Private Sub PrintPageHandler(ByVal sender As Object, _
        ByVal e As PrintPageEventArgs)
        Dim page As Metafile = pages(pageIndex)
        pageIndex += 1
        Dim pWidth As Integer = 827
        Dim pHeight As Integer = 1100
        e.Graphics.DrawImage(page, 0, 0, pWidth, pHeight)
        e.HasMorePages = pageIndex < pages.Count
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        Dim doc As New Printing.PrintDocument()
        doc = New Printing.PrintDocument()
        AddHandler doc.PrintPage, AddressOf PrintPageHandler
        Dim dialog As New PrintDialog()
        dialog.Document = doc
        Dim print As DialogResult
        print = dialog.ShowDialog()

        doc.PrinterSettings = dialog.PrinterSettings

        Dim deviceInfo As String = _
        "<DeviceInfo>" & _
        "<OutputFormat>emf</OutputFormat>" & _
        "  <PageWidth>8.5in</PageWidth>" & _
        "  <PageHeight>11in</PageHeight>" & _
        "  <MarginTop>0.25in</MarginTop>" & _
        "  <MarginLeft>0.25in</MarginLeft>" & _
        "  <MarginRight>0.25in</MarginRight>" & _
        "  <MarginBottom>0.25in</MarginBottom>" & _
        "</DeviceInfo>"

        Dim warnings() As Microsoft.Reporting.WinForms.Warning
        Dim streamids() As String
        Dim mimeType, encoding, filenameExtension, path As String
        mimeType = "" : encoding = "" : filenameExtension = ""

        'Input parameter report
        Dim DateFrom As Date = CDate("4/15/2015")
        Dim DateTo As Date = CDate("4/15/2015")

        Dim parmDateFrom As New ReportParameter("DateFrom", DateFrom)
        Dim parmDateTo As New ReportParameter("DateTo", DateTo)
       
Dim parmSO1(1) As ReportParameter
        parmSO1(0) = parmDateFrom
        parmSO1(1) = parmDateTo

       
Dim data() As Byte
        ReportViewer1.ServerReport.SetParameters(parmSO1)
        data = ReportViewer1.ServerReport.Render("Image", _
               deviceInfo, mimeType, encoding, filenameExtension, _
               streamids, warnings)
        pages.Add(New Metafile(New MemoryStream(data)))

        For Each pageName As String In streamids
            data = ReportViewer1.ServerReport.RenderStream("Image", _
                   pageName, deviceInfo, mimeType, encoding)
            pages.Add(New Metafile(New MemoryStream(data)))
        Next
        doc.Print()
        Me.ReportViewer1.RefreshReport()
    End Sub
End Class


Markicob, mari kita coba.Panggil form kemudian klik tombol print, dan dialog print langsung muncul. Kita tidak melihat preview dulu kan? Contoh di bawah ini Rani coba print nya pake PDF Printer.

Referensi:
http://blogs.msdn.com/b/bryanke/archive/2004/02/11/71491.aspx
https://monirblog.wordpress.com/2012/10/16/print-ssrs-report-directly-to-a-printer-without-viewing-using-vb-net/

Click here if you like this article.



Artikel Mengenai SSRS lainnya:

  1. Menginstall Business Intelligence SQL Server 2005
  2. Membuat Report menggunakan SQL Server Reporting Service (dengan wizard)
  3. Membuat Report Tanpa Wizard (Part 1)
  4. Membuat Report Tanpa Wizard (Part 2)
  5. Design Layout Report
  6. Filter/Parameter Report 
  7. Data source Report dari Stored Procedure
  8. Data Parameter dari Query Database 
  9. Membuat Group Report 
  10. Membuat Nomor Urut
  11. Cara Upload File Report (.rdl) Ke Report Server SQL Server Reporting
  12. Menghilangkan Page Break pada Email Subscription 
  13. List Subscription dengan Query 
  14. Mencetak Langsung ke Printer (Tanpa Preview) 

0 comments: