Convert HTML or URL to PDF with Rotativa.io HTTP API

date Oct 14, 2016

Now you can use Rotativa.io to create PDF docs by making a POST HTTP request to one of our endpoints.

The call should contain a JSON body and a Header with the ApiKey you can get on the preferences page. You can sign up for a free account here.

This is a sample HTTP header:

Accept: application/json, text/plain, */*
X-ApiKey: <YOUR_APIKEY_HERE>

And this is a sample JSON body:

{
    "htmlUrl": "http://www.vodafone.it"
}

To test it you can use curl:

curl -L https://eunorth.rotativahq.com \
  --fail --silent --show-error \
  --header "X-ApiKey:<YOUR_APIKEY_HERE>" \
  --data '{
            "htmlUrl": "https://news.ycombinator.com"
}' > test.pdf

The service will redirect the request to a temporary file on Azure storage. This is the reason for the -L option. This way curl will silently follow the redirect and download the PDF file. If you use any other tool/library to make the call you should make sure it silently follows 302 HTTP redirects.

If you prefer to get the URL of the temporary file you can add the returnLink property in the JSON body, with true as value.

The curl command will look like this (you won’t need the -L option anymore):

curl https://eunorth.rotativahq.com \
  --header "X-ApiKey:<YOUR_APIKEY_HERE>" \
  --data '{
            "htmlUrl": "https://news.ycombinator.com",
            "returnLink": true
          }'

This will return a json object containing the URL of the PDF:

{
  "pdfUrl":"https://rhqeuno2.blob.core.windows.net/temppdf/962ca7c9a7cd427092bd925469aefe24.pdf?sv=2015-04-05&sr=b&si=twominutepolicy&sig=r%2Fkzz8975%2FWp9QY7OCntxzTzJlmeT3L5lVBeSVRwdPg%3D"
}

It will be available for two minutes and then it will be deleted.

You can also pass a HTML document instead of a page URL. For example:

curl -L https://eunorth.rotativahq.com \
  --fail --silent --show-error \
  --header "X-ApiKey:<YOUR_APIKEY_HERE>" \
  --data '{
            "html": "<html><body>Hello world!</body></html>"
}' > test.pdf

All links in the HTML string should be absolute and public. No local path would work. This is the main difference with the .Net and PHP Rotativa.io client libraries.

If you are using Node then you can look at a blog post we dedicated to a Node implementation of the Rotativa.io call.

This is the JSON body with all possible parameters:

{
  "htmlUrl": "http://www.vodafone.it",
  "html": "",
  "filename": "kdkdkd.pdf",
  "returnLink": true,
  "pageMargins": {
    "bottom": null,
    "left": null,
    "right": null,
    "top": null
  },
  "pageSize": null,
  "pageWidth": null,
  "pageHeight": null,
  "pageOrientation": "portrait",
  "isJavaScriptDisabled": false,
  "isLowQuality": false,
  "isBackgroundDisabled": false,
  "minimumFontSize": null,
  "copies": null,
  "isGrayScale": false,
  "customSwitches": null
}

Those parameters will be converted to wkhtmltopdf parameters. If any are missing, you can manually add them in the customSwitches property.