Click here to Skip to main content
Click here to Skip to main content

Create PDF from html using C#, Nustache,Mustache and Pechkin

, 18 Aug 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This article explains how to create a PDF file using C# object


Recently, I had to work on a task where we need to create a PDF version of a Silverlight page.The data to the markup (xaml) was from a C# object (yes, you guessed it! we used MVVM pattern) and we wanted to use that object for the PDF file.Below are the components we used to create a PDF file.


As mentioned above you need

  • Nustache library
  • Pechkin

I have excluded these Dlls in the sample code because one of the dll,wkhtmltox0, of Pechkin package is about 29 MB resulting the attachment bigger.So, you will get build error if you just open and hit F5 (The source code can be opened in either VS 2010 or VS 2012).

Installing dependencies

Using Nuget package manager

Manual installation

You can also download the .zip version of these and manually add reference to your project.In this case you should do the following settings in order to make the code work

Add reference

Add these files to the root of the project and set 'copy if newer'

update web.config (or App.config in case you use it in Console)

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
        <bindingRedirect oldVersion="" newVersion="" />


Using the code

The sample solution has three projects: 1.WCF service 2. Silverlight app 3. Web app




The class PdfService offers two methods.

PdfBytes - accepts any string content and convert as a PDF bytes to the client.

EmployeePdf - specific to Employee object and it converts the employee data into a html string which then be passed to the PdfBytes method to get PDF bytes.

    public class PdfService : IPdfService
        public PdfResult PdfBytes(string content)
            var result = new PdfResult();
                var oc = new ObjectConfig();
                oc.SetPrintBackground(true); //to apply the background colors in the pdf
                var footer = oc.Footer;
                footer.SetTexts("For internal use only", string.Empty, string.Empty);
                result.Bytes = new SynchronizedPechkin(new GlobalConfig()).Convert(oc, content);
            catch (Exception ex)
                result.Error = ex.Message;
            return result;
        public PdfResult EmployeePdf(Employee employee)
            string nustachTemplate = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "App_Data", "EmployeeTemplate.html");
            var employeeHtml = Render.FileToString(nustachTemplate, employee);
            return PdfBytes(employeeHtml);

The Employee object's data are injected into the Mustache html template. You can apply styles (only inline styles, style sheets cannot be linked) to design the page output.

    <title>Employee Print</title>
        body {
            margin: 5px;
        h3 {
            text-align: center;
            border-radius: 10px;
        h4 {
            background: silver;
            border-radius: 5px;
            padding-left: 10px;
    <h3>Employee {{Name}}</h3>
    <h4>Basic Details</h4>
  <p>No skills</p>
  <p>No hobbies</p>
  <p>{{Company}}: as a {{Role}} for {{NoOfYears}} years.</p>
  <p>No jobs</p>

And the Employee class

    public class Employee
        public Employee()
            Hobbies = new List<Hobby>();
            Skills = new List<Skill>();
            Jobs = new List<Job>();
        public string Name { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public List<Hobby> Hobbies { get; set; }
        public List<Skill> Skills { get; set; }
        public List<Job> Jobs { get; set; }

Here is the final PDF


A client application using the PDFService to generate Pdf. The code to call the service and save the file.

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
            var dialog = new SaveFileDialog()
                    DefaultExt = "Adobe PDF Files(*.pdf)",
                    Filter = "PDF (*.PDF)|*.PDF",
                    FilterIndex = 2

            if (dialog.ShowDialog() == true)
                var client = new PdfServiceClient();
                var emp = GetEmployee();
                client.EmployeePdfCompleted += (s, ea) =>
                    if (ea.Error != null)
                        MessageBox.Show(string.Format("Error:{0}", ea.Error.Message));
                    if (!string.IsNullOrWhiteSpace(ea.Result.Error))
                        MessageBox.Show(string.Format("Error:{0}", ea.Result.Error));
                    using (System.IO.Stream stream = dialog.OpenFile())
                        stream.Write(ea.Result.Bytes, 0, ea.Result.Bytes.Length);

                    MessageBox.Show("Pdf saved");


Pros and cons


  • Easy to build page content
  • We can CSS to style the output
  • No manual calculation of page count in PDF. Pages generated automatically based on the content
  • Free libraries


  • Dlls are big in size
  • Any other you may think of?

Points of Interest

The Nustache and Mustache template are not really a requirement for creating a PDF file. You can create without it just by passing a string or build your own html as far as it meets the requirement.I personally prefer Mustache as it is easy to build html,more importantly you can show or hide elements based on conditions while you have the full layout designed in HTML.

Before you download sample code

Again, the sample code will not work unless you follow the instructions mentioned in the Dependencies section in the top.


First version


I wanted to share my work on creating PDF and I did now.If somebody finds this interesting and can be of help then I will feel worth of it. As always, comments/suggestions are welcome.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Prabu ram
Technical Lead CGI
India India
No Biography provided
Follow on   LinkedIn

Comments and Discussions

GeneralMy vote of 2 Pinmembersilvia.stanila29-Oct-14 21:05 
Question"Cons: Any other you may think of?" PinmemberQuake2Player9-Dec-13 11:49 
AnswerRe: "Cons: Any other you may think of?" PinmemberPrabu ram9-Dec-13 18:37 
GeneralRe: "Cons: Any other you may think of?" PinmemberBeatrice Monk2-Nov-14 23:57 
SuggestionMessage Automatically Removed PinmemberMember 99341022-Oct-13 0:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 19 Aug 2013
Article Copyright 2013 by Prabu ram
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid