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

print my report straight to pdf

18 Answers 849 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
rob mays
Top achievements
Rank 1
rob mays asked on 14 Jun 2007, 03:45 PM
Instead of having my web report open up in the report viewer. Can i have some C# code on how to export my report straight to open a PDF file on the clients browser? Please.

18 Answers, 1 is accepted

Sort by
0
Chavdar
Telerik team
answered on 14 Jun 2007, 04:38 PM
Hi Rob,

You can use the following code snippet to programmatically export the report into "PDF" format from a Web Site or Web Application project:

void ExportToPDF(string reportName, Telerik.Reporting.Report reportToExport)    
    {    
        string mimeType = string.Empty;    
        string ext = string.Empty;    
        Encoding encoding = Encoding.Default;    
    
        byte[] reportBytes = Telerik.Reporting.Processing.ReportProcessor.Render(    
            "PDF"    
            , reportToExport    
            , null    
            , out mimeType    
            , out ext    
            , out encoding);    
    
            
        string fileName = reportName + ".pdf";    
    
        Response.Clear();    
        Response.ContentType = mimeType;    
        Response.Cache.SetCacheability(HttpCacheability.Private);    
        Response.Expires = -1;    
        Response.Buffer = false;    
    
        Response.AddHeader("Content-Disposition",    
                           string.Format("{0};FileName=\"{1}\"",    
                                         "attachment",    
                                         fileName));    
    
        Response.OutputStream.Write(reportBytes, 0, reportBytes.Length);            
        Response.End();    
    }    
 
 


For the Web Site you may need to manually specify the folder in which the "PDF" rendering extension is located using this code line: 

    Telerik.Reporting.Processing.ReportProcessor.ExtensionDir = HttpContext.Current.Server.MapPath("~/bin");

Also, make sure that Telerik.Reporting.ImageRendering.dll resides in the Bin folder of the application.


Best wishes,
Chavdar
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
rob mays
Top achievements
Rank 1
answered on 14 Jun 2007, 05:02 PM
many thanks for the excellent reply. firtst class forum. just what i needed
0
NightFlash
Top achievements
Rank 1
answered on 18 Jun 2007, 11:34 AM
where do I have to past that code, could be usefull for me aswell.
0
rob mays
Top achievements
Rank 1
answered on 19 Jun 2007, 07:29 AM
can i have some more info please, dont fully understand what you waqnt to do.
0
NightFlash
Top achievements
Rank 1
answered on 19 Jun 2007, 07:46 AM
I also want to export my report straight to a PDF file. Chavder gave you the code for that, but how do I have to use the code, where do I have to past it. And how do I have to link it? Because it's just a normal function, so you have to link it somewhere.

Thnx already,
Kevin
0
rob mays
Top achievements
Rank 1
answered on 19 Jun 2007, 08:04 AM
what I did was in a new aspx page in the page load I create an instance of the report in the page load event, then in the page load event I call the ExportToPDF function passing it the name I give the report, then the actual report as the second parameter. If you are in the uk give me a number and i can help you more if needed.
0
NightFlash
Top achievements
Rank 1
answered on 19 Jun 2007, 08:20 AM
ok, thanks mate! I'm going to try it over an hour! I live in Belgium so I think that would be a little bit too expensive :) I let you know something in 1,5hour!
0
rob mays
Top achievements
Rank 1
answered on 19 Jun 2007, 08:23 AM
if you get stuck let me know and i will past you my c# code on how i did it
0
NightFlash
Top achievements
Rank 1
answered on 19 Jun 2007, 09:47 AM
Ok! I think I find out how it works. But it's not like I want :( Because I work like this:

I have a page where you can select some options, this I forward in a querystring to another page where I request this querystring. On that page I request data from my database with those variables. And after that I bind it to the report. And with the script bellow you can't send variables :(

Any ideas mate?
0
rob mays
Top achievements
Rank 1
answered on 19 Jun 2007, 09:54 AM
what is the name name of your report? first try to get it to display in the telerik reportviewer? r u able to do that?
here is my code in the page load event. there are no controls on this page what so ever.

ReportLib.Plumbworld.rptDeliveryNotices.PassDeliveryDate = Session["DeliveryDate"].ToString();

