DocRaptor HTML TO PDF API

How to Convert HTML-to-PDF with DocRaptor

With DocRaptor's unique HTML to PDF API, powered by the Prince PDF converter, it's easy to convert HTML, CSS, and JavaScript into PDF and XLS documents. Below are working code examples for creating documents. Our API reference lists all the generation options and our style and formatting guides will help make it look perfect.

Select Your Language

Make a direct API request

Not using any of the above languages? Test the API in Postman or simply send a POST request to https://YOUR_API_KEY_HERE@api.docraptor.com/docs (yes, that key works!) with the below JSON payload:

{
  "test": true,                                                    // test documents are free but watermarked
  "document_content": "<html><body>Hello</body></html>",           // supply content directly
  // "document_url": "https://docraptor.com/examples/invoice.html", // or use a url
  "type": "pdf",                                                   // pdf or xls or xlsx
  // "javascript": true,                                           // enable JavaScript processing
  // "prince_options": {
  //   "media": "screen"                                           // use screen styles instead of print styles
  //   "baseurl": "http://hello.com"                               // pretend URL when using document_content
  // }
}

For more information, see our complete API reference.

curl Example

Here's an example of that POST request that you can copy and paste:

curl https://YOUR_API_KEY_HERE@api.docraptor.com/docs \
  --fail --silent --show-error \
  --header "Content-Type:application/json" \
  --data '{"test": true,
           "document_url": "https://docraptor.com/examples/invoice.html",
           "type": "pdf" }' > docraptor.pdf

Next Steps

PHP Installation

composer require docraptor/docraptor

Basic Usage

$docraptor = new DocRaptor\DocApi();
$docraptor->getConfig()->setUsername("YOUR_API_KEY_HERE");
// $docraptor->getConfig()->setDebug(true);

$doc = new DocRaptor\Doc();
$doc->setTest(true);                                                   // test documents are free but watermarked
$doc->setDocumentContent("<html><body>Hello World</body></html>");     // supply content directly
// $doc->setDocumentUrl("http://docraptor.com/examples/invoice.html"); // or use a url
$doc->setName("docraptor-php.pdf");                                    // help you find a document later
$doc->setDocumentType("pdf");                                          // pdf or xls or xlsx
// $doc->setJavascript(true);                                          // enable JavaScript processing
// $prince_options = new DocRaptor\PrinceOptions();                    // pdf-specific options
// $doc->setPrinceOptions($prince_options);
// $prince_options->setMedia("screen");                                // use screen styles instead of print styles
// $prince_options->setBaseurl("http://hello.com");                    // pretend URL when using document_content

$create_response = $docraptor->createDoc($doc);

Next Steps

  • Optionally store and get a URL for your converted document with document hosting
  • View more code examples with error handling, asynchronous creation, file saving, and document hosting.
  • Perfect your document styling with our style and formatting reference, and API reference. Easily add headers and footers, page breaks, page numbers, table of contents, and much more!

Python Installation

pip install --upgrade docraptor

or

easy_install --upgrade docraptor

If you are on a system with easy_install but not pip, you can use easy_install instead. If you're not using virtualenv, you may have to prefix those commands with sudo.

Basic Usage

import docraptor

doc_api = docraptor.DocApi()
doc_api.api_client.configuration.username = 'YOUR_API_KEY_HERE'
# doc_api.api_client.configuration.debug = True

response = doc_api.create_doc({
  "test": True,                                                   # test documents are free but watermarked
  "document_content": "<html><body>Hello World</body></html>",    # supply content directly
  # "document_url": "http://docraptor.com/examples/invoice.html", # or use a url
  "name": "docraptor-python.pdf",                                 # help you find a document later
  "document_type": "pdf",                                         # pdf or xls or xlsx
  # "javascript": True,                                           # enable JavaScript processing
  # "prince_options": {
  #   "media": "screen",                                          # use screen styles instead of print styles
  #   "baseurl": "http://hello.com",                              # pretend URL when using document_content
  # },
})

Next Steps

  • Optionally store and get a URL for your converted document with document hosting
  • View more code examples with error handling, asynchronous creation, file saving, and document hosting.
  • Perfect your document styling with our style and formatting reference, and API reference. Easily add headers and footers, page breaks, page numbers, table of contents, and much more!

Node.js Installation

Install the request module for easy HTTP request management.

$ npm install request

Basic Usage

var request = require('request');
var fs = require('fs');
var content = "<html><body>TEST!</body></html>";

config = {
  url: 'https://api.docraptor.com/docs',
  encoding: null, //IMPORTANT! This produces a binary body response instead of text
  headers: {
    'Content-Type': 'application/json'
  },
  json: {
    user_credentials: "YOUR_API_KEY_HERE",
    doc: {
      document_content: content,
      type: "pdf",
      test: true,
      // prince_options: {
      //   media:   "screen",          // use screen styles instead of print styles
      //   baseurl: "http://hello.com" // URL to use for generating absolute URLs for assets from relative URLs
      // }
    }
  }
};

request.post(config, function(err, response, body) {
  fs.writeFile('doc_raptor_sample.pdf', body, "binary", function(writeErr) {
    console.log('Saved!');
  });
});

Next Steps

  • Optionally store and get a URL for your converted document with document hosting
  • Perfect your PDF styling with our style and formatting reference. Easily add headers and footers, page breaks, page numbers, table of contents, and much more!

Ruby Installation

Add the following to your Gemfile.

gem "docraptor"

Then run:

bundle install

Basic Usage

DocRaptor.configure do |config|
  config.username = "YOUR_API_KEY_HERE"
  # config.debugging = true
