none
Local Report Rendering to PDF RRS feed

  • Question

  • Hi Guys,

    I have an ASP app that Renders a PDF and then export's to client machine.

    Now I am not sure if it's the code or cross browser issue, but the only browser it works perfectly is Firefox...

    Here is my Code:

    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.Reporting.WebForms;
    
    namespace TNTWEB
    {
        public partial class ReportExport : System.Web.UI.Page
        {
            public string Parm;
            public string Key;
            public string TypeKey;
            public string KeyVal;
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.User.Identity.IsAuthenticated)
                {
                    Response.Redirect(System.Web.Security.FormsAuthentication.LoginUrl);
                }
                Key = Request.QueryString["Type"];
                Parm = Request.QueryString["Parm"];
    
                if (Key == "1")
                {
                    int Parm1 = Convert.ToInt32(Parm);
                    using (LocalReport lr = new LocalReport())
                    {
                        lr.ReportPath = HttpContext.Current.Server.MapPath("~/Local Reports/GRN.rdlc");
                        App_Data.uecdeczaDataSetTableAdapters.spGRNDataTableAdapter ta1 = new App_Data.uecdeczaDataSetTableAdapters.spGRNDataTableAdapter();
                        ReportDataSource ds1 = new ReportDataSource("DS", (DataTable)ta1.GetData(Parm1));
                        lr.DataSources.Add(ds1);
                        Microsoft.Reporting.WebForms.Warning[] warnings;
                        string[] streamids;
                        string mimeType;
                        string encoding;
                        string filenameExtension;
                        byte[] bytes;
                        bytes = lr.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
    
                        using (MemoryStream s = new MemoryStream(bytes))
                        {
                            s.Seek(0, SeekOrigin.Begin);
                            Response.ContentType = "application/pdf";
                            Response.AppendHeader("content-disposition", "render; filename=GRN" + Parm1 + " " + System.DateTime.Now.ToShortDateString() + ".pdf");
                            Response.BinaryWrite(bytes);
                            Response.Flush();
                            Response.Close();
                        }
                    }
                }
                Response.Close();
            }
        }
    }

    If Anyone has any advice to do this in a better way, please let me know.....

    For more info on this: MyASPQuestion

    Any Advice will be greatly Appreciated

    Thanks And Regards

    Jacques


    ‎"Be polite, be professional, but have a plan to kill everybody you meet." Maj. Gen. James Mattis

    Monday, June 18, 2012 6:27 AM

Answers

  • By Adding my Content-Disposition as Attachment solved this issue, as well as turning caching off in IIS.

    See the Code Below :)

      ReportDataSource ds1 = new ReportDataSource("DS", (DataTable)ta1.GetData(Parm1));
                            lr.DataSources.Add(ds1);
                            Microsoft.Reporting.WebForms.Warning[] warnings;
                            string[] streamids;
                            string mimeType;
                            string encoding;
                            string filenameExtension;
                            byte[] bytes;
                            lr.Refresh();
                            bytes = lr.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids,
                                              out warnings);
                            
                            Page.Response.Buffer = false;
                            Page.Response.BufferOutput = false;
                            Page.Response.Clear();
                            Page.Response.ClearContent();
                            Page.Response.ClearHeaders();
                            Response.AppendHeader("content-disposition", "attachment; filename=report.pdf");
                            Response.ContentType = "application/pdf";
                            using (MemoryStream memoryStream = new MemoryStream(bytes))
                            {
                                memoryStream.Seek(0, SeekOrigin.Begin);
                                var streamBytes = memoryStream.ToArray();
                                Response.BinaryWrite(streamBytes);
                            }
                            Response.End();

    Regards

    Jacques


    ‎"Be polite, be professional, but have a plan to kill everybody you meet." Maj. Gen. James Mattis

    • Marked as answer by Jacques444 Tuesday, June 26, 2012 6:30 AM
    Tuesday, June 26, 2012 6:30 AM

All replies

  • A better (easier) way is probably Crystal Report which is provided with the extended versions of Visual Studio. 

    However it is not anymore supported by Microsoft.

    In Crystal Report is a standard output to PDF.

    http://scn.sap.com/community/crystal-reports-for-visual-studio/content?filterID=content~objecttype~objecttype[thread]


    Success
    Cor

    Monday, June 18, 2012 6:48 AM
  • Just wanted to make sure whether adobe acrobat reader plugin is there for IE on your local machine if it's only working in firefox.

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Monday, June 18, 2012 7:34 AM
  • Yes the Reader plugin is there, because the report does show, but only the headers. No data is populated in the report.


    ‎"Be polite, be professional, but have a plan to kill everybody you meet." Maj. Gen. James Mattis

    Monday, June 18, 2012 7:41 AM
  • By Adding my Content-Disposition as Attachment solved this issue, as well as turning caching off in IIS.

    See the Code Below :)

      ReportDataSource ds1 = new ReportDataSource("DS", (DataTable)ta1.GetData(Parm1));
                            lr.DataSources.Add(ds1);
                            Microsoft.Reporting.WebForms.Warning[] warnings;
                            string[] streamids;
                            string mimeType;
                            string encoding;
                            string filenameExtension;
                            byte[] bytes;
                            lr.Refresh();
                            bytes = lr.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids,
                                              out warnings);
                            
                            Page.Response.Buffer = false;
                            Page.Response.BufferOutput = false;
                            Page.Response.Clear();
                            Page.Response.ClearContent();
                            Page.Response.ClearHeaders();
                            Response.AppendHeader("content-disposition", "attachment; filename=report.pdf");
                            Response.ContentType = "application/pdf";
                            using (MemoryStream memoryStream = new MemoryStream(bytes))
                            {
                                memoryStream.Seek(0, SeekOrigin.Begin);
                                var streamBytes = memoryStream.ToArray();
                                Response.BinaryWrite(streamBytes);
                            }
                            Response.End();

    Regards

    Jacques


    ‎"Be polite, be professional, but have a plan to kill everybody you meet." Maj. Gen. James Mattis

    • Marked as answer by Jacques444 Tuesday, June 26, 2012 6:30 AM
    Tuesday, June 26, 2012 6:30 AM