Git Product home page Git Product logo

docker-wkhtmltopdf-aas's Introduction

docker-wkhtmltopdf-aas

wkhtmltopdf in a docker container as a web service.

This image is based on the wkhtmltopdf container.

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 openlabs/docker-wkhtmltopdf-aas

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 'file=@path/to/local/file.html' http://<docker-host>:<port>/ -o path/to/output/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.

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)

TODO

  • Implement conversion of URLs to PDF
  • Add documentation on passing options to the service
  • Add curl example for JSON api
  • Explain more gunicorn options

Bugs and questions

The 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.

Authors and Contributors

This image was built at Openlabs.

Professional Support

This image is professionally supported by Openlabs. If you are looking for on-site teaching or consulting support, contact our sales and support teams.

docker-wkhtmltopdf-aas's People

Contributors

aroraumang avatar gustav-weightpoint avatar hi-arti avatar ncjones avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.