The HTML2PDF Web Service for converting HTML to PDF is available through a HTTP API. Please use this document to get started.
The end-point of the HTTP API is https://app.html2pdfwebservice.com/api/convert and only accepts POST-requests. To tell the web service which page or content to convert to PDF and with what settings you're required to POST a JSON string as the payload. To see which settings are supported please take a look at the Settings-section. Authentication is also required and is explained in the API Authentication-section.
To be able to use our REST API you're required to authenticate yourself. To do so an account is required. In case you don't have an account yet you can sign up for our 14-day free trial. To authenticate against our web service you're required to use your API key and username. These can be found on your profile page and dashboard.
Authentication is done by providing these details in extra HTTP headers as shown below:
$ curl -H "X-API-Key: F8802062-4D31-11E3-8F59-BFD4058B6BFF" -H "X-API-Username: MyUsername" -d '{"url":"http://domain.com/invoice.html"}' https://app.html2pdfwebservice.com/api/convert > invoice.pdf
For your convenience we've listed all available options on this page.
We support multiple versions of our rendering engine due to visually different results between engines. By default v1
is used. To use v2
provide the engine parameter like so https://app.html2pdfwebservice.com/api/convert?engine=v2
You're required to either provide the content setting or url setting. The content setting may contain HTML data. If the page to be converted is accessible online use the url setting.
{"content": "<html><body><h1>Hello World!</h1><p>Lorem ipsum</p></body></html>"}
{"url": "http://html2pdfwebservice.com/documentation"}
{"collate": true}
{"no_collate": true}
{"copies": 1}
{"disable_external_links": false}
{"disable_internal_links": false}
{"disable_javascript": false}
{"no_pdf_compression": false}
{"disable_smart_shrinking": false}
{"dpi": 100}
{"encoding": "utf-8"}
{"enable_forms": true}
<!DOCTYPE html>
line or otherwise it will not show.{"footer_content": "<!DOCTYPE html> <p>Footer text</p>"}
{"footer_html": "http://html2pdfwebservice.com/footer.html"}
{"footer_center": "Footer text"}
{"footer_left": "Footer text"}
{"footer_right": "Footer text"}
{"no_footer_line": true}
{"footer_line": true}
{"footer_spacing": 40}
{"grayscale": false}
<!DOCTYPE html>
line or otherwise it will not show.{"header_content": "<!DOCTYPE html> <p>Footer text</p>"}
{"header_html": "http://html2pdfwebservice.com/header.html"}
{"header_center": "Footer text"}
{"header_left": "Footer text"}
{"header_right": "Footer text"}
{"no_footer_line": true}
{"header_line": true}
{"header_spacing": 40}
{"lowquality": false}
{"margin_bottom": "10mm"}
{"margin_left": "10mm"}
{"margin_right": "10mm"}
{"margin_top": "10mm"}
{"minimum_font_size": 5}
{"no_background": false}
{"orientation": "Landscape"}
{"orientation": "Portrait"}
{"outline": false}
{"outline_depth": 4}
{"page_height": 500}
{"page_size": "A4"}
{"page_size": "Letter"}
{"page_offset": 1}
{"page_width": 500}
{"print_media_type": false}
{"password": "my secret"}
{"proxy": "http://myproxy:8080"}
{"javascript_delay": 200}
{"title": "My document title"}
{"username": "myname"}
{"zoom": 1.0}
Note: Some styling options such as zoom
,
page_size
, page_width
, page_height
and
orientation
will not work if your HTML document contains floating
elements.
Instead of providing HTML you can also provide other markup languages. This
only works in cojunction with the content
,
header_content
and footer_content
settings. If you
make use of the url
setting these settings will be ignored.
<pre>
-element<body>
-element, but not the element itself!<span class="page"></span>
element will show the current page number.<span class="topage"></span>
element will show the total page count.<span class="date"></span>
element will show the current date.<span class="isodate"></span>
element will show the current isodate.<span class="time"></span>
element will show the current time.<span class="title"></span>
element will show the title of the current page object.<span class="doctitle"></span>
element will show the title of the output document.To make use of Preprocessors you only need to provide a few extra options. You
can mix & match preprocessors for the header, content and footer of your
document. Note that header_content
and footer_content
will be wrapped inside a <header id="header">
-element and <footer id="footer">
-element respectively. You can use their ID's to style them.
{"preprocessors":{"content":"Markdown","footer_content":"Textile","header_content":"HTML","style":"* { color: red; }"}}
{"preprocessors":{"content":"POD"}}
style
attribute: {"preprocessors":{"content":"Markdown","style":"body { font-family: Verdana; color: red; font-style: italic;}"}}
.Our REST API only has 1 end-point at https://app.html2pdfwebservice.com/api/convert
and only accepts POST requests.
{"url": "http://domain.com/invoice.html"}
Retry-After
header.The following headers are added to the response as well.
$ curl -H "X-API-Key: F8802062-4D31-11E3-8F59-BFD4058B6BFF" -H "X-API-Username: MyUsername" -d '{"url":"http://domain.com/invoice.html"}' https://app.html2pdfwebservice.com/api/convert > invoice.pdf
$ curl -H "X-API-Key: F8802062-4D31-11E3-8F59-BFD4058B6BFF" -H "X-API-Username: MyUsername" -d '{"content":"<html><head><title>My page</title></head><body><h1>Hello World!</h1><p>I am an HTML page converted to PDF!</p></body></html>"}' https://app.html2pdfwebservice.com/api/convert > page.pdf
$ curl -H "X-API-Key: F8802062-4D31-11E3-8F59-BFD4058B6BFF" -H "X-API-Username: MyUsername" -d '{"url":"http://domain.com/invoice.html", "username": "JohnDoe", "password": "MySecret!"}' https://app.html2pdfwebservice.com/api/convert > invoice.pdf
$ curl -H "X-API-Key: F8802062-4D31-11E3-8F59-BFD4058B6BFF" -H "X-API-Username: MyUsername" -X GET https://app.html2pdfwebservice.com/api/usage
Which results in a hash containing the following fields: {"used": 0,
"limit": 250}
. In case an error occurs it returns an HTTP 500 error.
Otherwise when successful it returns an HTTP 200 status.
$settings = array( 'url' => 'http://domain.com/invoice.html', ); $curl = curl_init(); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($settings)); curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'X-API-Username: MyUsername', 'X-API-Key: F8802062-4D31-11E3-8F59-BFD4058B6BFF' )); curl_setopt($curl, CURLOPT_URL, 'https://app.html2pdfwebservice.com/api/convert'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // Helps to debug in case of issues // curl_setopt($curl, CURLOPT_VERBOSE, 1); // In case the SSL certificate isn't accepted because of outdated certificates // on your server curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $res = curl_exec($curl); // Save PDF to disk file_put_contents('document.pdf', $res); curl_close($curl);
#!/usr/bin/env perl use strict; use warnings; use Mojo::UserAgent; my $ua = Mojo::UserAgent->new; my $tx = $ua->post( 'https://app.html2pdfwebservice.com/api/convert' => { 'X-API-Username' => 'MyUsername', 'X-API-Key' => 'F8802062-4D31-11E3-8F59-BFD4058B6BFF' } => json => {url => 'http://domain.com/invoice.html'} ); if (my $res = $tx->success) { my $pdf_data = $res->body; }
require 'net/https' require 'uri' uri = URI.parse('https://app.html2pdfwebservice.com/api/convert') https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true # In case the SSL certificate isn't accepted https.verify_mode = OpenSSL::SSL::VERIFY_NONE req = Net::HTTP::Post.new(uri.path) req['X-API-Username'] = 'MyUsername' req['X-API-Key'] = 'F8802062-4D31-11E3-8F59-BFD4058B6BFF' req.body = '{"url": "http://domain.com/invoice.html"}' res = https.request(req) if res.code == '200' pdf_data = res.body # - or write to file - # File.open('invoice.pdf', 'w') { |file| file.write(res.body) } end
var https = require('https'); var fs = require('fs'); var data = JSON.stringify({ 'url': 'http://domain.com/invoice.html' }); var options = { host: 'app.html2pdfwebservice.com', port: '443', path: '/api/convert', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': data.length, 'X-API-Username': 'MyUsername', 'X-API-Key': 'F8802062-4D31-11E3-8F59-BFD4058B6BFF' } }; var req = https.request(options, function(res) { var msg = ''; console.log("statusCode: ", res.statusCode); console.log("headers: ", res.headers); res.setEncoding('utf8'); res.on('data', function(chunk) { msg += chunk; }); res.on('end', function() { fs.writeFile('test.pdf', msg, 'binary', function(err) { if(err) { console.log(err); } }); }); }); req.write(data); req.end(); req.on('error', function(err) { console.log(err); });
Examples for other languages will follow soon! Got a working example for us to put here? Please contact us!
Every plan offers an amount of tokens to be used when converting documents. When a PDF has been generated its token usage will be calculated. A token is worth 500KB. So if the resulting PDF is 250KB of size it will have used 1 token(s). A PDF with a size of 2048KB will have used 5 token(s).
We maintain soft and hard limits for token usage. The amount of tokens that your subscription plan offers functions as a soft limit. When your soft limit is exceeded we'll start returning the X-Tokens-Warning
header which will notify you of exceeding your usage. Every convert call will return your token usage and remaining tokens as well.
The hard limit is set at 20%*. So if your plan has 500 tokens, the hard limit will be set at 600 tokens. After you've reached the hard limit access to the conversion API will be denied.
Please take care of monitoring your usage. You can retrieve your usage with the API-call /api/usage
or by logging in into your control panel. The header X-Tokens-Left
will be included with every conversion API-call as well. Contact us before you reach your hard limit to ensure continued service.
* The way the hard limit is calculated may change at any time without notice.
Missing anything from the documentation? Do not hesitate to contact us.