docker-wkhtmltopdf-aas
955
wkhtmltopdf in a docker container as a web service.
This image is based on the wkhtmltopdf container.
Run the container with docker run and binding the ports to the host. The web service is exposed on port 80 in the container.
docker run -d -e API_TOKEN='travisci-test123456789' -p 127.0.0.1:80:5555
Take a note of the public port number where docker binds to.
There are multiple ways to generate a PDF of HTML using the service.
This is a convenient way to use the service from command line utilities like curl.
curl -X POST -vv -F 'file=@path/to/local/file.html' http://<docker-host>:<port>/ -o path/to/output/file.pdf
where:
If you are planning on using this service in your application, it might be more convenient to use the JSON API that the service uses.
Here is an example using python requests:
import json
import requests
url = 'http://<docker_host>:<port>/'
data = {
'contents': open('/file/to/convert.html').read().encode('base64'),
}
headers = {
'Content-Type': 'application/json', # This is important
}
response = requests.post(url, data=json.dumps(data), headers=headers)
# Save the response contents to a file
with open('/path/to/local/file.pdf', 'wb') as f:
f.write(response.content)
Here is another example in python, but this time we pass options to wkhtmltopdf. When passing our settings we omit the double dash "--" at the start of the option. For documentation on what options are available, visit http://wkhtmltopdf.org/usage/wkhtmltopdf.txt
import json
import requests
url = 'http://<docker_host>:<port>/'
data = {
'contents': open('/file/to/convert.html').read().encode('base64'),
'options': {
#Omitting the "--" at the start of the option
'margin-top': '6',
'margin-left': '6',
'margin-right': '6',
'margin-bottom': '6',
'page-width': '105mm',
'page-height': '40mm'
}
}
headers = {
'Content-Type': 'application/json', # This is important
}
response = requests.post(url, data=json.dumps(data), headers=headers)
# Save the response contents to a file
with open('/path/to/local/file.pdf', 'wb') as f:
f.write(response.content)
PHP example
$url = 'http://<docker_host>:<port>/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); // Assuming you're requesting JSON
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = json_encode([
'contents' => base64_encode($html)
]);
# print response
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
echo curl_exec($ch);
curl example for JSON apiThe development of the container takes place on Github. If you have a question or a bug report to file, you can report as a github issue.
This image was built at Openlabs.
This image is professionally supported by Openlabs. If you are looking for on-site teaching or consulting support, contact our sales and support teams.
Content type
Image
Digest
Size
133 MB
Last updated
about 10 years ago
docker pull madnight/docker-wkhtmltopdf-aas