This is a migrated thread and some comments may be shown as answers.

C# Export To PDF

7 Answers 1035 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Steven
Top achievements
Rank 1
Steven asked on 06 May 2011, 06:02 PM
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!

7 Answers, 1 is accepted

Sort by
0
Steven
Top achievements
Rank 1
answered on 09 May 2011, 10:12 AM
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.
0
Datamex
Top achievements
Rank 2
answered on 09 May 2011, 03:17 PM
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
0
Steven
Top achievements
Rank 1
answered on 09 May 2011, 03:28 PM
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.
0
kachy
Top achievements
Rank 1
answered on 23 Aug 2011, 01:54 PM
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.

0
David Billingham
Top achievements
Rank 1
answered on 24 Aug 2011, 04:58 PM
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
0
kachy
Top achievements
Rank 1
answered on 24 Aug 2011, 06:05 PM
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.
0
Fred Green
Top achievements
Rank 1
answered on 01 Mar 2018, 05:00 PM
Try using ZedPDF.com
It helped me a lot!

Best regards

Fred Green
Tags
General Discussions
Asked by
Steven
Top achievements
Rank 1
Answers by
Steven
Top achievements
Rank 1
Datamex
Top achievements
Rank 2
kachy
Top achievements
Rank 1
David Billingham
Top achievements
Rank 1
Fred Green
Top achievements
Rank 1
Share this question
or