Converting Razor views into Pdf

By Dawid on (tags: convert, generate, pdf, razor, categories: tools, code)

This post talks about how you can convert Razor views into PDF. There is couple of NuGet packages which we can use to achieve that. I will try to describe couple of them.

This library allows us to create documents in Portable Document Format (PDF). Some of the features:
- generate documents and reports based on either provided HTML or by manually adding PDF elements such as text, paragraphs, tables and many more
- PDF manipulation: stamping watermarks, merging and splitting PDF files
- PDF form filling
- server dynamic generated or manipulated PDF documents to a web browser
Library can be easily used in ASP.NET MVC in couple of ways. Bellow you will find couple of examples how we can use library.

Using XMLWorkerHelper to convert HTML to the PDF:

Manual creation of PDF elements:

Example usage in ASP.NET MVC:

More details about iTextSharp can be found here: http://itextpdf.com/ and here: https://github.com/itext/itextsharp

The HiQPdf Library for .NET offers you a modern, simple, fast, flexible and powerful tool to create complex and stylish PDF documents in your applications with just a few lines of code.
Using the high quality HTML to PDF conversion engine you can easily design a document in HTML with CSS3, JavaScript, SVG or Canvas and then convert it to PDF preserving the exact content and style.
Some of most important features according to their website:
- fast and precise HTML to PDF Conversion Technology
- support for all modern HTML5, CSS3, SVG and Canvas features
- support for Web Fonts and Web Open Font Format (WOFF)
- convert HTML to PDF, to transparent PNG or to SVG vector image
- create, edit, merge and split PDFs, fill and submit PDF forms
- absolutely no external dependencies, direct copy deployment

Basics usage is quite simple:

EVO HTML to PDF Converter library for .NET can be easily integrated into any type of .NET application, either ASP.NET web sites, Windows Forms and WPF applications or Windows Azure Cloud services to convert URLs, HTML strings and streams, SVG vector graphics to a PDF document or to an image. You can use the converter as a general purpose tool for converting web pages and HTML code to PDF documents and images or you can use it as a nice tool to instantly create nicely formatted and easily maintainable PDF reports directly from existing HTML reports.
The converter offers full support for HTML tags, CSS styles, SVG vector graphics, page breaks control with CSS styles, automatically repeated HTML table header on each PDF page, live URLs and internal links in PDF, automatically generated bookmarks, HTML in the headers and footers, Unicode and right to left text,  PDF merge, split and edit.
We can easily integrate this library into our ASP.NET MVC web application like that:

And here is nice way how we can render Razor view into HTML which we can convert later on:

There seems to be one problem with that library – poor support. I’ve found couple of entries where we can read that support organisation it’s the best and thy aren’t giving any feedback on reported bugs.
More information and full list of featues can be found here: http://www.evopdf.com/html-to-pdf-converter.aspx

Select.Pdf for .NET is a professional PDF library that can be used for creating, writing, editing, handling and reading PDF files without any external dependencies within .NET applications. Using this .NET PDF library, you can implement rich capabilities to create PDF files from scratch or process existing PDF documents entirely through C#/VB.NET without installing Adobe Acrobat.
Many rich features can be supported by the .NET PDF API, such as security setting (including digital signatures), PDF merge/split, text, html and image drawing into PDF and many more. Besides, Select.Pdf for .NET component can be used to easily convert HTML to PDF with C#/VB.NET in high quality.
Some examples:

More information here: http://selectpdf.com/pdf-library-for-net/

There is couple more of libraries which we can use to convert Razor views into PDF. I won’t describe each of them because usage of those libraries is very similar. What can I say is few words about licencing:
- EVO HTML to PDF Converter for .NET – here we can use quite interesting Company License which can be used by an unlimited number of developers, in an unlimited number of applications that can be deployed on any number of computers.
- HiQPdf – something similar here called Enterprise Licence. It allows any number of developers to develop any number of applications using our software. There are no run time or deployment costs and the license never expires. The license price includes the software upgrades and Enterprise Technical Support for the first year.
- Select.PDF – there is quite similar licence called Enterprise OEM License but it’s a little bit more expensive. What is interesting here that there is an option to use free Community Edition. The free html to pdf converter offers most of the features the professional sdk offers, the only notable limitation is that it can only generate PDF documents up to 5 pages long.

If you are looking for good library which can be used by unlimited number of developers to develop any number of applications then I would suggest HiGPdf. Very fast library and function complete. Cheaper then EVO and Select.PDF libraries.