Skip to content

a docker web service to convert html to pdf or image

Notifications You must be signed in to change notification settings

lalekz/go-wkhtmltopdf

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wkhtmltopdf as a web service

A dockerized webservice written in Go that uses wkhtmltopdf to convert HTML into documents (images or pdf files).

Docker

build

docker build -t lalekz/go-wkhtmltopdf:alpine3.10 .

run

docker run -d --name go-wkhtmltopdf -p 8080:80 lalekz/go-wkhtmltopdf:alpine3.10

Usage

The service listens on port 80 for POST requests on the root path (/). Any other method returns a 405 not allowed status. Any other path returns a 404 not found status.

The body should contain a JSON-encoded object containing the following parameters:

  • contents: Base64-encoded HTML page contents to convert.
  • output: The type of document to generate, can be either jpg, png or pdf. Defauts to pdf if not specified. Depending on the output type the appropriate binary is called.
  • options: A list of key-value arguments that are passed on to the appropriate wkhtmltopdf binary. Boolean values are interpreted as flag arguments (e.g.: --greyscale).
  • cookies: A list of key-value arguments that are passed on to the appropriate wkhtmltopdf binary as separate cookie arguments.

Example: posting HTML file using python requests:

import json
import requests

url = 'http://<docker_host>:<port>/'
data = {
  'contents': open('/file/to/convert.html').read().encode('base64'),
  'options': {
    'margin-bottom': '1cm',
    'orientation': 'Landscape'
  },
  'cookies': {
    'foo': 'bar',
    'baz': 'foo'
  },
  'output':'pdf'

}

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)

will have the effect of the following command-line being executed on the server:

/usr/local/bin/wkhtmltopdf --margin-bottom 1cm --orientation Landscape --cookie foo bar --cookie baz foo /tmp/238084854.html -

The - at the end of the command-line is so that the document contents are redirected to stdout so we can in turn redirect it to the web server's response stream.

When using jpg or png output, the set of options you can pass are actually more limited. Please check wkhtmltopdf usage docs or rather just use wkhtmltopdf --help or wkhtmltoimage --help to get help on the available command-line arguments.

reference

https://github.com/Surnet/docker-wkhtmltopdf

https://github.com/mickaelperrin/docker-wkhtmltopdf-service

About

a docker web service to convert html to pdf or image

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 78.8%
  • Dockerfile 21.2%