C# Export To PDF

7 posts, 0 answers
  1. Steven
    Steven avatar
    4 posts
    Member since:
    Jan 2011

    Posted 06 May 2011 Link to this post

    Hi Guys,

    I'm hoping this is an easy thing to fix but I've had a play and a search and can't figure it out!

    What I need to do in my system is generate a report, save it to my harddrive as a PDF so that I can then attach it to an email. This all needs to be done programatically. I found your example and have been trying to get it to work but for some reason (I expect I'm missing something obvious) it doesn't like the "response" bit of the code below:

        RepQuote quote_rep = new RepQuote(_qu);
     
    Telerik.Reporting.Processing.ReportProcessor reportProcessor = new ReportProcessor();
    RenderingResult result = reportProcessor.RenderReport("PDF", quote_rep, null);
     
    string fileName = result.DocumentName + ".pdf";      
          
    Response.Clear();
    Response.ContentType = result.MimeType;
    Response.Cache.SetCacheability(HttpCacheability.Private);
    Response.Expires = -1;
    Response.Buffer = true;
     
    Response.AddHeader("Content-Disposition",
    string.Format("{0};FileName=\"{1}\"",
      "attachment",
       fileName));
     
    Response.BinaryWrite(result.DocumentBytes);
    Response.End();

    I'm guessing im just missing a "using" statement but the code wont compile past "Response.Clear()". It can't find the response class. I did a search around and people suggested adding "using System.Web" to get it to work but that didn't help. I'm a relative newbie with C# so any help would be appreciated!
  2. Steven
    Steven avatar
    4 posts
    Member since:
    Jan 2011

    Posted 09 May 2011 Link to this post

    Anybody able to help? I'm sure other people much have saved a report to disk programatically in the past using C#! I'm not sure whether it makes much difference, but i'm building my application using Visual Studio 2008.
  3. DevCraft banner
  4. Datamex
    Datamex avatar
    161 posts
    Member since:
    Jun 2012

    Posted 09 May 2011 Link to this post

    AFAIK, you have to use 

    Telerik.Reporting.Processing.RenderingResult result = reportProcessor.RenderReport("PDF", quote_rep, null);

    That works for me.

    HTH for you,

    Regards,
    Datamex
  5. Steven
    Steven avatar
    4 posts
    Member since:
    Jan 2011

    Posted 09 May 2011 Link to this post

    Right, managed to get it all working, heres the code for those who are interested:

    Aim - Generate a new report, save it to a temporary directory, automatically attach it to an email (by passing the path into email object)

    Code -

    RepQuote quote_rep = new RepQuote(_qu); // creates the report
     
    Telerik.Reporting.Processing.ReportProcessor reportProcessor = new ReportProcessor();
    RenderingResult result = reportProcessor.RenderReport("PDF", quote_rep, null);  // renders the report as a PDF, stored in variable 'result'
     
    string filePath = System.IO.Path.GetTempPath();
    string fileName = "random_filename.PDF";
    string full_path = filePath + fileName; //this is where the PDF will be saved to temporarily
     
    using (FileStream fs = new FileStream(full_path, FileMode.Create))
    {
        fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);  // writes the pdf to disk
    }
     
    be.quotePDFfilepath = full_path;  // passes the path of the pdf to my email object which then uses this info later on

    If anyone else happens to stumble across this code and fancies using it, just beware that every time you create a new document using this code, it will overwrite the previous file. It is best to generate a unique filename but for our software our client wanted a specified filename. Luckily we only have 1 user of this section of the system so it wont be an issue for us.
  6. kachy
    kachy avatar
    89 posts
    Member since:
    Aug 2005

    Posted 23 Aug 2011 Link to this post

    Hi,

    I am trying to export my report to PDF programatically and I get this error 'Entry point was not found.' on the code line

     

    RenderingResult result = reportProcessor.RenderReport("PDF", report1, null);

    I searched for the error 'Entry point was not found.' and it says that if there is some kind of mismatch in the report version then it happens but for my machine same report runs in the reportviewer.

    I also wanted to add that I am using

    private void Report1_NeedDataSource(object sender, EventArgs e)

    When I use the reportviewer then the needdatasource event runs but when I use the reportProcessor.RenderReport then the needdatasource event does not run. I have few public variables in the report which I am using to pass the parameter to the report.

    Can somebody help me with this urgently?

    Thanks.

  7. David Billingham
    David Billingham avatar
    5 posts
    Member since:
    Sep 2009

    Posted 24 Aug 2011 Link to this post

    Hi

    The data you need to save to disk is in "result.DocumentBytes".

    Edit the code you posted to remove all references to/use of the response object as this is only relevanmt when rendering the document back to a web page.

    You then simply need to add code to save the byte array in "result.DocumentBytes" to disk. Try using a BinaryWriter object.

    HTH

    David
  8. kachy
    kachy avatar
    89 posts
    Member since:
    Aug 2005

    Posted 24 Aug 2011 Link to this post

    Finally it turned out to be installation problem. When I could not figure out what to do, I uninstalled Telerik Report and reinstalled. Now export to PDF is working fine.

    Thanks.
Back to Top
DevCraft banner