Getting Started

wkhtmltopdf as a service gives you a REST api to create PDF documents from html without the headache of managing servers and capacity.

This guide will help you implement your html to pdf conversion.

Authentication#

You need an API key to make any conversion requests against our service. If you don't have one, you can get one for free below.

Building the request#

Authorization#

There are a couple ways to authorize your request.

Query parameter (auth)

https://wkhtmltopdfservice.com/api/convert-to-pdf?auth=wk_********

Bearer Token

POST https://wkhtmltopdfservice.com/api/convert-to-pdf
Authorization: Bearer wk_********

Body#

The request body can be any of the following formats:

JSON

Content-Type: application/json
{
"html": string // optional
"url": string // optional (either html or url must be specified)
"options": {} // optional, see next page for complete list of options
}

WWW Form Encoded

Content-Type: application/x-www-form-urlencoded
html: string // optional
url: string // optional (either html or url must be specified)
options: string of json-encoded data // optional, see next page for complete list of options

Multipart Form Encoded

Content-Type: multipart/form-data
html: string // optional
url: string // optional (either html or url must be specified)
options: string of json-encoded data // optional, see next page for complete list of options

Responses#

Success

When your conversion is successful, the response will have the following format

HTTP/1.1 200 OK
Content-Type: application/pdf
...pdf document bytes

Failure

Failures will always have a status code >=400 and have an error of the following structure.

{
"error": "string,required",
"message": "string,optional",
"stackTrace": "string,optional"
}

429

Failures with http status code 429 are caused by invalid API keys. If you make too many requests with an invalid API key you'll receive a 429.

Working Sample#

async function getReport() {
// build report
const result = await fetch("https://wkhtmltopdfservice.com/api/convert-to-pdf", {
method: "POST",
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer wk_*************',
},
body: JSON.stringify({
url: "https://mysite/the-report",
options: {
title: "My Report",
}
})
});
// check status code before touching response body
if(result.status === 200) {
return result.arrayBuffer();
}
let message = "";
try {
const js = await result.json();
message = "Failed to generate report with message '" + js.error + "''"
} catch (e) {
message = "Failed to generate report with statusCode=" + result.status;
}
throw new Error(message);
}

* you'll need to use your own token in place of wk_*************