This is an outdated version of the HTML To PDF API. Please refer to Latest Version Documentation for the greatest functionality.

Introduction

Restpack HTML to PDF API is an easy to use RESTful web service that can capture live web pages and deliver the structured view as a PDF document. The service sits on a fully functional browser rendering engine with rich html / css / js capabilities.

Authentication

With each API call, you must provide an authentication token. To create a new access token please refer to user tokens page on your account dashboard.

You will need to use your Direct Access Token for simple API calls.

You can either provide the access token within the querystring or as a header.

  • If you wish to use the querystring method. Provide the token as access_token querystring parameter. As in https://restpack.io/api/html2pdf/v2/convert?access_token={TOKEN}
  • In case you want to use a header, please provide the access token in X-Access-Token header.

Errors

Access token errors are represented in 4xx class HTTP errors.

  • 401 - in case no access token is provided.
  • 402 - when the account balance is not sufficient for this API call.
  • 403 - when the access token is not valid / revoked.

Response Modes

By default, the API returns the raw document itself. This is the easiest way to consume the API. You can stream the result back to a file, to a web request or any other storage service. Meanwhile, it is possible to receive a JSON response using mode: json parameter.

When you request a JSON response, API will return a JSON object with a publicly accesible URL to the resulting rendered pdf. You can use this document directly on your websites or access the file from this URL and save it elsewhere. Using this URL is not counted against your API call limits and it is being hosted over our CDN servers.

JSON responses simply contain an image URL, that points to the PDF and a cached field to indicate if the document is being served from cache:

{
  "image": "http://cdn.restpack.io/...",
  "cached": true
}

Endpoint

Create and return a PDF document

In order to create a PDF document, you simply need to invoke a GET request on the following URL.

All parameters should be passed in querysting.

GEThttps://restpack.io/api/html2pdf/v2/convert

Parameters

It is required to provide one of url or html parameters as the document source.

urlurl

The URL of web page, including the protocol that you want to capture.

Example: http://example.com
htmlstring

Raw HTML string of a page that you want to capture.

Example: <p>Test</p>
access_tokenstring

Your personal access token for API access.

Example: XXXXXXXX
jsonboolean

Return a JSON response with the resulting image's URL instead of the image itself.

Default: false
pdf_pageenum

Custom page size for created document

Default: FullPattern: A3 | A4 | A5 | Legal | Letter | Tabloid | Full
pdf_marginsenum

0 for default margins, 1 for no margins, 2 for minimal margins

Default: 0Pattern: 0 | 1 | 2
cssstring

Additional CSS string to be injected into the page before render.

jsstring

Additional JS string to be injected into the page before render.

prescrollboolean

Force scrolling the webpage before capture. Might help with dynamic loading assets.

delaynumber

Time in milliseconds to delay capture after page load.

Default: 2000Max: 10000
ttlnumber

Time in milliseconds for the resulting image to be cached for further requests.

Default: 1 dayMax: 1 week
freshboolean

Force rendering a new screenshot disregarding the cache status.

Default: false
user_agentstring

Custom user-agent header string for the web request.

Default: Chrome Compatible User Agent
accept_languagestring

Custom accept-language header string for the web request.

headersstring

Additional headers seperated with newline

Example: X-Test: header\nAccept-Type: html
base64boolean

Serialize response file to base64

POSThttps://restpack.io/api/html2pdf/v2/convert

POST mode accepts the exact same parameters but you need to use a JSON or url encoded body. access_token parameter still needs to be passed in the querystring or via x-access-token header.

Request & Response Sample:

http --form POST https://restpack.io/api/html2pdf/v2/convert \
  x-access-token:TOKEN \
  url=http://google.com \
  json=truecurl --request POST \
  --url https://restpack.io/api/html2pdf/v2/convert \
  --header 'x-access-token: TOKEN' \
  --data 'url=http%3A%2F%2Fgoogle.com&json=true'var request = require("request");

var options = { method: 'POST',
  url: 'https://restpack.io/api/html2pdf/v2/convert',
  headers: { 'x-access-token': 'TOKEN' },
  form: { url: 'http://google.com', json: true } };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});
require 'uri'
require 'net/http'

url = URI("https://restpack.io/api/html2pdf/v2/convert")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(url)
request["x-access-token"] = 'TOKEN'
request.body = "url=http%3A%2F%2Fgoogle.com&json=true"

response = http.request(request)
puts response.read_bodyimport http.client

conn = http.client.HTTPSConnection("restpack.io")

payload = "url=http%3A%2F%2Fgoogle.com&json=true"

headers = { 'x-access-token': "TOKEN" }