end

$docraptor = DocRaptor::DocApi.new

response = $docraptor.create_doc(
  test:             true,                                         # test documents are free but watermarked
  document_content: "<html><body>Hello World</body></html>",      # supply content directly
  # document_url:   "http://docraptor.com/examples/invoice.html", # or use a url
  name:             "docraptor-ruby.pdf",                         # help you find a document later
  document_type:    "pdf",                                        # pdf or xls or xlsx
  # javascript:       true,                                       # enable JavaScript processing
  # prince_options: {
  #   media: "screen",                                            # use screen styles instead of print styles
  #   baseurl: "http://hello.com",                                # pretend URL when using document_content
  # },
)

Next Steps

  • Optionally store and get a URL for your converted document with document hosting
  • View more code examples with error handling, asynchronous creation, file saving, and document hosting.
  • Perfect your document styling with our style and formatting reference, and API reference. Easily add headers and footers, page breaks, page numbers, table of contents, and much more!

Java Installation

To install the API client library to your local Maven repository, simply execute:

mvn install

To deploy it to a remote Maven repository instead, configure the settings of the repository and execute:

mvn deploy

After the client library is installed/deployed, you can use it in your Maven project by adding the following to your pom.xml:

<dependency>
  <groupId>com.docraptor</groupId>
  <artifactId>docraptor</artifactId>
  <version>2.0.0</version>
</dependency>

Basic Usage

import java.io.*;
import java.net.*;
import com.docraptor.*;

public class Sync {
  public static void main(String[] args) throws Exception {
    DocApi docraptor = new DocApi();
    ApiClient client = docraptor.getApiClient();
    client.setUsername("YOUR_API_KEY_HERE");
    //client.setDebugging(true);

    Doc doc = new Doc();
    doc.setTest(true);                                                   // test documents are free but watermarked
    doc.setDocumentContent("<html><body>Hello World</body></html>");     // supply content directly
    // doc.setDocumentUrl("http://docraptor.com/examples/invoice.html"); // or use a url
    doc.setDocumentType(Doc.DocumentTypeEnum.PDF);                       // PDF or XLS or XLSX
    doc.setName("docraptor-java.pdf");                                   // help you find a document later
    doc.setJavascript(true);                                             // enable JavaScript processing
    // prince_options = new PrinceOptions();
    // doc.setPrinceOptions(prince_options);
    // prince_options.setMedia("screen");                                // use screen styles instead of print styles
    // prince_options.setBaseurl("http://hello.com")                     // pretend URL when using document_content
    docraptor.createDoc(doc);
  }
}

Next Steps

  • Optionally store and get a URL for your converted document with document hosting
  • View more code examples with error handling, asynchronous creation, file saving, and document hosting.
  • Perfect your document styling with our style and formatting reference, and API reference. Easily add headers and footers, page breaks, page numbers, table of contents, and much more!

.NET Installation

Frameworks supported

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

Dependencies

Installation

Command line:

nuget.exe install DocRaptor

Package Manager Console:

Install-Package DocRaptor

Download DLLs: get DocRaptor.dll from GitHub

Basic Usage

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

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

    Doc doc = new Doc(
      test: true,                                                    // test documents are free but watermarked
      documentContent: "<html><body>Hello World</body></html>",      // supply content directly
      // documentUrl: "http://docraptor.com/examples/invoice.html",  // 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: "http://hello.com"                               // pretend URL when using document_content
      // )
    );

    byte[] createResponse = docraptor.CreateDoc(doc);
  }
}

Next Steps

  • Optionally store and get a URL for your converted document with document hosting
  • View more code examples with error handling, asynchronous creation, file saving, and document hosting.
  • Perfect your document styling with our style and formatting reference, and API reference. Easily add headers and footers, page breaks, page numbers, table of contents, and much more!

jQuery & JavaScript

The DocRaptor JavaScript library makes it easy to create PDFs with JavaScript. The library does not require jQuery, but you can use jQuery to define your document content. When a PDF is requested, the library constructs a hidden form and submits it to the DocRaptor API. Until all modern browsers support the download link attribute, using this hidden form is the best way to generate a file download directly from JavaScript.

Warning: This code exposes your API key in your website source code. This code should not be used in a publicly-accessible location, instead try using a server-side agent such as PHP or Ruby.

Example Code

<html>
  <head>
    <script src="https://docraptor.com/docraptor-1.0.0.js"></script>
    <script>
      var downloadPDF = function() {
        DocRaptor.createAndDownloadDoc("YOUR_API_KEY_HERE", {
          test: true, // test documents are free, but watermarked
          type: "pdf",
          document_content: document.querySelector('html').innerHTML, // use this page's HTML
          // document_content: "<h1>Hello world!</h1>",               // or supply HTML directly
          // document_url: "http://example.com/your-page",            // or use a URL
          // javascript: true,                                        // enable JavaScript processing
          // prince_options: {
          //   media: "screen",                                       // use screen styles instead of print styles
          // }
        })
      }
    </script>
    <style>
      @media print {
        #pdf-button {
          display: none;
        }
      }
    </style>
  </head>
  <body>
    <h1>Example!</h1>
    <input id="pdf-button" type="button" value="Download PDF" onclick="downloadPDF()" />
  </body>
</html>


We've got a DocRaptor jQuery plugin that you should check out. Examples and usage details can be found on that page.
Note: if you don't want to expose your API key in your client-side JavaScript, check out Referrer-based Document Generation. Also, DocRaptor fully supports the CORS specification, allowing cross-site HTTP requests.

Ready to get started? Try DocRaptor for free with unlimited test documents.