#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. 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:
- Create a controller that derives from
- Create a view that generates the HTML which should be translated to a PDF repor
- Create an action on a controller which calls the
ViewPDFmethod on the
- Create a link to trigger the action on the controller
##Create a controller that derives from PdfViewController
Create a new or reuse an existing controller and let it derive from
PdfViewController from the PdfReportGenerator project. This enables your controller to call
ViewPDF method of the
PDFViewController which generates the actual PDF. In the demo project, this is the
##Create a view that generates the HTML
Create a view that should be translated to a report. This could be an existing view or a new view specially for reporting. I usually create a new view as it lets me control
the HTML markup for the report. As stated earlier, the report generator does not support all the HTML markup. In the demo project, this is the
Below, the PrintDemo view from the demo project is shown. As can be seen, this is just a simple ASP.NET Razor view with a table and some rows.
It uses a strongly typed model but that is not necessary. A tip when trying to design your report is to add borders to your
Using these borders, when looking at the generated PDF, you can clearly see the start and end of the areas of your report.
##Create an action which calls the ViewPDF method
PdfViewController class from which your controller derives contains a
ViewPDF method. This method has the following signature:
Parameters | pageTitle | The title of the report which appears on the header of the page | viewName| The name of the view which should be converted to a report | model| 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.
The last step is to create a link on a page that calls this action to actually print the report.
##Trigger the action on the controller
A simple method to create a link to trigger the action on the controller is by using an
ActionLink. This link calls the action that we defined on the controller.
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.
#Detailed Reporting Project overview The PdfReportGenerator project consist of six classes which can be seen in the image below. The PdfReportGenerator assembly works by rendering the ASP.NET MVC3 view into a string and converting this string with HTML using the iTextSharp into a PDF report.
#Rendering an ASP.NET MVC3 view into a string
HtmlViewRenderer is responsible for rendering the ASP.NET view into a string. The method
RenderViewToString has the following signature.
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.
##Convert the HTML string into a PDF byte array
Once we have the HTML in a string,
StandardPdfRenderer converts the HTML string into a PDF byte array. The class
StandardPdfRenderer has a method
Render with the following signature.
htmlText is the rendered view as a string that was produced by the
pageTitle is, as the name suggests, the title of the report.
##Send the byte array back to the client as a stream
The last step is to convert the byte array into an instance of the
BinaryContentResult class. The class
BinaryContentResult derives from the ASP.NET MVC
ActionResult. It overrides
ExecuteResult and returns the content as a binary stream.
PdfViewController is the class that combines these classes. The method
ViewPdf uses all the three previously mentioned classes to generate the PDF as shown in the code below:
This is in the controller.
#Points of interest Some remarks on using iTextSharp
##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.
##New page support
One thing that I needed with my project that was not supported by the HTML conversion in iTextSharp was functionality to force a page break. Most of the time, with reporting, you need to be able to force a page break, for example, if you want a graph to start always on a new page. The way I solved this was to add support for it to iTextSharp. As it is Open-Source, you are able to add new features to it. I added support for a non-existing HTML tag called
which forces iTextSharp to create a new page. I created a patch so that it could be committed to the iTextSharp project.I do not know if it will be included in the main trunk of the project as it feels somewhat strange to invent new HTML tags just to support a page break. But if you need it, you can use the patch to compile iTextSharp with new page support.
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 generate the images that I needed before the action conversion process takes place. A single static image can be seen in the report.
##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.