Active5 years, 2 months ago
I would like to render an RDLC report in HTML within an ASP.NET MVC project.
In this article I will explain with an example, how to export RDLC Report to PDF without using ReportViewer in ASP.Net with C# and VB.Net. Database Here I am making use of Microsoft’s Northwind Database. Up vote 76 down vote accepted. This is easy to do, you can render the report as a PDF, and save the resulting byte array as a PDF file on disk. To do this in the background, that's more a question of how your app is written. You can just spin up a new thread, or use a BackgroundWorker (if this is a WinForms app), etc. SQL Reporting:: Render PDF / Print Without Reportviewer? I have created many reports in my ASP.Net 3.5 app's and using VB Code-behind. I have the following code that will take the report from the reportviewer and on a click of a button, export it to PDF. You can render to PDF by calling ReportViewer.LocalReport.Render() and specifying 'PDF' for the format. Then this byte stream can be used any way you want in. Byte[] bytes = viewer.LocalReport.Render('PDF', null, out mimeType, out encoding, out extension, out streamIds, out warnings); // Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
I successfully made a prototype that renders an RDLC report in PDF, Excel, and TIFF image, with the help of this article. But I was surprised that HTML is not one of the default available formats in
LocalReport.Render()
.I came across this article, which describes a trick to enable the rendering format of HTML4.0, but I think that is only for a
ReportViewer
control (I could be wrong though).The question is, in MVC how to render an RDLC report in HTML just like a
William NiuWilliam NiuReportView
does (see the screenshot below)?![Localreport render Localreport render](/uploads/1/2/6/0/126068206/784393629.jpg)
14.1k77 gold badges4646 silver badges9191 bronze badges
3 Answers
Reportviewer Page Size
This is a simple task. You can follow the following steps.
- Create a folder in your solution and give a name Reports.
- Add a ASP.Net web form and named it ReportView.aspx
- Create a Class ReportData and add it to the Reports folder. Add the following codeto the Class.
- Add another Class and named it ReportBasePage.cs. Add the following code in this Class.
- Add ScriptManager to the ReportView.aspx page. Now Take a Report Viewer to the page. In report viewer set the property AsyncRendering='false'. The code is given below.
- Add two NameSpace in ReportView.aspx.cs
- Change the System.Web.UI.Page to ReportBasePage. Just replace your code using the following.
- Add a Folder to the Reports Folder and named it Report. Now add a RDLC report to the Reports/Report folder and named it ClosingInventory.rdlc.
- Now add a Controller and Named it ReportController. In to the controller add the following action method.
- Add a view page click on the ReportViewer Controller. Named the view page ReportViewer.cshtml. Add the following code to the view page.
- Add an Iframe. Set the property of the Iframe as follows
- Add Following JavaScript to the viewer.
- In Web.config file add the following key to the appSettings section add
- In system.web handlers Section add the following keyadd
verb='*' path='Reserved.ReportViewerWebControl.axd' type = 'Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
- Change your data source as your own. This solution is very simple and I think every one enjoy it.
14.1k77 gold badges4646 silver badges9191 bronze badges
Md. Nazrul IslamMd. Nazrul Islam
You can use the ReportViewer object to render an RDLC to PDF or HTML. For my case (below) I wanted a PDF document and I returned it as a FileContentResult ActionResult. If you want it to return as a download use the File ActionResult (I've commented that out for your use).
![Pdf Pdf](/uploads/1/2/6/0/126068206/357826811.png)
2,72622 gold badges1919 silver badges4141 bronze badges
I put this project together ages ago http://mvcrdlc.codeplex.com/
From memory there should be an html render of the rdlc as well as the PDF
PeterPeter4,79788 gold badges5252 silver badges8585 bronze badges
Not the answer you're looking for? Browse other questions tagged asp.net-mvcreportingreportviewerrdlclocalreport or ask your own question.
Re: PDF or Print a .rdlc report without a reportviewer
May 06, 2010 02:17 AM|contactrajkumar|LINK
Hi,
This code is working fine for me by using itextsharp,
Render .rdlc To Pdf Without Reportviewer Control
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Web.UI.Page
Imports System.Web.HttpResponse
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html
Imports iTextSharp.text.html.simpleparser
Imports System.Text
Imports System
Imports System.Data
Imports System.Web.UI.Page
Imports System.Web.HttpResponse
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html
Imports iTextSharp.text.html.simpleparser
Imports System.Text
Public Class ClsPDF
Public Function funPrintPDFfile(ByVal ds As DataSet, ByVal response As HttpResponse, ByVal sTitle As String, ByVal sFileName As String) As Boolean
Dim dt_data As DataSet = ds
'Dim doc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35)
Dim pdftable As Table = New Table(ds.Tables(0).Columns.Count)
Dim font1 As iTextSharp.text.Font = FontFactory.GetFont('ARIAL', 7, False, 7, 1, iTextSharp.text.Color.BLACK)
Dim font2 As iTextSharp.text.Font = FontFactory.GetFont('ARIAL', 7, False, 10, 1, iTextSharp.text.Color.BLACK)
Try
If ds.Tables(0).Rows.Count > 0 Then
'Dim pdftable As Table = New Table(ds.Tables(0).Columns.Count)
Dim PdfPCell As Cell = Nothing
'Add Header of the pdf table
pdftable.Spacing = 5.0F
For i As Integer = 0 To ds.Tables(0).Columns.Count - 1
PdfPCell = New Cell(New Phrase(New Chunk(Replace(dt_data.Tables(0).Columns(i).ColumnName, '-', ' '), font2)))
PdfPCell.BackgroundColor = iTextSharp.text.Color.GRAY
pdftable.AddCell(PdfPCell)
Next
For rows As Integer = 0 To ds.Tables(0).Rows.Count - 1
For column As Integer = 0 To ds.Tables(0).Columns.Count - 1
PdfPCell = New Cell(New Phrase(New Chunk(ds.Tables(0).Rows(rows)(column).ToString(), font1)))
pdftable.AddCell(PdfPCell)
Next
Next
pdftable.Spacing = 5.0F
'doc.Open()
'doc.Add(pdftable)
End If
Catch dex As DocumentException
'Handle document exception
Catch ioex As IOException
'Handle IO exception
Catch ex As Exception
'Handle Other Exception
Finally
'Close document
'doc.Close()
'Response.Redirect('~/Test11.pdf')
Dim pdfDoc As New Document(PageSize.A4.Rotate, 10.0F, 10.0F, 10.0F, 0.0F)
PdfWriter.GetInstance(pdfDoc, response.OutputStream)
'Dim wri As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream(Request.PhysicalApplicationPath + 'GridViewExport.pdf', FileMode.Create))
pdfDoc.Open()
'pdfDoc.Add(img)
Dim para As Paragraph = New Paragraph(Date.Today.ToString('dd/MM/yyyy'))
para.SetAlignment('right')
para.Font.Size = 8
pdfDoc.Add(para)
para = New Paragraph(sTitle)
para.SetAlignment('center')
para.Font.Color = iTextSharp.text.Color.BLUE
para.Font.Size = 14
pdfDoc.Add(para)
pdfDoc.Add(pdftable)
pdfDoc.Close()
response.ContentType = 'application/pdf'
response.AddHeader('content-disposition', 'attachment;filename=' & sFileName & '.pdf')
response.Cache.SetCacheability(HttpCacheability.NoCache)
response.Write(pdfDoc)
response.End()
End Try
Return True
End Function
End Class
Public Function funPrintPDFfile(ByVal ds As DataSet, ByVal response As HttpResponse, ByVal sTitle As String, ByVal sFileName As String) As Boolean
Dim dt_data As DataSet = ds
'Dim doc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35)
Dim pdftable As Table = New Table(ds.Tables(0).Columns.Count)
Dim font1 As iTextSharp.text.Font = FontFactory.GetFont('ARIAL', 7, False, 7, 1, iTextSharp.text.Color.BLACK)
Dim font2 As iTextSharp.text.Font = FontFactory.GetFont('ARIAL', 7, False, 10, 1, iTextSharp.text.Color.BLACK)
Try
If ds.Tables(0).Rows.Count > 0 Then
'Dim pdftable As Table = New Table(ds.Tables(0).Columns.Count)
Dim PdfPCell As Cell = Nothing
'Add Header of the pdf table
pdftable.Spacing = 5.0F
For i As Integer = 0 To ds.Tables(0).Columns.Count - 1
PdfPCell = New Cell(New Phrase(New Chunk(Replace(dt_data.Tables(0).Columns(i).ColumnName, '-', ' '), font2)))
PdfPCell.BackgroundColor = iTextSharp.text.Color.GRAY
pdftable.AddCell(PdfPCell)
Next
For rows As Integer = 0 To ds.Tables(0).Rows.Count - 1
For column As Integer = 0 To ds.Tables(0).Columns.Count - 1
PdfPCell = New Cell(New Phrase(New Chunk(ds.Tables(0).Rows(rows)(column).ToString(), font1)))
pdftable.AddCell(PdfPCell)
Next
Next
pdftable.Spacing = 5.0F
'doc.Open()
'doc.Add(pdftable)
End If
Catch dex As DocumentException
'Handle document exception
Catch ioex As IOException
'Handle IO exception
Catch ex As Exception
'Handle Other Exception
Finally
'Close document
'doc.Close()
'Response.Redirect('~/Test11.pdf')
Dim pdfDoc As New Document(PageSize.A4.Rotate, 10.0F, 10.0F, 10.0F, 0.0F)
PdfWriter.GetInstance(pdfDoc, response.OutputStream)
'Dim wri As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream(Request.PhysicalApplicationPath + 'GridViewExport.pdf', FileMode.Create))
pdfDoc.Open()
'pdfDoc.Add(img)
Dim para As Paragraph = New Paragraph(Date.Today.ToString('dd/MM/yyyy'))
para.SetAlignment('right')
para.Font.Size = 8
pdfDoc.Add(para)
para = New Paragraph(sTitle)
para.SetAlignment('center')
para.Font.Color = iTextSharp.text.Color.BLUE
para.Font.Size = 14
pdfDoc.Add(para)
pdfDoc.Add(pdftable)
pdfDoc.Close()
response.ContentType = 'application/pdf'
response.AddHeader('content-disposition', 'attachment;filename=' & sFileName & '.pdf')
response.Cache.SetCacheability(HttpCacheability.NoCache)
response.Write(pdfDoc)
response.End()
End Try
Return True
End Function
End Class
Create a class file and call funPrintPDFfile function and pass corresponding parameters,its working fine for me.
Please remember to click “Mark as Answer” on the post that helps you, it will help other(s) to get there answer.