http://www.codeproject.com/KB/aspnet/ASP_MVC_Reporting.aspx
Not quite what you are looking for? You may want to try: Generate Popup PDF Forms with ASP.NET MVC and Open Office Rendering PDF views in ASP MVC using iTextSharp
8,201,852 members and growing! (40,213 online)
highlights off
Member 3975385
301
Sign out
Home
Articles
Quick Answers
Discussions
Learning Zones
Features
Help!
The Lounge
MVC 3 PDF
See Also
More like this More by this author
A description and solution for creating PDF reports using ASP.NET MVC3.
Article Browse Code Stats Revisions (2)
4.53 (11 votes) 15
Sponsored Links
Essential PDF Easily generate richly-formatted Adobe PDF files, even on systems that... www.syncfusion.com Essential Studio Reporting Edition Provide business users with the reports they need, in the formats they... www.syncfusion.com
Contents
Introduction Background Creating a report Create a controller that derives from PdfViewController Create a Razor view that generates the HTML Create an action which calls the ViewPDF method Trigger the action on the controller Detailed Reporting Project overview Rendering an ASP.NET MVC3 view into a string Convert the HTML string into a PDF byte array Send the byte array back to the client Points of interest Colors New page support Images ASP.NET support History
See Also...
Introduction
Almost every web application (that I have built) needs some kind of reporting. Many times the client wants to generate a PDF report that corresponds with the web page he or she is viewing. Using iTextSharp, a free C# PDF library, this is possible. This article describes and includes a solution for creating reports using iTextSharp and ASP.NET MVC3.
Background
iTextSharp is a free C# PDF library that is ported from the Java-PDF library iText. iText was launched in 2000 and is a popular Open Source Java library for programmatic creation and manipulation of PDF.
1 de 5
27/10/2011 12:16
http://www.codeproject.com/KB/aspnet/ASP_MVC_Reporting.aspx
As with many successful Open-Source Java libraries, it was ported to C# under the name iTextSharp. iTextSharp has been under development since 2008 and is distributed under the GNU Affero General Public License version 3. At its core, iTextSharp is an API to manipulate PDF creation. For a recent project, I was looking for a solution that would enable me to create PDF documents from existing HTML documents or Views used in an ASP.NET MVC3 project. The optimal solution for me would be to throw an existing ASP.NET MVC3 view at it and let it generate a PDF that is exactly the same as the HTML rendered by a browser. The solution described here uses the HTMLWorker class from iTextSharp to generate a PDF from an HTML view. HTMLWorker is an iTextSharp class that is able to parse an HTML document and generate a PDF using what is called the SimpleParser of iTextSharp. Notice the name SimpleParser which indicates that not all HTML elements and CSS styles are supported. However, I was able to get good results using this solution. To add reporting to your ASP.NET MVC3 project, first add the PDFReportGenerator project to your solution and add the iTextSharp.dll binary to your solution. PDFReportGenerator needs a reference to the binary. By adding the binary to the solution, you are able to build the project directly from source control. Check the demo source for an example.
Creating a report
Follow these steps to generate an actual report from your web application: 1. 2. 3. 4. Create Create Create Create a controller that derives from PdfViewController. a view that generates the HTML which should be translated to a PDF report. an action on a controller which calls the ViewPDF method on the PdfViewController. a link to trigger the action on the controller.
@using MvcReportGeneratorDemo.Models @model CustomerList <br /> <table cellpadding="3" cellspacing="3"> <tr border="1" bgcolor="#777777" color="#ffffff"> <td>Name</td> <td>Address</td> <td>Place</td> </tr> @foreach (Customer customer in Model) { <tr border="1"> <td>@customer.Name</td> <td>@customer.Address</td> <td>@customer.Place</td> </tr> } </table>
Parameters
pageTitle
Type: System.String The title of the report which appears on the header of the page.
viewName
Type: System.String The name of the view which should be converted to a report.
2 de 5
27/10/2011 12:16
http://www.codeproject.com/KB/aspnet/ASP_MVC_Reporting.aspx
model
Type: System.Object The model that is rendered by the view. This methods generates the HTML view and converts it into a PDF report and sends this PDF as a binary stream back to the client. This means that when the client has a PDF plug-in installed, the PDF is shown inside the browser. From an action inside your controller, this method should be called to generate the report and send it to the client. The following action from the demo application generates the PDF. "Customer report" is the title of the report, "PrintDemo is the name of the view, and the model is returned by the CreateCustomerList() which as its name implies generates a dummy list with customers.
Collapse | Copy Code
The last step is to create a link on a page that calls this action to actually print the report.
These are the steps you need to be able to create PDF reports from your ASP.NET MVC3 projects. Read the next part of the article if you are interested in the details of how the PdfReportGenerator actually converts the ASP.NET MVC3 view into a report.
The first argument is viewName which is the name of the view that should get rendered to a string including the model viewData that is needed by the view. The controller is necessary to be able to use to render the view using the view engine of ASP.NET MVC.
3 de 5
27/10/2011 12:16
http://www.codeproject.com/KB/aspnet/ASP_MVC_Reporting.aspx
htmlText is the rendered view as a string that was produced by the HtmlViewRender; pageTitle is,
as the name suggests, the title of the report.
protected ActionResult ViewPdf(string pageTitle, string viewName, object model) { // Render the view html to a string. string htmlText = this.htmlViewRenderer.RenderViewToString(this, viewName, model); // Let the html be rendered into a PDF document through iTextSharp. byte[] buffer = standardPdfRenderer.Render(htmlText, pageTitle); // Return the PDF as a binary stream to the client. return new BinaryContentResult(buffer, "application/pdf"); }
Points of interest
Colors
iTextsharp supports colors out of the box; in the demo application, the background colors of the rows are alternated using different colors. These colors are visible in the report.
Images
It is possible to add images to the report by using an <img src="" /> tag. I had no success with dynamically generated images. So I generated the images that I needed before the action conversion process took place.
ASP.NET support
It should be possible to use the same kind of solution using older versions of ASP.NET MVC. However, I have not tried it.
History
26/09/2011 First version.
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
4 de 5
27/10/2011 12:16
http://www.codeproject.com/KB/aspnet/ASP_MVC_Reporting.aspx
Article Top
S earch Layout
Normal
Per page
10
U pdat e
Refresh objectstudio Jonathan Fernndez Mehul Harry Patrick Kalkman Seth Juarez mornedc Patrick Kalkman albertoleon Patrick Kalkman bdaniel7
First Prev Next 16hrs 21mins ago 6:26 20 Oct '11 14:09 7 Oct '11 10:42 10 Oct '11 14:32 10 Oct '11 22:07 3 Oct '11 1:59 4 Oct '11 22:55 27 Sep '11 0:38 28 Sep '11 6:48 27 Sep '11 1 2 Next
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Permalink | Advertise | Privacy | Mobile | Web23 | 2.5.111017.1 Layout: fixed | fluid Article Copyright 2011 by Patrick Kalkman Everything else Copyright CodeProject, 1999-2011 Terms of Use
5 de 5
27/10/2011 12:16