Page 1 of 1

Exporting Report to PDF and Sending as Email Attachment from Website

Posted: Thu Sep 13, 2007 11:57 pm
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?

Exporting Report to PDF and Sending as Email Attachment from Website

Posted: Fri Sep 14, 2007 4:53 am
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.

Exporting Report to PDF and Sending as Email Attachment from Website

Posted: Sun Sep 16, 2007 3:48 pm
by focus_nz
Thanks, that worked.

Exporting Report to PDF and Sending as Email Attachment from Website

Posted: Mon Sep 17, 2007 2:00 am
by Edward
Let us know if you need any help.

Thank you.

Exporting Report to PDF and Sending as Email Attachment from Website

Posted: Sun Feb 07, 2010 10:56 am
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:

Exporting Report to PDF and Sending as Email Attachment from Website

Posted: Mon Feb 08, 2010 1:20 pm
by Jan
Hello,

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

Thank you.