conn.request("POST", "/api/html2pdf/v2/convert", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
)

func main() {

	url := "https://restpack.io/api/html2pdf/v2/convert"

	payload := strings.NewReader("url=http%3A%2F%2Fgoogle.com&json=true")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("x-access-token", "TOKEN")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://restpack.io/api/html2pdf/v2/convert",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "url=http%3A%2F%2Fgoogle.com&json=true",
  CURLOPT_HTTPHEADER => array(
    "x-access-token: TOKEN"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}var client = new RestClient("https://restpack.io/api/html2pdf/v2/convert");
var request = new RestRequest(Method.POST);
request.AddHeader("x-access-token", "TOKEN");
request.AddParameter("undefined", "url=http%3A%2F%2Fgoogle.com&json=true", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);HttpResponse<String> response = Unirest.post("https://restpack.io/api/html2pdf/v2/convert")
  .header("x-access-token", "TOKEN")
  .body("url=http%3A%2F%2Fgoogle.com&json=true")
  .asString();
{
  "image": "http://cdn.restpack.io/...",
  "cached": true
}

Features

Delay

By default, capturing engine waits for the page load and then 2 more seconds for initial javascripts to settle their rendering. You can control this duration using delay parameter (in seconds) from instantaneous to 10 seconds.

http GET 'https://restpack.io/api/html2pdf/v2/convert?url=http%3A%2F%2Fgoogle.com&delay=5' \
  x-access-token:TOKENcurl --request GET \
  --url 'https://restpack.io/api/html2pdf/v2/convert?url=http%3A%2F%2Fgoogle.com&delay=5' \
  --header 'x-access-token: TOKEN'var request = require("request");

var options = { method: 'GET',
  url: 'https://restpack.io/api/html2pdf/v2/convert',
  qs: { url: 'http://google.com', delay: '5' },
  headers: { 'x-access-token': 'TOKEN' } };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});
require 'uri'
require 'net/http'

url = URI("https://restpack.io/api/html2pdf/v2/convert?url=http%3A%2F%2Fgoogle.com&delay=5")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Get.new(url)
request["x-access-token"] = 'TOKEN'

response = http.request(request)
puts response.read_bodyimport http.client

conn = http.client.HTTPSConnection("restpack.io")

headers = { 'x-access-token': "TOKEN" }

conn.request("GET", "/api/html2pdf/v2/convert?url=http%3A%2F%2Fgoogle.com&delay=5", headers=headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))package main

import (
	"fmt"
	"net/http"
	"io/ioutil"
)

func main() {

	url := "https://restpack.io/api/html2pdf/v2/convert?url=http%3A%2F%2Fgoogle.com&delay=5"

	req, _ := http.NewRequest("GET", url, nil)

	req.Header.Add("x-access-token", "TOKEN")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://restpack.io/api/html2pdf/v2/convert?url=http%3A%2F%2Fgoogle.com&delay=5",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "x-access-token: TOKEN"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}var client = new RestClient("https://restpack.io/api/html2pdf/v2/convert?url=http%3A%2F%2Fgoogle.com&delay=5");
var request = new RestRequest(Method.GET);
request.AddHeader("x-access-token", "TOKEN");
IRestResponse response = client.Execute(request);HttpResponse<String> response = Unirest.get("https://restpack.io/api/html2pdf/v2/convert?url=http%3A%2F%2Fgoogle.com&delay=5")
  .header("x-access-token", "TOKEN")
  .asString();

TTL

The API caches generated PDF documents up to a specified time period. If you request a PDF of a previously cached web page, result will be provided from the cache. It is possible to control the duration of cache using ttl parameter (in seconds)

In addition to setting a ttl, it is possible to use fresh=true prameter to invalidate cache and capture a new PDF.

http GET 'https://restpack.io/api/html2pdf/v2/convert?url=http%3A%2F%2Fgoogle.com&ttl=60' \
  x-access-token:TOKENcurl --request GET \
  --url 'https://restpack.io/api/html2pdf/v2/convert?url=http%3A%2F%2Fgoogle.com&ttl=60' \
  --header 'x-access-token: TOKEN'var request = require("request");

var options = { method: 'GET',
  url: 'https://restpack.io/api/html2pdf/v2/convert',
  qs: { url: 'http://google.com', ttl: '60' },
  headers: { 'x-access-token': 'TOKEN' } };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});
require 'uri'
require 'net/http'

url = URI("https://restpack.io/api/html2pdf/v2/convert?url=http%3A%2F%2Fgoogle.com&ttl=60")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Get.new(url)
request["x-access-token"] = 'TOKEN'

response = http.request(request)
puts response.read_bodyimport http.client

