print my report straight to pdf

19 posts, 0 answers
  1. rob mays
    rob mays avatar
    58 posts
    Member since:
    Jun 2007

    Posted 14 Jun 2007 Link to this post

    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.
  2. Chavdar
    Admin
    Chavdar avatar
    898 posts

    Posted 14 Jun 2007 Link to this post

    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
  3. DevCraft banner
  4. rob mays
    rob mays avatar
    58 posts
    Member since:
    Jun 2007

    Posted 14 Jun 2007 Link to this post

    many thanks for the excellent reply. firtst class forum. just what i needed
  5. NightFlash
    NightFlash avatar
    50 posts
    Member since:
    Mar 2007

    Posted 18 Jun 2007 Link to this post

    where do I have to past that code, could be usefull for me aswell.
  6. rob mays
    rob mays avatar
    58 posts
    Member since:
    Jun 2007

    Posted 19 Jun 2007 Link to this post

    can i have some more info please, dont fully understand what you waqnt to do.
  7. NightFlash
    NightFlash avatar
    50 posts
    Member since:
    Mar 2007

    Posted 19 Jun 2007 Link to this post

    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
  8. rob mays
    rob mays avatar
    58 posts
    Member since:
    Jun 2007

    Posted 19 Jun 2007 Link to this post

    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.
  9. NightFlash
    NightFlash avatar
    50 posts
    Member since:
    Mar 2007

    Posted 19 Jun 2007 Link to this post

    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!
  10. rob mays
    rob mays avatar
    58 posts
    Member since:
    Jun 2007

    Posted 19 Jun 2007 Link to this post

    if you get stuck let me know and i will past you my c# code on how i did it
  11. NightFlash
    NightFlash avatar
    50 posts
    Member since:
    Mar 2007

    Posted 19 Jun 2007 Link to this post

    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?
  12. rob mays
    rob mays avatar
    58 posts
    Member since:
    Jun 2007

    Posted 19 Jun 2007 Link to this post

    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);

  13. NightFlash
    NightFlash avatar
    50 posts
    Member since:
    Mar 2007

    Posted 19 Jun 2007 Link to this post

    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;

     

  14. Haderach
    Haderach avatar
    45 posts
    Member since:
    Dec 2006

    Posted 22 Jun 2007 Link to this post

    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,
  15. rob mays
    rob mays avatar
    58 posts
    Member since:
    Jun 2007

    Posted 22 Jun 2007 Link to this post

    //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)

  16. Chavdar
    Admin
    Chavdar avatar
    898 posts

    Posted 22 Jun 2007 Link to this post

    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
  17. thulasiram
    thulasiram avatar
    3 posts
    Member since:
    Jan 2008

    Posted 28 Feb 2008 Link to this post

    Hi
    i need to export to pdf in landscape format. please suggest me on this requirement.

    Thanks in Advance
  18. thulasiram
    thulasiram avatar
    3 posts
    Member since:
    Jan 2008

    Posted 29 Feb 2008 Link to this post

    Hi

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

    Thanks in advance
  19. Svetoslav
    Admin
    Svetoslav avatar
    649 posts

    Posted 29 Feb 2008 Link to this post

    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
  20. thulasiram
    thulasiram avatar
    3 posts
    Member since:
    Jan 2008

    Posted 04 Mar 2008 Link to this post

    Thanks. It is working.

Back to Top
DevCraft banner