4

I have a report that I need to run multiple times and save as PDFs. I am currently generating the report as a PDF programatically but want to save the reports without the user having to choose the save option manually each time.

The code I use to render a single report as a PDF 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 was figuring I could run it in a loop and save the PDF each time.

hawbsl
  • 15,313
  • 25
  • 73
  • 114
monkeypushbutton
  • 196
  • 1
  • 9
  • 21

3 Answers3

6

What's your question in here? Is it that it does not work?

here is an example of something we didin 2005. We defined a control called rptViewer1 which can be visible or not depending of your needs. strFormat should contain "PDF" and strNomFicher the full path.

BTW the variable names and functions are in french, but that will work anyway :)


    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

David Brunelle
  • 6,528
  • 11
  • 64
  • 104
  • I need send to client in `Response` the report like ***application/pdf and application/vnd.ms-excel*** using ***bytes array*** with `HttpContext.Current.Response.BinaryWrite(renderedBytes)`. Is it possible or _I need to save the file to disk first,and then send to client?_ – Kiquenet Oct 15 '15 at 13:54
  • What is ***_stream*** variable ? static in class ? – Kiquenet Oct 15 '15 at 13:59
5

David's answer was very helpful to me. I thought I would publish my simplified and (slightly) translated version of this code, since the original contained some French, and also a few references that weren't 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
Brian MacKay
  • 31,133
  • 17
  • 86
  • 125
  • I use ***bytes array*** with `HttpContext.Current.Response.BinaryWrite(renderedBytes)` but sometimes I get **exception** _OutOfMemory_ Maybe first save file to filesystem in ASP.NET Server ? – Kiquenet Oct 15 '15 at 13:58
0
dt = c.ds.Tables["vt5"];
            ReportViewer1.Visible = true;
            ReportViewer1.LocalReport.ReportPath = "InvoiceSGST.rdlc";
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("Invoice", dt));
            ReportDataSource rds = new ReportDataSource("Invoice", dt);

            this.ReportViewer1.LocalReport.EnableExternalImages = true;
            ReportViewer1.LocalReport.Refresh();

            // Variables
            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;

            ReportViewer viewer = new ReportViewer();
            viewer.ProcessingMode = ProcessingMode.Local;
            viewer.LocalReport.ReportPath = "InvoiceSGST.rdlc";
            viewer.LocalReport.DataSources.Add(rds); // Add datasource here
            viewer.LocalReport.EnableExternalImages = true;
            byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
            if (bytes != null)
            {
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-length", bytes.Length.ToString());
                Response.BinaryWrite(bytes);


                string strFilePath = @"C:\Temp\";
                string strFileName = "Blah.PDF";
                string filename = Path.Combine(strFilePath, strFileName);
                using (FileStream fs = new FileStream(filename, FileMode.Create))
                {
                    fs.Write(bytes, 0, bytes.Length);
                }
            }
Sibi Babu
  • 9
  • 2