I’m working on a .NET Framework 4.7.2 project using the Class Library template. I’m trying to generate a PDF report using RDLC, but the PDF file is generated without displaying any content, not even static data.
RDLC Report Setup:
I created an RDLC report file (SummonsReport.rdlc) with static text and images to ensure that some content should be displayed.
Report Generation Code:
[HttpGet]
[Route(“GenerateSummonsReports”)]
public HttpResponseMessage GenerateSummonsReports(long session_id)
{
try
{
// Fetch the data
IList summonsData = new Summons().GetSummonsForPrint(session_id);
Debug.WriteLine($”Fetched {summonsData.Count} summons records.”);
// Create a DataSet and DataTable
DataSet dataSet = new DataSet("SummonTemplateDataset");
DataTable dataTable = new DataTable("DataTable_Summon");
// Add columns to DataTable
dataTable.Columns.Add("citizen_id", typeof(long));
dataTable.Columns.Add("entity_id", typeof(long));
dataTable.Columns.Add("name", typeof(string));
dataTable.Columns.Add("contact_number", typeof(string));
dataTable.Columns.Add("address", typeof(string));
dataTable.Columns.Add("date_of_birth", typeof(DateTime));
dataTable.Columns.Add("profession", typeof(string));
dataTable.Columns.Add("session_id", typeof(long));
dataTable.Columns.Add("status_id", typeof(long));
dataTable.Columns.Add("summon_file", typeof(string));
// Populate DataTable with data
foreach (var summon in summonsData)
{
dataTable.Rows.Add(
summon.citizen_id,
summon.entity_id,
summon.name,
summon.contact_number,
summon.address,
summon.date_of_birth,
summon.profession,
summon.session_id,
summon.status_id,
summon.summon_file
);
}
// Add DataTable to DataSet
dataSet.Tables.Add(dataTable);
// Path to your RDLC report file
string reportPath = HostingEnvironment.MapPath("~/Reports/SummonsReport.rdlc");
// Check if the file exists at the specified path
if (!File.Exists(reportPath))
{
throw new FileNotFoundException($"Report file not found at path: {reportPath}");
}
// Initialize LocalReport
LocalReport localReport = new LocalReport
{
ReportPath = reportPath
};
// Set the data source for the current report
ReportDataSource reportDataSource = new ReportDataSource("SummonTemplateDataset", dataTable);
localReport.DataSources.Clear();
localReport.DataSources.Add(reportDataSource);
// Render the report to a byte array
string reportType = "PDF";
string mimeType;
string encoding;
string fileNameExtension;
string[] streams;
Warning[] warnings;
byte[] renderedBytes = localReport.Render(
reportType,
null,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
// Log warnings if any
if (warnings != null && warnings.Length > 0)
{
foreach (var warning in warnings)
{
Debug.WriteLine($"Warning: {warning.Message}");
}
}
// Return the PDF report
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(renderedBytes)
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = $"SummonsReport_{session_id}.pdf"
};
return response;
}
catch (Exception ex)
{
Debug.WriteLine($"Exception: {ex.Message}");
return new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent($"Error generating report: {ex.Message}")
};
}
}
Result:
The PDF file is created successfully, but it does not display any content. The PDF is blank.
What I’ve Tried:
I verified that the RDLC file is correctly set up with static text and images.
I confirmed that the ReportPath is correct.
I tried adding different data sources, but the result remains the same (blank PDF).
I’ve checked for any runtime errors, but there are none.
Environment Details:
.NET Framework: 4.7.2
Template: Class Library
Tools: Visual Studio 2022
Abhay Kamble is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.