EPiServer  /  CMS December 09, 2012

Building a PDF Channel for EPiServer 7

Channels in EPiServer 7 is an exciting new feature that can be used for a variety of purposes. While it may not be the first use case that comes to mind we can use channels to enable rendering of pages on a site as PDF documents.

Last Thursday at the Stockholm EPiServer Meetup I demoed a quick and easy way to create a PDF channel for an EPiServer 7 site using the open source library Rotativa. While it may not be sexiest use case for channels it may actually be useful for some sites. Not to mention that it’s fun to be able to surf around on the site in PDF format while in edit mode. Here’s how to build it.

The channel

Channels in EPiServer 7 couldn’t be much easier to define. We simply create a class inheriting from DisplayChannel and implement two abstract methods.

//using System.Web;
//using EPiServer.Web;

public class PdfChannel : DisplayChannel
    public override bool IsActive(HttpContextBase context)
        return context.Request.QueryString["pdf"] == 1.ToString();

    public override string ChannelName
        get { return "PDF"; }

The above class will add a new channel to a site which will be selectable in edit mode. It will also be active if there’s a query string parameter named pdf with a value of 1 in the URL. That is, no matter which page is being requested it will be active if the query string parameter is there.

The controller

Rotativa is a nice little library which wraps the larger wkhtmltopdf project which can be used to generate PDF documents from HTML markup through the use of WebKit. Rotativa is easily downloadable from NuGet.

As Rotativa is built for being used with ASP.NET MVC we’ll add a renderer for the PDF channel in the form of an MVC controller. With EPiServer 7 doing so will work no matter if the rest of the site is built using MVC or Web Forms.

//using System.Web.Mvc;
//using EPiServer.Core;
//using EPiServer.Framework.DataAnnotations;
//using EPiServer.ServiceLocation;
//using EPiServer.Web.Mvc;
//using EPiServer.Web.Routing;
//using Rotativa;

[TemplateDescriptor(Inherited = true, Tags = new [] { "pdf" })]
public class PdfController : PageController<PageData>
    public ActionResult Index(PageData currentPage)
        var url = ServiceLocator.Current

        return new UrlAsPdf(url)
            FormsAuthenticationCookieName = ".EPiServerLogin"

The above controller simply receives the current page, figures out its URL in an MVC and Web Forms agnostic way and returns a UrlAsPdf result which is an action result type added by Rotativa. To ensure that the PDF generator will be able to reach the page even in edit mode when using ASP.NET MVC we alse ensure that it can use the correct authentication cookie.

The result – any page as PDF

With the channel and renderer in place we’re now able to select the PDF channel in edit mode to preview any page as a PDF document. Public visitors are also able to get any page as PDF by adding pdf=1 to the query string. Of course, in a real world scenario we’d still have the bulk of work in front of us if we wanted the channel to be useful as we’d probably want to use a separate stylesheet. Nevertheless, here’s how it may look in edit mode.


PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.

Joel Abrahamsson

Joel Abrahamsson

I'm a passionate web developer and systems architect living in Stockholm, Sweden. I work as CTO for a large media site and enjoy developing with all technologies, especially .NET, Node.js, and ElasticSearch. Read more


comments powered by Disqus

My book

Want a structured way to learn EPiServer 7 development? Check out my book on Leanpub!

More about EPiServer CMS