pashov.net/code|April 2015

Monthly Archives: April 2015

.NET Coding

A really good pdf library for .net

Published by:

If you are into Web apps development you will probably face at least once the problem to export web content to pdf. And if it is the first time you face that problem, things get scary. You usually face a few options:

– commercial and expensive library
– the almighty wkhtmltopdf tool (built on top of WebKit rendering engine) but you have to run the executable from your .net app which makes deployment more complex
– a .net wrapper library of the wkhtmltopdf tool

The third one is definitely the winning option as the tool is really powerful and free. Which wrapper though? Some of them don’t work in some cases and docs are poor. After an extensive research i found this one called NReco PDF Generator for .NET to be the best and render everything you might need (html of course, but also svg, custom fonts etc.) in one liner. The library is free but for 30$ you can get some support and code samples and for 100$ you can get all future updates plus component’s source if you want. As i said it works perfect outside of the box and chances are you wont need samples and support unless you render something very special. It the most basic scenario, you need only

var converter = new HtmlToPdfConverter();
var result = converter.GeneratePdf(html);

where html is… right, your html that you want to export. If you need any customization you may find useful to use:

converter.PageFooterHtml="your html as string goes here"

or

converter.PageHeaderHtml="your html as string goes here"

If you want some more granular control you may use :

converter.CustomWkHtmlPageArgs

property which takes native to the wkhtmltopdf tool arguments as a string so if you need page numbers in the footer you may type in:

converter.CustomWkHtmlPageArgs ="--footer-right [page]/[topage] --footer-font-size 8";

I saved the best for last – it is available through NuGet and has no dependencies so installation is one-click away and makes a deployment as consistent as it can get. So to wrap up , good job guys for that library. It is very rare to find something free to work so well outside of the box.