ReportLib.Plumbworld.rptDeliveryNotices rpt = new ReportLib.Plumbworld.rptDeliveryNotices();

ExportToPDF("Deliveries", rpt);

0
NightFlash
Top achievements
Rank 1
answered on 19 Jun 2007, 10:02 AM
Nice writen script! But what is this (red, underline)

ReportLib.Plumbworld.rptDeliveryNotices.PassDeliveryDate = Session["DeliveryDate"].ToString();

I work like this, the code I show is on the page load of my reportviewer: 

//here I request my data
VerhuurGeschDA
verhuurGeschDA = new VerhuurGeschDA();

VerhuurGeschData data = verhuurGeschDA.GetDataNietBetaald("Kevin", Request.Querystring["var1"].ToString());

//here I attach my data to my reportviewer
ReportViewer1.Report = (Telerik.Reporting.
Report)new VerhuurRapport();

ReportViewer1.Report.DataSource = data;

 

0
Haderach
Top achievements
Rank 1
answered on 22 Jun 2007, 08:04 AM
Hello,

I try the code given by Chavdar on 14tyh June... and I get this message :

"sys.webforms.pagerequestmanagerparsererrorexception the message received from the server could not be parsed ... ...  ... "

I think it is due to the fact that the linkbutton event where I call the method is not making a regular PostBack because this linkbutton is linked to a RadAjaxManager Prometheus....

Do you have any idea to help me ?

Thank for your help,
0
rob mays
Top achievements
Rank 1
answered on 22 Jun 2007, 08:16 AM

//here I request my data
VerhuurGeschDA verhuurGeschDA = new VerhuurGeschDA();

VerhuurGeschData data = verhuurGeschDA.GetDataNietBetaald("Kevin", Request.Querystring["var1"].ToString());

//here I attach my data to my reportviewer
VerhuurRapport myReport = new VerhuurRapport();

//the export to pdf dont need report viewer
ExportPDF("YourReport",myReport)

0
Chavdar
Telerik team
answered on 22 Jun 2007, 02:35 PM
Hi Emmanuel,

You are right that the ajaxification of the LinkButton is causing the trouble. The browser's build-in XmlHttpRequest object doesn't interpret correctly the content type of the response and there is nothing we can do to fix this behavior. Given this you have the following choices: 

  • To remove the LinkButton from the ajaxified controls list and allow it do regular postbacks
  • To use Respose.Redirect in the Click event handler and redirect to a page that can serve the report:
      
    protected void LinkButton1_Click1(object sender, EventArgs e)
    {            
        Response.Redirect("ExportReport.aspx");
    }

     
    and in the Page_Load event of the ExportReport.aspx have something like this:
     
    protected void Page_Load(object sender, EventArgs e)
    {
        this.ExportToPDF("Report1", new Report1());
    }
     
  • Use javascript code to manually make a new request to the report page.

For your convenience I have attached several sample pages which show the approached mentioned above. Let me know if you have any other questions.

 
Regards,

Chavdar

the Telerik team


Instantly find answers to your questions at the new Telerik Support Center
0
thulasiram
Top achievements
Rank 1
answered on 29 Feb 2008, 05:12 AM
Hi
i need to export to pdf in landscape format. please suggest me on this requirement.

Thanks in Advance
0
thulasiram
Top achievements
Rank 1
answered on 29 Feb 2008, 06:22 AM
Hi

i need to generate PDF in landscape format. any suggestion on this requirement will be a great help.

Thanks in advance
0
Svetoslav
Telerik team
answered on 29 Feb 2008, 08:09 AM
Hi thulasiram,

All you need to do is to set the Report.PageSettings to the desired page orientation like this:

myReport.PageSettings.Landscape = True 

where myReport is Telerik.Reporting.Report class instance.

All the best,
Svetoslav
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
thulasiram
Top achievements
Rank 1
answered on 04 Mar 2008, 01:59 PM

Thanks. It is working.

Tags
General Discussions
Asked by
rob mays
Top achievements
Rank 1
Answers by
Chavdar
Telerik team
rob mays
Top achievements
Rank 1
NightFlash
Top achievements
Rank 1
Haderach
Top achievements
Rank 1
thulasiram
Top achievements
Rank 1
Svetoslav
Telerik team
Share this question
or