conn = http.client.HTTPSConnection("restpack.io")

headers = { 'x-access-token': "TOKEN" }

conn.request("GET", "/api/html2pdf/v2/convert?url=http%3A%2F%2Fgoogle.com&ttl=60", headers=headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))package main

import (
	"fmt"
	"net/http"
	"io/ioutil"
)

func main() {

	url := "https://restpack.io/api/html2pdf/v2/convert?url=http%3A%2F%2Fgoogle.com&ttl=60"

	req, _ := http.NewRequest("GET", url, nil)

	req.Header.Add("x-access-token", "TOKEN")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://restpack.io/api/html2pdf/v2/convert?url=http%3A%2F%2Fgoogle.com&ttl=60",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "x-access-token: TOKEN"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}var client = new RestClient("https://restpack.io/api/html2pdf/v2/convert?url=http%3A%2F%2Fgoogle.com&ttl=60");
var request = new RestRequest(Method.GET);
request.AddHeader("x-access-token", "TOKEN");
IRestResponse response = client.Execute(request);HttpResponse<String> response = Unirest.get("https://restpack.io/api/html2pdf/v2/convert?url=http%3A%2F%2Fgoogle.com&ttl=60")
  .header("x-access-token", "TOKEN")
  .asString();

Headers

While the service uses reasonable defaults for the request headers, you can customize them as you wish.

  • user_agent parameter customizes the User-Agent header.
  • accept_language parameter customizes the Accept-Language header.
  • headers parameter is the freeform one. You can pass any string containing headers seperated by \n.
echo '{ "url": "http://google.com", "json": true, user_agent: "MyBot 1/2", headers: "Cookie: name=value\nX-Custom-Header: custom" }' |  \
  http POST https://restpack.io/api/html2pdf/v2/convert \
  x-access-token:TOKENcurl --request POST \
  --url https://restpack.io/api/html2pdf/v2/convert \
  --header 'x-access-token: TOKEN' \
  --data '{ "url": "http://google.com", "json": true, user_agent: "MyBot 1/2", headers: "Cookie: name=value\nX-Custom-Header: custom" }'var request = require("request");

var options = { method: 'POST',
  url: 'https://restpack.io/api/html2pdf/v2/convert',
  headers: { 'x-access-token': 'TOKEN' },
  body: '{ "url": "http://google.com", "json": true, user_agent: "MyBot 1/2", headers: "Cookie: name=value\\nX-Custom-Header: custom" }' };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});
require 'uri'
require 'net/http'

url = URI("https://restpack.io/api/html2pdf/v2/convert")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(url)
request["x-access-token"] = 'TOKEN'
request.body = "{ \"url\": \"http://google.com\", \"json\": true, user_agent: \"MyBot 1/2\", headers: \"Cookie: name=value\\nX-Custom-Header: custom\" }"

response = http.request(request)
puts response.read_bodyimport http.client

conn = http.client.HTTPSConnection("restpack.io")

payload = "{ \"url\": \"http://google.com\", \"json\": true, user_agent: \"MyBot 1/2\", headers: \"Cookie: name=value\\nX-Custom-Header: custom\" }"

headers = { 'x-access-token': "TOKEN" }

conn.request("POST", "/api/html2pdf/v2/convert", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
)

func main() {

	url := "https://restpack.io/api/html2pdf/v2/convert"

	payload := strings.NewReader("{ \"url\": \"http://google.com\", \"json\": true, user_agent: \"MyBot 1/2\", headers: \"Cookie: name=value\\nX-Custom-Header: custom\" }")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("x-access-token", "TOKEN")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://restpack.io/api/html2pdf/v2/convert",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{ \"url\": \"http://google.com\", \"json\": true, user_agent: \"MyBot 1/2\", headers: \"Cookie: name=value\\nX-Custom-Header: custom\" }",
  CURLOPT_HTTPHEADER => array(
    "x-access-token: TOKEN"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}var client = new RestClient("https://restpack.io/api/html2pdf/v2/convert");
var request = new RestRequest(Method.POST);
request.AddHeader("x-access-token", "TOKEN");
request.AddParameter("undefined", "{ \"url\": \"http://google.com\", \"json\": true, user_agent: \"MyBot 1/2\", headers: \"Cookie: name=value\\nX-Custom-Header: custom\" }", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);HttpResponse<String> response = Unirest.post("https://restpack.io/api/html2pdf/v2/convert")
  .header("x-access-token", "TOKEN")
  .body("{ \"url\": \"http://google.com\", \"json\": true, user_agent: \"MyBot 1/2\", headers: \"Cookie: name=value\\nX-Custom-Header: custom\" }")
  .asString();