Exporting Report to PDF and Sending as Email Attachment from Website

Stimulsoft Reports.NET discussion
Post Reply
focus_nz
Posts: 4
Joined: Wed Sep 12, 2007 11:34 pm
Location: New Zealand

Exporting Report to PDF and Sending as Email Attachment from Website

Post by focus_nz »

I am displaying a report using the web viewer control and when a button on the page is clicked I want it to generate a pdf document from the report and send it as an attachment. I am using .NET Framework 2.0 and version 2007.2 of Stimulsoft Reports.

I want to be able to export the report to pdf and send it as an attachment without having to save the pdf to disk first.

Below is my code so far:

Code: Select all

    Private Sub SendReport()
        Dim stream As IO.MemoryStream
        Dim smtp As Mail.SmtpClient
        Dim item As Mail.MailMessage
        Dim attachment As Mail.Attachment
        Dim report As Stimulsoft.Report.StiReport
        Try
            'Get Report
            report = StiWebViewer.Report

            'Create Mail Message
            item = New Mail.MailMessage("", "")
            item.Subject = "Test Report"
            item.Body = "Attached is a report."

            'Create Attachment
            stream = New IO.MemoryStream
            report.ExportDocument(Stimulsoft.Report.StiExportFormat.Pdf, stream)
            attachment = New Mail.Attachment(stream, "MyReport.pdf", "application/pdf")
            item.Attachments.Add(attachment)

            'Create SMTP Client
            smtp = New Mail.SmtpClient
            smtp.Host = ""
            smtp.Send(item)

        Catch ex As Exception
            lblError.Text = "An error occured while trying to generate report."
        End Try
    End Sub
At the moment the email is sent with a pdf attached but the filesize is 64 bytes instead of 33,000 bytes and can't be opened by adobe acrobat.

Any ideas on how I best go about this?
Edward
Posts: 2913
Joined: Fri Jun 09, 2006 4:02 am

Exporting Report to PDF and Sending as Email Attachment from Website

Post by Edward »

Please try to change your code as the following:

Code: Select all

report.ExportDocument(Stimulsoft.Report.StiExportFormat.Pdf, stream)
stream.Seek(0, SeekOrigin.Begin);
attachment = New Mail.Attachment(stream, "MyReport.pdf", "application/pdf")
The rest of your code seems to be ok.

Thank you.
focus_nz
Posts: 4
Joined: Wed Sep 12, 2007 11:34 pm
Location: New Zealand

Exporting Report to PDF and Sending as Email Attachment from Website

Post by focus_nz »

Thanks, that worked.
Edward
Posts: 2913
Joined: Fri Jun 09, 2006 4:02 am

Exporting Report to PDF and Sending as Email Attachment from Website

Post by Edward »

Let us know if you need any help.

Thank you.
fuhrj
Posts: 120
Joined: Wed Jun 11, 2008 12:51 pm
Location: Lancaster, Ohio

Exporting Report to PDF and Sending as Email Attachment from Website

Post by fuhrj »

I wanted to follow-up this thread with a helpful tip if you want to email the report without displaying it.

On my page, I am not using the WebViewer control. I just want to give the option to the user to send their report via email.

You need to make sure that you add report.Render(); before the report.ExportDocument. Otherwise, your report will be empty:

Code: Select all

             StiReport report = new StiReport();
             report.Load(filePath);             
             report.Compile();
             report.Render();

             MemoryStream ms = new MemoryStream();
             report.ExportDocument(StiExportFormat.Pdf, ms);
             ms.Seek(0, SeekOrigin.Begin);
             System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(ms, "Receipt.pdf", "application/pdf");

             [etc - your mail handler here]

Hope this helps someone in the future :biggrin:
Jan
Posts: 1265
Joined: Thu Feb 19, 2009 8:19 am

Exporting Report to PDF and Sending as Email Attachment from Website

Post by Jan »

Hello,

Thank you for your information. It will be helpful for our customers!

Thank you.
Post Reply