-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Port wkhtmltopdf to maintained image Copied from opg-docker repo, but now using Alpine and Python 3.x * Switch from Alpine to Debian The Alpine version doesn't include the QT patch, which is essential for rendering custom fonts. Debian is the default OS used by `python:3` * Build wkhtmltopdf image in build process * Specify version of wkhtmltopdf to use * Use apt-get rather than apt * Add cronable script to clean files from /tmp * Install and run cron on container We need `service cron restart` to ensure that the cron process starts and acknowledges our new file in `cron.hourly` * Increase cleanup delay to five minutes Just to give some breathing extra room for currently-running processes * Don't expose wkhtmltopdf port It's not necessary and wasn't the case before
- Loading branch information
Showing
7 changed files
with
113 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
FROM python:3 | ||
|
||
# Download and install wkhtmltopdf and bash (because executor can't use 'sh') | ||
RUN apt-get update \ | ||
&& apt-get -y install cron wget xfonts-base xfonts-75dpi \ | ||
&& wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.stretch_amd64.deb \ | ||
&& dpkg -i wkhtmltox_0.12.5-1.stretch_amd64.deb | ||
|
||
# Install dependencies for running web service | ||
RUN pip install werkzeug executor gunicorn | ||
|
||
ADD docker-entrypoint.sh /docker-entrypoint.sh | ||
ADD app.py /app.py | ||
ADD clean-tmp /etc/cron.hourly/clean-tmp | ||
EXPOSE 80 | ||
|
||
# Make commands executable | ||
RUN ["chmod", "+x", "/etc/cron.hourly/clean-tmp"] | ||
RUN ["chmod", "+x", "/docker-entrypoint.sh"] | ||
|
||
ENTRYPOINT ["/docker-entrypoint.sh"] | ||
|
||
CMD ["-b", "0.0.0.0:80", "--log-file", "-", "app:application"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#! /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 base64 | ||
import json | ||
import tempfile | ||
|
||
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') as source_file: | ||
|
||
if request_is_json: | ||
# If a JSON payload is there, all data is in the payload | ||
payload = json.loads(request.data) | ||
source_file.write(base64.b64decode(payload['contents'])) | ||
options = payload.get('options', {}) | ||
elif request.files: | ||
# First check if any files were uploaded | ||
source_file.write(request.files['file'].read()) | ||
# 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 = ['wkhtmltopdf'] | ||
|
||
# Add Global Options | ||
if options: | ||
for option, value in options.items(): | ||
args.append('--%s' % option) | ||
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')), | ||
mimetype='application/pdf', | ||
direct_passthrough=True, | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
from werkzeug.serving import run_simple | ||
run_simple( | ||
'127.0.0.1', 5000, application, use_debugger=True, use_reloader=True | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/sh | ||
|
||
find /tmp -mmin +5 -type f -delete |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
service cron restart | ||
|
||
usr/local/bin/gunicorn $@ |