Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Page History: PDF Generation via RDLC Report File

Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: Fri, Jan 21, 2011, 9:10 AM


.NET 2.0

public static void FromRdlcFile(string rdlcFileName, string dataSourceName, DataTable inputData, 
    HttpResponse response)
{
    //- Initialize Report Viewer Control ------------------------------------------------------
    ReportViewer rv = new ReportViewer();
    LocalReport r = rv.LocalReport;
    r.EnableExternalImages = true;
    r.ReportPath = "Reports/" + rdlcFileName;
    r.DataSources.Clear();
    ReportDataSource rds = new ReportDataSource(dataSourceName, inputData);
    r.DataSources.Add(rds);

    //- Render Report in PDF ------------------------------------------------------------------
    Warning[] warnings;
    string mimeType, encoding, fileNameExtension;
    string[] streams;

    byte[] bytes = r.Render("PDF", "", out mimeType, out encoding, out fileNameExtension, out 
                            streams, out warnings);

    int size = bytes.GetUpperBound(0) + 1;

    char[] c = new char[size];

    bytes.CopyTo(c, 0);

    //- Write PDF Contents to HTTP Response ---------------------------------------------------
    //response.Clear();
    response.ContentType = "Application/pdf";
    response.Write(c, 0, size);
    response.End();
}

.NET 4.0

private void RenderAsPdf(string rdlcFileName, string dataSourceName, DataTable 
    inputData, HttpResponse response)
{
    //- Initialize Report Viewer Control --------------------------------------------------
    ReportViewer rv = new ReportViewer();
    LocalReport r = rv.LocalReport;
    r.EnableExternalImages = true;
    r.ReportPath = "Reports/" + rdlcFileName + ".rdlc";
    r.DataSources.Clear();
    ReportDataSource rds = new ReportDataSource(dataSourceName, inputData);
    r.DataSources.Add(rds);

    //- Render Report in PDF --------------------------------------------------------------
    Warning[] warnings;
    string mimeType, encoding, fileNameExtension;
    string[] streams;

    byte[] bytes = r.Render("PDF", null, out mimeType, out encoding, out fileNameExtension,  
                            out streams, out warnings);

    int size = bytes.GetUpperBound(0) + 1;

    char[] c = new char[size];

    bytes.CopyTo(c, 0);

    //- Write PDF Contents to HTTP Response -----------------------------------------------
    response.Buffer = true;
    response.Clear();
    response.ContentType = mimeType;
    response.AddHeader("content-disposition", "attachment; filename=" + rdlcFileName + "." 
        + fileNameExtension);
    response.BinaryWrite(bytes);
    response.Flush();
    response.End();
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.