Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to ubuntu20.04 python3.8 wkhtmltopdf0.12.6 #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions ubuntu20-python3/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM ubuntu:20.04

ENV DEBIAN_FRONTEND noninteractive

ADD app.py /app.py

RUN sed 's/main$/main universe/' -i /etc/apt/sources.list \
&& apt-get update && apt-get upgrade -y \
&& apt-get install -y build-essential xorg libssl-dev libxrender-dev wget python3-pip vim --no-install-recommends xvfb libfontconfig libjpeg-turbo8 xfonts-75dpi fontconfig \
&& wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb \
&& apt install ./wkhtmltox_0.12.6-1.focal_amd64.deb \
&& pip install werkzeug executor gunicorn futures gunicorn[eventlet] gunicorn[gevent] gunicorn[tornado] gunicorn[gthread] \
&& apt-get -y clean \
&& apt-get -y purge \
&& rm wkhtmltox_0.12.6-1.focal_amd64.deb \
&& rm -rf /var/lib/apt/lists/* /tmp/*

EXPOSE 6500

ENTRYPOINT ["usr/local/bin/gunicorn"]

CMD ["-b", "0.0.0.0:6500", "--access-logfile", "-", "--error-logfile", "-", "--log-level", "debug", "app:application"]
63 changes: 63 additions & 0 deletions ubuntu20-python3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
New features:

* Updated to Ubuntu 20.04
* Updated WKHTMLTOPDF to 0.12.6-1
* Updated to Python3.8


## Build the image

`cd ubuntu20-python3`

`docker build -t wkhtmltopdf .`


## Run the container

`docker run -d -p 6500:6500 localhost/wkhtmltopdf`


## Python3 file example

``` python
import requests

with open('input_file.html', 'r') as f:
html = f.read()

response = requests.post('http://127.0.0.1:6500/', files={'file': html})

with open('output_file.pdf', 'w+b') as f:
f.write(response.content)
```


## Python3 json example

``` python
import json
import requests

html = """<!DOCTYPE html>
<html>
<head>
<title>Horay!</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
"""

data = {
'contents': html,
}
headers = {
'Content-Type': 'application/json', # This is important
}

response = requests.post('http://127.0.0.1:6500/', data=json.dumps(data), headers=headers)

with open('output_file.pdf', 'w+b') as f:
f.write(response.content)
```
81 changes: 81 additions & 0 deletions ubuntu20-python3/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#! /usr/bin/env python
"""
WSGI APP to convert wkhtmltopdf As a webservice
:copyright: (c) 2013 by Openlabs Technologies & Consulting (P) Limited
:license: BSD, see LICENSE for more details.
"""
import json
import tempfile
import subprocess

from werkzeug.wsgi import wrap_file
from werkzeug.wrappers import Request, Response
from executor import execute


@Request.application
def application(request):
"""
To use this application, the user must send a POST request with
base64 or form encoded encoded HTML content and the wkhtmltopdf Options in
request data, with keys 'base64_html' and 'options'.
The application will return a response with the PDF file.
"""
if request.method != 'POST':
return

request_is_json = request.content_type.endswith('json')
with tempfile.NamedTemporaryFile(suffix='.html', mode='w+b') as source_file:
options = {}
if request_is_json:
# If a JSON payload is there, all data is in the payload
payload = json.loads(request.data.decode())
source_file.write(payload['contents'].encode())
options = payload.get('options', {})
elif request.files:
# First check if any files were uploaded
pdf_file = request.files['file']
f_content = pdf_file.read()
source_file.write(f_content)

# Load any options that may have been provided in options
options = json.loads(request.form.get('options', '{}'))

source_file.flush()

# Evaluate argument to run with subprocess
args = ['/usr/bin/xvfb-run', 'wkhtmltopdf']

# Add Global Options
if options:
for option, value in options.items():
args.append('--%s' % option)
if option == 'header-html':
subprocess.call(["wget", value, "-O", "/tmp/header.html"])
value = "/tmp/header.html"

if option == 'footer-html':
subprocess.call(["wget", value, "-O", "/tmp/footer.html"])
value = "/tmp/footer.html"

if value:
args.append('"%s"' % value)

# Add source file name and output file name
file_name = source_file.name
args += [file_name, file_name + ".pdf"]
# Execute the command using executor
execute(' '.join(args))

return Response(
wrap_file(request.environ, open(file_name + '.pdf', 'r+b')),
mimetype='application/pdf',
)


if __name__ == '__main__':
from werkzeug.serving import run_simple

run_simple(
'127.0.0.1', 15000, application, use_debugger=True, use_reloader=True
)