Sign inSign up

jrm0/wkhtmltopdf-ass

By jrm0

Updated about 2 years ago

wkhtmltopdf in a docker container as a web service.

Image
0

2.5K

jrm0/wkhtmltopdf-ass repository overview

wkhtmltopdf-ass

wkhtmltopdf in a docker container as a web service.

Running the service

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 -P jrm0/wkhtmltopdf-ass

The container now runs as a daemon.

Find the port that the container is bound to:

docker port 071599a1373e 80

where 071599a1373e is the container SHA that docker assigned when docker run was executed in the previous command.

Take a note of the public port number where docker binds to.

Using the webservice

There are multiple ways to generate a PDF of HTML using the service.

Uploading a HTML file

This is a convenient way to use the service from command line utilities like curl.

curl -X POST -vv -F '[email protected]&options={"page-size":"A4","encoding":"UTF-8","dpi":"300"}' http://<docker-host>:<port>/ -o -o file.pdf

where:

  • docker-host is the hostname or address of the docker host running the container
  • port is the public port to which the container is bound to.
JSON API

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.

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)

Tag summary

Content type

Image

Digest

sha256:f0c104158

Size

150.1 MB

Last updated

about 2 years ago

docker pull jrm0/wkhtmltopdf-ass