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.

Quick Start

Here are some code samples you can use. Don't forget to obtain a token here.

You can find detailed usage information below.

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/v7/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 json: true 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 contain an image URL, that points to the PDF and a cached field to indicate if the document is being served from cache, height and width of the captured URL, HTTP status code as remote_status, and execution (rendering + converting) time as run_time parameter.

{
  "cached": true,
  "height": 768,
  "width": 1280,
  "image": "https://cdn.restpack.io/a/cache/pdf/96f6cd877429e5b37e96533cff96c52ff10fc687d537d6cad72f1f2795c020ec",
  "remote_status": 200,
  "run_time": 1166,
  "url": "https://google.com"
}

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/v7/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.

jsonboolean

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

Default: false
privacyboolean

Ensure that the captured document does not get cached / stored for further use. You can not use json mode with this setting as it would not be possible to provide a CDN URL.

Default: false
cache_ttlnumber

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

Max: 1 week
filenamestring

If specified, ensures that the resulting file is saved with the given name.

Example: mydocument.pdf
pdf_pageenum

Page size for created document

Default: FullPattern: A0 | A1 | A2 | A3 | A4 | A5 | A6 | Legal | Letter | Tabloid | Ledger | Full
pdf_widthstring

Custom pdf page width. Must be used together with pdf_height. Prefer pdf_page if you don't have an exact page size requirement.. Unit can be either px for pixels, in for inches.

Pattern: NUM[px|in]Example: 700px
pdf_heightstring

Custom pdf page height. Check pdf_width for details.

Pattern: NUM[px|in]Example: 30in
pdf_marginscss sizes

CSS style margin sizes. 1px 2px 3px 4px would cause 1px top, 2px right, 3px bottom, 4px left margins. A single size can be specified for both margins.

Default: 0Example: 30px 20px
pdf_orientationenum

Page orientation

Default: portraitPattern: portrait | landscape
pdf_headerstring

HTML template for page header. It should have a valid markup and can contain elements with classes 'pageNumber', 'totalPages', 'url', 'title' or 'date'. Header is automatically added to all pages. Note that you need to have top margins on your documents in order to have the header show up. Please add margins using pdf_margins. Check Headers and Footers section for more details.

Example: Page <span class="pageNumber"></span> of <span class="totalPages"></span>
pdf_footerstring

HTML template for page footer. Please check pdf_header information for details.

Example: Page <span class="pageNumber"></span> of <span class="totalPages"></span>
pdf_authorstring

Author name of the document

Example: Restpack Inc.
pdf_page_rangesstring

Returns only specified pages of the PDF document

Pattern: 1 | 5-6
pdf_encryptionenum

Enable document encryption

Default: falsePattern: 40 | 128Example: 40
owner_passwordstring

Secure PDF document with a owner password.

user_passwordstring

Secure PDF document with a user password. With user password person can only view the document and allowed operations

no_printboolean

Disable to print PDF document

Default: false
no_modifyboolean

Disable docuent modification on PDF document

Default: false
no_extractboolean

Disable text extraction on PDF document.

Default: false
cssstring

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

jsstring

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

delaynumber

Time in milliseconds to delay capture after page load.

Default: 500Max: 20000
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
emulate_mediaenum

Force CSS media emulation for print or screen.

Default: screenPattern: screen | print
allow_failedboolean

By default, any response from remote server outside http 200-299 status codes generates an error. If you wish to capture error pages, pass true.

Default: false
waitenum

Wait until window load event fires or network becomes idle before capturing the page. By default we wait for the load event but if you are capturing pages with async content / lazy loaded images etc, waiting for network might work better. Third option is dom, which basically waits for domready event on the main window.

Default: loadPattern: load | network | dom
shutterstring

Wait until a DOM element matching the provided css selector becomes present on the page. You can control when you want to capture the page using this parameter. The process times out after 10 seconds.

Example: h1.someclass
block_adsboolean

Block / hide ads on the page.

Default: false
block_cookie_warningsboolean

Block / hide European Union cookie warnings before capture.

Default: false
grayscaleboolean

Render document with grayscale filter

Default: false
POSThttps://restpack.io/api/html2pdf/v7/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:

{
  "cached": true,
  "height": 768,
  "width": 1280,
  "image": "https://cdn.restpack.io/a/cache/pdf/96f6cd877429e5b37e96533cff96c52ff10fc687d537d6cad72f1f2795c020ec",
  "remote_status": 200,
  "run_time": 1166,
  "url": "https://google.com"
}

Retrieve usage information

To get your plans latest usage information, you need to invoke a GET request on the following URL with your access token.

As a result, you will get a JSON response with a date range of usage, your plans monthly conversions limit in the limit field, your used conversions in the total field and last seven days usage stats in days field.

GEThttps://restpack.io/api/html2pdf/usage

Parameters

access_tokenstring

Your personal access token for API access.

Request & Response Sample:

{
  "days": [
    {
      "count": 6,
      "day": "2019-05-06T00:00:00Z"
    },
    {
      "count": 9,
      "day": "2019-05-07T00:00:00Z"
    },
    {
      "count": 1,
      "day": "2019-05-08T00:00:00Z"
    },
    {
      "count": 31,
      "day": "2019-05-15T00:00:00Z"
    },
    {
      "count": 1,
      "day": "2019-05-16T00:00:00Z"
    },
    {
      "count": 3,
      "day": "2019-05-17T00:00:00Z"
    },
    {
      "count": 1,
      "day": "2019-05-18T00:00:00Z"
    }
  ],
  "from": "2019-05-01T00:00:00Z",
  "limit": 1000,
  "to": "2019-05-20T11:58:06.604980595Z",
  "total": 52,
  "workers": 1
}

Features

Headers and Footers

Captured documents do not contain any header or footer by default. If you wish to add page headers or footers you can provide an HTML template and the API automatically handles injection for you.

Note that we refer to these html snippets as templates because they can contain placeholders for dynamic data. For example if you wish to show the current page number somewhere within your header, you can use <span class="pageNumber"></span> element. The pageNumber class instructs our engine to inject the current page number in that element.

Available class names for dynamic data:

  • pageNumber: Current page number
  • totalPages: Total number of pages in the document
  • url: Captured page URL
  • title: Document title
  • date: Date of capture in UTF

Also note that you must have a pdf_margins paramater in order to use the header / footer functionality. The header and footer show up inside your top and bottom margins, you need to provide appropriate space for the headers and footers to render.

While the header and footer sections can be styled using CSS, they do not share the main pages css styles. The best way to style header and footer elements is to use inline styles as in <div style="background: red">...</div>.

Delay

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

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 cache_ttl parameter (in seconds)

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

HTTP 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.
  • headers parameter is the freeform one. You can pass any string containing headers seperated by \n.