'Theres a problem with the file format' when exporting report to PDF to attach in Email message

2 posts, 1 answers
  1. Chris
    Chris avatar
    7 posts
    Member since:
    Mar 2014

    Posted 11 Dec 2014 Link to this post


    I am having difficulty writing a method that will render my report as a PDf so that I can attach it to an e-mail. Here is my code:

    public bool SendReportViaEmail(string reportName)
        // TODO verify user access to report
            var reportProcessor = new ReportProcessor();
            var report = new Report {DocumentName = HttpContext.Current.Server.MapPath(string.Format("{0}{1}.trdx", FileUtility.ReportPath, reportName))};
            var instanceReportSource = new InstanceReportSource {ReportDocument = report};
            instanceReportSource.Parameters.Add(new Parameter("userId", 3));
            instanceReportSource.Parameters.Add(new Parameter("eventId", 0));
            instanceReportSource.Parameters.Add(new Parameter("exceptionId", 14));
            instanceReportSource.Parameters.Add(new Parameter("employeeId", 3860));
            var result = reportProcessor.RenderReport("PDF", instanceReportSource, new Hashtable());
            var fileName = report.Name + "." + result.Extension;
            var path = Path.GetTempPath();
            var filePath = Path.Combine(path, fileName);
            var mailMessage = new MailMessage {From = new MailAddress(ConfigurationManager.AppSettings["fromAddress"])};
            using (var fs = new FileStream(filePath, FileMode.Create))
                fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
                fs.Seek(0, SeekOrigin.Begin);
                var a = new Attachment(fs, report.Name + "." + result.Extension);
                mailMessage.To.Add(new MailAddress("cbohatka@aztekweb.com"));
                mailMessage.Subject = "TEST";
                var smtpClient = new SmtpClient();
        catch (Exception ex)
        return true; // dummy return

    I am receiving a PDF attached to my e-mail but it is smaller than expected and display an error message.

    Thanks in advance!
  2. Answer
    Nasko avatar
    1049 posts

    Posted 16 Dec 2014 Link to this post

    Hello Chris,

    The error message displayed in the generated PDF file is caused by this part of the code:
    var report = new Report {DocumentName = HttpContext.Current.Server.MapPath(string.Format("{0}{1}.trdx", FileUtility.ReportPath, reportName))};

    The Report.DocumentName Property above is not used correctly. The value of this property is used to suggest a file name for the exported file when exporting a report to one of the available export formats. It cannot be used to create a report object from a TRDX file. The latter can be achieved by deserializing the TRDX file.

    Instead of deserializing a TRDX file to a report object and creating an InstanceReportSource, you can use the TRDX file directly with the UriReportSource. Example code can be found in the UriReportSource Class page. The different types of report sources and their usage is explained in the Report Sources help article.


    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

Back to Top