Save RDLC reports as PDF

I have a report that I need to run several times and save as PDF files. I am currently creating a report in PDF format, but I want to save the reports without having to select the option to save manually each time.

The code that I use to create a separate report in PDF format is:

Dim warnings As Microsoft.Reporting.WebForms.Warning() = Nothing Dim streamids As String() = Nothing Dim mimeType As String = Nothing Dim encoding As String = Nothing Dim extension As String = Nothing Dim deviceInfo As String Dim bytes As Byte() Dim lr As New Microsoft.Reporting.WebForms.LocalReport deviceInfo = "<DeviceInfo><SimplePageHeaders>True</SimplePageHeaders></DeviceInfo>" bytes = ReportViewer1.LocalReport.Render("PDF", deviceInfo, mimeType, encoding, extension, streamids, warnings) Response.ClearContent() Response.ClearHeaders() Response.ContentType = "application/pdf" Response.BinaryWrite(bytes) Response.Flush() Response.Close() 

I realized that I could run it in a loop and save the PDF every time.

+4
source share
2 answers

What is your question? Does it really not work?

Here is an example of what we did in 2005. We have defined the rptViewer1 control, which may or may not be visible to your needs. strFormat must contain "PDF" and strNomFicher the full path.

By the way, variable names and functions are in French, but it will still work :)

     Public Sub CreerFichierRapport (ByVal strNomFichier As String, ByVal strFormat As String)
         Dim bytes () As Byte
         Dim strDeviceInfo As String = ""
         Dim strMimeType As String = ""
         Dim strEncoding As String = ""
         Dim strExtension As String = ""
         Dim strStreams () As String
         Dim warnings () As Warning
         Dim oFileStream As FileStream
         _stream = New List (Of Stream)
         Try
             bytes = rptViewer1.LocalReport.Render (strFormat, strDeviceInfo, strMimeType, strEncoding, strExtension, strStreams, warnings)

             oFileStream = New FileStream (strNomFichier, FileMode.Create)
             oFileStream.Write (bytes, 0, bytes.Length)
             _stream.Add (oFileStream)
         Finally
             If Not IsNothing (oFileStream) Then
                 oFileStream.Close ()
                 oFileStream.Dispose ()
             End if
         End try
     End sub

+6
source

David's answer was very helpful to me. I thought that I would publish my simplified and (slightly) translated version of this code, as the original contained some French as well as some links that were not relevant:

 Imports Microsoft.Reporting.WebForms Imports System.IO Public Class RenderToPDF Public Sub Save(ByVal viewer As ReportViewer, ByVal savePath As String) Dim Bytes() As Byte = viewer.LocalReport.Render("PDF", "", Nothing, Nothing, Nothing, Nothing, Nothing) Using Stream As New FileStream(savePath, FileMode.Create) Stream.Write(Bytes, 0, Bytes.Length) End Using End Sub End Class 
+5
source

All Articles