-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
36 lines (32 loc) · 969 Bytes
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import subprocess
import boto3
from chalice import Chalice
from jinja2 import Template
app = Chalice(app_name='html2pdf')
s3 = boto3.client('s3', region_name='eu-central-1')
BUCKET = 'bulyatestlambda'
@app.route('/url2pdf', methods=['POST'])
def url2pdf():
try:
request = app.current_request
ctx = request.json_body['template_context']
tpl = Template('<html>Hello {{ name }}!</html>')
html = tpl.render(ctx)
proc = subprocess.run(
["./wkhtmltopdf2", "-", "-"],
input=bytes(html, encoding="utf-8"),
stdout=subprocess.PIPE,
)
pdf = str(proc.stdout)
key = 'test.pdf'
s3.put_object(
Bucket=BUCKET,
Key=key,
Body=pdf,
ACL='public-read',
)
return {
"pdf_url": "https://{}.s3.amazonaws.com/{}".format(BUCKET, key)
}
except Exception as e:
return {'error': str(e)}