HTML-to-PDF with .NET


Code Examples

Frameworks supported

  • .NET 4.0 or later
  • Windows Phone 7.1 (Mango)



Command line:

nuget.exe install DocRaptor

Package Manager Console:

Install-Package DocRaptor

Download DLLs: get DocRaptor.dll from GitHub


See examples for runnable examples with file output, error handling, etc.

using DocRaptor.Client;
using DocRaptor.Model;
using DocRaptor.Api;
using System.IO;

class Example {
  static void Main(string[] args) {
    Configuration.Default.Username = "YOUR_API_KEY_HERE";
    DocApi docraptor = new DocApi();

    Doc doc = new Doc(
      test: true,                                                    // test documents are free but watermarked
      documentContent: "<html><body>Hello World</body></html>",      // supply content directly
      // documentUrl: "",  // or use a url
      name: "docraptor-csharp.pdf",                                  // help you find a document later
      documentType: Doc.DocumentTypeEnum.Pdf                         // pdf or xls or xlsx
      // Javascript: true,                                           // enable javaScript processing
      // princeOptions: new PrinceOptions(
      //   media: "screen",                                          // use screen styles instead of print styles
      //   baseurl: ""                               // pretend URL when using document_content
      // )

    byte[] create_response = docraptor.CreateDoc(doc);

Docs created like this are limited to 60 seconds to render, check out the async example which allows 10 minutes.

We have guides for doing some of the common things:

With DocRaptor, it's fast and painless to convert HTML, CSS, and JavaScript into PDF and XLS documents with .NET. Below are working .NET examples for creating documents. Our API reference lists all the generation options and our style and formatting guide will help make it look perfect.