Skip to content

WKHTMLTOPDF_OPTS and WKHTMLTOPDF_WORKDIR env variable #30

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

Open
wants to merge 2 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
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ FROM openlabs/docker-wkhtmltopdf:latest
MAINTAINER Sharoon Thomas <[email protected]>

# Install dependencies for running web service
RUN apt-get install -y python-pip
RUN pip install werkzeug executor gunicorn
RUN apt-get update \
&& apt-get install -y python-pip \
&& pip install werkzeug executor gunicorn \
&& rm -rf /var/lib/apt/lists/*

ADD app.py /app.py
EXPOSE 80
Expand Down
27 changes: 18 additions & 9 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
import json
import tempfile
import os

from werkzeug.wsgi import wrap_file
from werkzeug.wrappers import Request, Response
Expand All @@ -26,7 +27,8 @@ def application(request):

request_is_json = request.content_type.endswith('json')

with tempfile.NamedTemporaryFile(suffix='.html') as source_file:
workdir=os.environ.get('WKHTMLTOPDF_WORKDIR')
with tempfile.NamedTemporaryFile(suffix='.html', dir=workdir, delete=True) as source_file:

if request_is_json:
# If a JSON payload is there, all data is in the payload
Expand All @@ -42,8 +44,10 @@ def application(request):
source_file.flush()

# Evaluate argument to run with subprocess
args = ['wkhtmltopdf']

args = []

env_options = os.environ.get('WKHTMLTOPDF_OPTS') or ''

# Add Global Options
if options:
for option, value in options.items():
Expand All @@ -55,13 +59,18 @@ def application(request):
file_name = source_file.name
args += [file_name, file_name + ".pdf"]

# Execute the command using executor
execute(' '.join(args))
try:
# Execute the command using executor
execute('wkhtmltopdf ' + env_options + ' ' + ' '.join(args))

pdf = open(file_name + '.pdf')
return Response(
wrap_file(request.environ, pdf),
mimetype='application/pdf',
)
finally:
os.remove(file_name + '.pdf')

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


if __name__ == '__main__':
Expand Down